From 336b84a18a994e2007abe08591100bf30bc14998 Mon Sep 17 00:00:00 2001 From: Lisachenko Alexander Date: Sun, 17 Jan 2016 20:58:09 +0300 Subject: [PATCH 1/3] Allow to pass reference of class to the ReflectionMethod, resolves #3 --- src/ReflectionMethod.php | 26 +++++++++++++++++++------ src/Traits/ReflectionClassLikeTrait.php | 2 +- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/ReflectionMethod.php b/src/ReflectionMethod.php index 6577f6d1..c1d0cb34 100644 --- a/src/ReflectionMethod.php +++ b/src/ReflectionMethod.php @@ -30,17 +30,30 @@ class ReflectionMethod extends BaseReflectionMethod */ private $className; + /** + * Optional declaring class reference + * + * @var ReflectionClass + */ + private $declaringClass; + /** * Initializes reflection instance for the method node * * @param string $className Name of the class * @param string $methodName Name of the method * @param ClassMethod $classMethodNode AST-node for method + * @param ReflectionClass $declaringClass Optional declaring class */ - public function __construct($className, $methodName, ClassMethod $classMethodNode = null) + public function __construct( + $className, + $methodName, + ClassMethod $classMethodNode = null, + ReflectionClass $declaringClass = null) { //for some reason, ReflectionMethod->getNamespaceName in php always returns '', so we shouldn't use it too $this->className = $className; + $this->declaringClass = $declaringClass; $this->functionLikeNode = $classMethodNode ?: ReflectionEngine::parseClassMethod($className, $methodName); // Let's unset original read-only properties to have a control over them via __get @@ -116,7 +129,7 @@ public function getClosure($object) */ public function getDeclaringClass() { - return new ReflectionClass($this->className); + return isset($this->declaringClass) ? $this->declaringClass : new ReflectionClass($this->className); } /** @@ -263,11 +276,11 @@ public function setAccessible($accessible) * Parses methods from the concrete class node * * @param ClassLike $classLikeNode Class-like node - * @param string $fullClassName FQN of the class + * @param ReflectionClass $reflectionClass Reflection of the class * * @return array|ReflectionMethod[] */ - public static function collectFromClassNode(ClassLike $classLikeNode, $fullClassName) + public static function collectFromClassNode(ClassLike $classLikeNode, ReflectionClass $reflectionClass) { $methods = []; @@ -277,9 +290,10 @@ public static function collectFromClassNode(ClassLike $classLikeNode, $fullClass $methodName = $classLevelNode->name; $methods[$methodName] = new ReflectionMethod( - $fullClassName, + $reflectionClass->getName(), $methodName, - $classLevelNode + $classLevelNode, + $reflectionClass ); } } diff --git a/src/Traits/ReflectionClassLikeTrait.php b/src/Traits/ReflectionClassLikeTrait.php index d035c77d..628b1282 100644 --- a/src/Traits/ReflectionClassLikeTrait.php +++ b/src/Traits/ReflectionClassLikeTrait.php @@ -348,7 +348,7 @@ public function getMethod($name) public function getMethods($filter = null) { if (!isset($this->methods)) { - $directMethods = ReflectionMethod::collectFromClassNode($this->classLikeNode, $this->getName()); + $directMethods = ReflectionMethod::collectFromClassNode($this->classLikeNode, $this); $parentMethods = $this->recursiveCollect(function (array &$result, \ReflectionClass $instance, $isParent) { $reflectionMethods = []; foreach ($instance->getMethods() as $reflectionMethod) { From 683d84cb8ff75c1aa6b4f2013393a171749dd068 Mon Sep 17 00:00:00 2001 From: Lisachenko Alexander Date: Sun, 17 Jan 2016 22:50:31 +0300 Subject: [PATCH 2/3] PHP7.0.2 represents floating numbers with additional zeroes --- src/ReflectionParameter.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/ReflectionParameter.php b/src/ReflectionParameter.php index 2479c2be..dbb183fb 100644 --- a/src/ReflectionParameter.php +++ b/src/ReflectionParameter.php @@ -132,6 +132,10 @@ public function __toString() if (is_string($defaultValue) && strlen($defaultValue) > 15) { $defaultValue = substr($defaultValue, 0, 15) . '...'; } + /* @see https://3v4l.org/DJOEb for behaviour changes */ + if (is_double($defaultValue) && fmod($defaultValue, 1.0) === 0.0) { + $defaultValue = (int) $defaultValue; + } $defaultValue = str_replace('\\\\', '\\', var_export($defaultValue, true)); } From ce10307dbcc433cd4c069b151e1c8936d18401d6 Mon Sep 17 00:00:00 2001 From: Lisachenko Alexander Date: Sun, 17 Jan 2016 22:55:28 +0300 Subject: [PATCH 3/3] Fix CS issues --- src/ReflectionMethod.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ReflectionMethod.php b/src/ReflectionMethod.php index c1d0cb34..c44971d7 100644 --- a/src/ReflectionMethod.php +++ b/src/ReflectionMethod.php @@ -49,8 +49,8 @@ public function __construct( $className, $methodName, ClassMethod $classMethodNode = null, - ReflectionClass $declaringClass = null) - { + ReflectionClass $declaringClass = null + ) { //for some reason, ReflectionMethod->getNamespaceName in php always returns '', so we shouldn't use it too $this->className = $className; $this->declaringClass = $declaringClass; @@ -290,7 +290,7 @@ public static function collectFromClassNode(ClassLike $classLikeNode, Reflection $methodName = $classLevelNode->name; $methods[$methodName] = new ReflectionMethod( - $reflectionClass->getName(), + $reflectionClass->name, $methodName, $classLevelNode, $reflectionClass