Skip to content

Commit

Permalink
Merge pull request #4 from goaop/fix/declaring-class
Browse files Browse the repository at this point in the history
Allow to pass reference of class to the ReflectionMethod, resolves #3
  • Loading branch information
lisachenko committed Jan 17, 2016
2 parents f16f1cc + ce10307 commit ede722f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
28 changes: 21 additions & 7 deletions src/ReflectionMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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 = [];

Expand All @@ -277,9 +290,10 @@ public static function collectFromClassNode(ClassLike $classLikeNode, $fullClass

$methodName = $classLevelNode->name;
$methods[$methodName] = new ReflectionMethod(
$fullClassName,
$reflectionClass->name,
$methodName,
$classLevelNode
$classLevelNode,
$reflectionClass
);
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/ReflectionParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down
2 changes: 1 addition & 1 deletion src/Traits/ReflectionClassLikeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit ede722f

Please sign in to comment.