From ba1ed0cd4dca3e9d0bc52635b240b09de3ae672c Mon Sep 17 00:00:00 2001 From: Matt Brown Date: Fri, 5 Jan 2018 10:15:46 -0500 Subject: [PATCH 01/11] Fix issues found by Psalm --- src/NodeCompiler/CompileNodeToValue.php | 4 +++ src/Reflection/Adapter/ReflectionClass.php | 2 +- src/Reflection/Adapter/ReflectionFunction.php | 2 +- src/Reflection/Adapter/ReflectionMethod.php | 4 +-- src/Reflection/Adapter/ReflectionObject.php | 2 +- .../Adapter/ReflectionParameter.php | 2 +- src/Reflection/Adapter/ReflectionProperty.php | 2 +- src/Reflection/ReflectionClass.php | 1 - src/Reflection/ReflectionClassConstant.php | 2 +- src/Reflection/ReflectionFunction.php | 2 ++ src/Reflection/ReflectionFunctionAbstract.php | 8 ++--- src/Reflection/ReflectionObject.php | 2 +- src/Reflection/ReflectionParameter.php | 10 +++++- src/Reflection/ReflectionProperty.php | 2 +- src/Reflection/ReflectionType.php | 2 +- .../Ast/FindReflectionsInTree.php | 4 +-- .../Reflection/SourceStubber.php | 25 ++++++++++----- .../AnonymousClassObjectSourceLocator.php | 2 +- .../Type/ClosureSourceLocator.php | 8 ++--- .../Type/FileIteratorSourceLocator.php | 4 +-- .../NamespaceNodeToReflectionTypeContext.php | 31 +++++++++++++------ 21 files changed, 77 insertions(+), 44 deletions(-) diff --git a/src/NodeCompiler/CompileNodeToValue.php b/src/NodeCompiler/CompileNodeToValue.php index bc7ecb5d0..be3d241cc 100644 --- a/src/NodeCompiler/CompileNodeToValue.php +++ b/src/NodeCompiler/CompileNodeToValue.php @@ -130,6 +130,10 @@ private function compileConstFetch(Node\Expr\ConstFetch $constNode) */ private function compileClassConstFetch(Node\Expr\ClassConstFetch $node, CompilerContext $context) { + if ($node->class instanceof Node\Expr || $node->name instanceof Node\Expr\Error) { + throw new \LogicException('Found unexpected value when evaluating class constant'); + } + $className = $node->class->toString(); if ($node->name === 'class') { diff --git a/src/Reflection/Adapter/ReflectionClass.php b/src/Reflection/Adapter/ReflectionClass.php index b92502001..f4b7d3d29 100644 --- a/src/Reflection/Adapter/ReflectionClass.php +++ b/src/Reflection/Adapter/ReflectionClass.php @@ -186,7 +186,7 @@ public function hasProperty($name) /** * {@inheritDoc} */ - public function getProperty($name) + public function getProperty($name) : ?ReflectionProperty { $betterReflectionProperty = $this->betterReflectionClass->getProperty($name); diff --git a/src/Reflection/Adapter/ReflectionFunction.php b/src/Reflection/Adapter/ReflectionFunction.php index 72c6cec1c..bb1fe11f4 100644 --- a/src/Reflection/Adapter/ReflectionFunction.php +++ b/src/Reflection/Adapter/ReflectionFunction.php @@ -27,7 +27,7 @@ public function __construct(BetterReflectionFunction $betterReflectionFunction) */ public static function export($name, $return = null) { - BetterReflectionFunction::export(...func_get_args()); + return BetterReflectionFunction::export(...func_get_args()); } /** diff --git a/src/Reflection/Adapter/ReflectionMethod.php b/src/Reflection/Adapter/ReflectionMethod.php index d93f64a65..4e467a688 100644 --- a/src/Reflection/Adapter/ReflectionMethod.php +++ b/src/Reflection/Adapter/ReflectionMethod.php @@ -34,7 +34,7 @@ public function __construct(BetterReflectionMethod $betterReflectionMethod) */ public static function export($class, $name, $return = null) { - BetterReflectionMethod::export(...func_get_args()); + return BetterReflectionMethod::export(...func_get_args()); } /** @@ -313,7 +313,7 @@ public function isDestructor() public function getClosure($object = null) { try { - $this->betterReflectionMethod->getClosure($object); + return $this->betterReflectionMethod->getClosure($object); } catch (NoObjectProvided | NotAnObject $e) { return null; } catch (Throwable $e) { diff --git a/src/Reflection/Adapter/ReflectionObject.php b/src/Reflection/Adapter/ReflectionObject.php index 88ab766a4..c7f5d8361 100644 --- a/src/Reflection/Adapter/ReflectionObject.php +++ b/src/Reflection/Adapter/ReflectionObject.php @@ -177,7 +177,7 @@ public function hasProperty($name) /** * {@inheritDoc} */ - public function getProperty($name) + public function getProperty($name) : ?ReflectionProperty { $property = $this->betterReflectionObject->getProperty($name); diff --git a/src/Reflection/Adapter/ReflectionParameter.php b/src/Reflection/Adapter/ReflectionParameter.php index 280bc00e0..78323da5a 100644 --- a/src/Reflection/Adapter/ReflectionParameter.php +++ b/src/Reflection/Adapter/ReflectionParameter.php @@ -26,7 +26,7 @@ public function __construct(BetterReflectionParameter $betterReflectionParameter */ public static function export($function, $parameter, $return = null) { - BetterReflectionParameter::export(...func_get_args()); + return BetterReflectionParameter::export(...func_get_args()); } /** diff --git a/src/Reflection/Adapter/ReflectionProperty.php b/src/Reflection/Adapter/ReflectionProperty.php index 0e07464c2..6601efd31 100644 --- a/src/Reflection/Adapter/ReflectionProperty.php +++ b/src/Reflection/Adapter/ReflectionProperty.php @@ -34,7 +34,7 @@ public function __construct(BetterReflectionProperty $betterReflectionProperty) */ public static function export($class, $name, $return = null) { - BetterReflectionProperty::export(...func_get_args()); + return BetterReflectionProperty::export(...func_get_args()); } /** diff --git a/src/Reflection/ReflectionClass.php b/src/Reflection/ReflectionClass.php index 016b8fbba..bbfb9b28b 100644 --- a/src/Reflection/ReflectionClass.php +++ b/src/Reflection/ReflectionClass.php @@ -350,7 +350,6 @@ function (ReflectionMethod $method) use ($filter) { */ public function getImmediateMethods(?int $filter = null) : array { - /** @var \ReflectionMethod[] $methods */ $methods = array_map( function (ClassMethod $methodNode) : ReflectionMethod { return ReflectionMethod::createFromNode( diff --git a/src/Reflection/ReflectionClassConstant.php b/src/Reflection/ReflectionClassConstant.php index a85668438..49f32ec13 100644 --- a/src/Reflection/ReflectionClassConstant.php +++ b/src/Reflection/ReflectionClassConstant.php @@ -188,7 +188,7 @@ public function __toString() : string /** * {@inheritDoc} */ - public static function export() : void + public static function export() { throw new Exception('Unable to export statically'); } diff --git a/src/Reflection/ReflectionFunction.php b/src/Reflection/ReflectionFunction.php index 3dbb292e1..492ae3368 100644 --- a/src/Reflection/ReflectionFunction.php +++ b/src/Reflection/ReflectionFunction.php @@ -107,6 +107,7 @@ public function getClosure() : Closure $this->assertFunctionExist($functionName); return function (...$args) use ($functionName) { + /** @var callable $functionName */ return $functionName(...$args); }; } @@ -140,6 +141,7 @@ public function invokeArgs(array $args = []) $this->assertFunctionExist($functionName); + /** @var callable $functionName */ return $functionName(...$args); } diff --git a/src/Reflection/ReflectionFunctionAbstract.php b/src/Reflection/ReflectionFunctionAbstract.php index 8c96f20d3..4dd19ea89 100644 --- a/src/Reflection/ReflectionFunctionAbstract.php +++ b/src/Reflection/ReflectionFunctionAbstract.php @@ -39,7 +39,7 @@ abstract class ReflectionFunctionAbstract implements CoreReflector public const CLOSURE_NAME = '{closure}'; /** - * @var NamespaceNode + * @var ?NamespaceNode */ private $declaringNamespace; @@ -67,7 +67,7 @@ protected function __construct() { } - public static function export() : void + public static function export() { throw new Exception('Unable to export statically'); } @@ -75,7 +75,7 @@ public static function export() : void /** * Populate the common elements of the function abstract. * - * @param Node\Stmt\ClassMethod|Node\FunctionLike|Node\Stmt|Node $node Node has to be processed by the PhpParser\NodeVisitor\NameResolver + * @param Node\Stmt\ClassMethod|Node\Stmt\Function_|Node\Expr\Closure $node Node has to be processed by the PhpParser\NodeVisitor\NameResolver * * @throws \Roave\BetterReflection\Reflection\Exception\InvalidAbstractFunctionNodeType */ @@ -515,7 +515,7 @@ public function setBodyFromClosure(Closure $newBody) : void */ public function setBodyFromString(string $newBody) : void { - $this->node->stmts = $this->loadStaticParser()->parse('node->stmts = $this->loadStaticParser()->parse('node->default; if ($defaultValueNode instanceof Node\Expr\ClassConstFetch) { + if ($defaultValueNode->class instanceof Node\Expr) { + throw new LogicException('Class constant classes cannot be expressions'); + } + + if ($defaultValueNode->name instanceof Node\Expr\Error) { + throw new LogicException('Cannot deal with error nodes'); + } + $className = $defaultValueNode->class->toString(); if ($className === 'self' || $className === 'static') { diff --git a/src/Reflection/ReflectionProperty.php b/src/Reflection/ReflectionProperty.php index 55f32c173..b7f49c4c4 100644 --- a/src/Reflection/ReflectionProperty.php +++ b/src/Reflection/ReflectionProperty.php @@ -71,7 +71,7 @@ private function __construct() { } - public static function export() : void + public static function export() { throw new Exception('Unable to export statically'); } diff --git a/src/Reflection/ReflectionType.php b/src/Reflection/ReflectionType.php index 35b80af43..1f0300097 100644 --- a/src/Reflection/ReflectionType.php +++ b/src/Reflection/ReflectionType.php @@ -25,7 +25,7 @@ class ReflectionType ]; /** - * @var $type + * @var string */ private $type; diff --git a/src/SourceLocator/Ast/FindReflectionsInTree.php b/src/SourceLocator/Ast/FindReflectionsInTree.php index 2ec96d4f1..34124cc91 100644 --- a/src/SourceLocator/Ast/FindReflectionsInTree.php +++ b/src/SourceLocator/Ast/FindReflectionsInTree.php @@ -87,7 +87,7 @@ public function __construct( $this->astConversionStrategy = $astConversionStrategy; } - public function enterNode(Node $node) : void + public function enterNode(Node $node) { if ($node instanceof Namespace_) { $this->currentNamespace = $node; @@ -115,7 +115,7 @@ public function enterNode(Node $node) : void } } - public function leaveNode(Node $node) : void + public function leaveNode(Node $node) { if ($node instanceof Namespace_) { $this->currentNamespace = null; diff --git a/src/SourceLocator/Reflection/SourceStubber.php b/src/SourceLocator/Reflection/SourceStubber.php index c3df7da84..a3c4643bb 100644 --- a/src/SourceLocator/Reflection/SourceStubber.php +++ b/src/SourceLocator/Reflection/SourceStubber.php @@ -64,11 +64,20 @@ public function __invoke(CoreReflectionClass $classReflection) : string { $classNode = $this->createClass($classReflection); - $this->addClassModifiers($classNode, $classReflection); + if ($classNode instanceof Class_) { + $this->addClassModifiers($classNode, $classReflection); + } + + if ($classNode instanceof Class_ || $classNode instanceof Interface_) { + $this->addExtendsAndImplements($classNode, $classReflection); + } + + if ($classNode instanceof Class_ || $classNode instanceof Trait_) { + $this->addProperties($classNode, $classReflection); + $this->addTraitUse($classNode, $classReflection); + } + $this->addDocComment($classNode, $classReflection); - $this->addExtendsAndImplements($classNode, $classReflection); - $this->addTraitUse($classNode, $classReflection); - $this->addProperties($classNode, $classReflection); $this->addConstants($classNode, $classReflection); $this->addMethods($classNode, $classReflection); @@ -107,9 +116,9 @@ private function addDocComment(BuilderAbstract $node, CoreReflector $reflection) } /** - * @param Class_|Interface_|Trait_ $classNode + * @param Class_ $classNode */ - private function addClassModifiers(Declaration $classNode, CoreReflectionClass $classReflection) : void + private function addClassModifiers(Class_ $classNode, CoreReflectionClass $classReflection) : void { if (! $classReflection->isInterface() && $classReflection->isAbstract()) { // Interface \Iterator is interface and abstract @@ -122,7 +131,7 @@ private function addClassModifiers(Declaration $classNode, CoreReflectionClass $ } /** - * @param Class_|Interface_|Trait_ $classNode + * @param Class_|Interface_ $classNode */ private function addExtendsAndImplements(Declaration $classNode, CoreReflectionClass $classReflection) : void { @@ -139,7 +148,7 @@ private function addExtendsAndImplements(Declaration $classNode, CoreReflectionC } foreach ($interfaces as $interfaceName) { - if ($classReflection->isInterface()) { + if ($classNode instanceof Interface_) { $classNode->extend(new FullyQualified($interfaceName)); } else { $classNode->implement(new FullyQualified($interfaceName)); diff --git a/src/SourceLocator/Type/AnonymousClassObjectSourceLocator.php b/src/SourceLocator/Type/AnonymousClassObjectSourceLocator.php index e7d6236f5..f3a44bc4e 100644 --- a/src/SourceLocator/Type/AnonymousClassObjectSourceLocator.php +++ b/src/SourceLocator/Type/AnonymousClassObjectSourceLocator.php @@ -123,7 +123,7 @@ public function __construct(string $fileName, int $startLine) $this->startLine = $startLine; } - public function enterNode(Node $node) : void + public function enterNode(Node $node) { if ($node instanceof Node\Stmt\Class_ && $node->name === null) { $this->anonymousClassNodes[] = $node; diff --git a/src/SourceLocator/Type/ClosureSourceLocator.php b/src/SourceLocator/Type/ClosureSourceLocator.php index fda7f7595..575c18266 100644 --- a/src/SourceLocator/Type/ClosureSourceLocator.php +++ b/src/SourceLocator/Type/ClosureSourceLocator.php @@ -97,7 +97,7 @@ private function getReflectionFunction(Reflector $reflector, IdentifierType $ide private $startLine; /** - * @var Node[][] + * @var (Node|null)[][] */ private $closureNodes = []; @@ -112,7 +112,7 @@ public function __construct(string $fileName, int $startLine) $this->startLine = $startLine; } - public function enterNode(Node $node) : void + public function enterNode(Node $node) { if ($node instanceof Namespace_) { $this->currentNamespace = $node; @@ -125,7 +125,7 @@ public function enterNode(Node $node) : void } } - public function leaveNode(Node $node) : void + public function leaveNode(Node $node) { if ($node instanceof Namespace_) { $this->currentNamespace = null; @@ -139,7 +139,7 @@ public function leaveNode(Node $node) : void */ public function getClosureNodes() : ?array { - /** @var Node[][] $closureNodesDataOnSameLine */ + /** @var (Node|null)[][] $closureNodesDataOnSameLine */ $closureNodesDataOnSameLine = array_values(array_filter($this->closureNodes, function (array $nodes) : bool { return $nodes[0]->getLine() === $this->startLine; })); diff --git a/src/SourceLocator/Type/FileIteratorSourceLocator.php b/src/SourceLocator/Type/FileIteratorSourceLocator.php index 17b38ba76..a6db7a00f 100644 --- a/src/SourceLocator/Type/FileIteratorSourceLocator.php +++ b/src/SourceLocator/Type/FileIteratorSourceLocator.php @@ -31,7 +31,7 @@ class FileIteratorSourceLocator implements SourceLocator private $aggregateSourceLocator; /** - * @var \Iterator|\SplFileInfo[] + * @var \Iterator<\SplFileInfo> */ private $fileSystemIterator; @@ -41,7 +41,7 @@ class FileIteratorSourceLocator implements SourceLocator private $astLocator; /** - * @param \Iterator|\SplFileInfo[] $fileInfoIterator note: only \SplFileInfo allowed in this iterator + * @param \Iterator<\SplFileInfo> $fileInfoIterator note: only \SplFileInfo allowed in this iterator * * @throws InvalidFileInfo In case of iterator not contains only SplFileInfo. */ diff --git a/src/TypesFinder/PhpDocumentor/NamespaceNodeToReflectionTypeContext.php b/src/TypesFinder/PhpDocumentor/NamespaceNodeToReflectionTypeContext.php index 8d2f7414a..16b8b7f0d 100644 --- a/src/TypesFinder/PhpDocumentor/NamespaceNodeToReflectionTypeContext.php +++ b/src/TypesFinder/PhpDocumentor/NamespaceNodeToReflectionTypeContext.php @@ -35,17 +35,28 @@ public function __invoke(?Namespace_ $namespace) : Context private function aliasesToFullyQualifiedNames(Namespace_ $namespace) : array { // flatten(flatten(map(stuff))) - return array_merge([], ...array_merge([], ...array_map(function ($use) : array { - /** @var $use Use_|GroupUse */ + return array_merge( + [], + ...array_merge( + [], + ...array_map( + /** @param Use_|GroupUse $use */ + function ($use) : array { + return array_map( + function (UseUse $useUse) use ($use) : array { + if ($use instanceof GroupUse) { + return [$useUse->alias => $use->prefix->toString() . '\\' . $useUse->name->toString()]; + } - return array_map(function (UseUse $useUse) use ($use) : array { - if ($use instanceof GroupUse) { - return [$useUse->alias => $use->prefix->toString() . '\\' . $useUse->name->toString()]; - } - - return [$useUse->alias => $useUse->name->toString()]; - }, $use->uses); - }, $this->classAlikeUses($namespace)))); + return [$useUse->alias => $useUse->name->toString()]; + }, + $use->uses + ); + }, + $this->classAlikeUses($namespace) + ) + ) + ); } /** From cc147904daae37a715f6aeb01b769c63b5e237c5 Mon Sep 17 00:00:00 2001 From: Matt Brown Date: Fri, 5 Jan 2018 10:46:10 -0500 Subject: [PATCH 02/11] Expand nullable type --- src/Reflection/ReflectionFunctionAbstract.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Reflection/ReflectionFunctionAbstract.php b/src/Reflection/ReflectionFunctionAbstract.php index 4dd19ea89..bb84db079 100644 --- a/src/Reflection/ReflectionFunctionAbstract.php +++ b/src/Reflection/ReflectionFunctionAbstract.php @@ -39,7 +39,7 @@ abstract class ReflectionFunctionAbstract implements CoreReflector public const CLOSURE_NAME = '{closure}'; /** - * @var ?NamespaceNode + * @var NamespaceNode|null */ private $declaringNamespace; From 3fac4b9a3c39bebbdbf1872f34371081b3e43dda Mon Sep 17 00:00:00 2001 From: Matt Brown Date: Fri, 5 Jan 2018 10:56:22 -0500 Subject: [PATCH 03/11] Convert assertions to docblocks --- src/NodeCompiler/CompileNodeToValue.php | 6 ++---- src/Reflection/Adapter/ReflectionClass.php | 6 ++++-- src/Reflection/Adapter/ReflectionObject.php | 6 ++++-- src/Reflection/ReflectionParameter.php | 11 +++-------- 4 files changed, 13 insertions(+), 16 deletions(-) diff --git a/src/NodeCompiler/CompileNodeToValue.php b/src/NodeCompiler/CompileNodeToValue.php index be3d241cc..3fa0cb4d0 100644 --- a/src/NodeCompiler/CompileNodeToValue.php +++ b/src/NodeCompiler/CompileNodeToValue.php @@ -130,10 +130,7 @@ private function compileConstFetch(Node\Expr\ConstFetch $constNode) */ private function compileClassConstFetch(Node\Expr\ClassConstFetch $node, CompilerContext $context) { - if ($node->class instanceof Node\Expr || $node->name instanceof Node\Expr\Error) { - throw new \LogicException('Found unexpected value when evaluating class constant'); - } - + /** @var Node\Name $node->class */ $className = $node->class->toString(); if ($node->name === 'class') { @@ -152,6 +149,7 @@ private function compileClassConstFetch(Node\Expr\ClassConstFetch $node, Compile $classInfo = $context->getReflector()->reflect($className); } + /** @var string $node->name */ $reflectionConstant = $classInfo->getReflectionConstant($node->name); return $this->__invoke( diff --git a/src/Reflection/Adapter/ReflectionClass.php b/src/Reflection/Adapter/ReflectionClass.php index f4b7d3d29..ac1412929 100644 --- a/src/Reflection/Adapter/ReflectionClass.php +++ b/src/Reflection/Adapter/ReflectionClass.php @@ -184,9 +184,11 @@ public function hasProperty($name) } /** - * {@inheritDoc} + * @param string $name + * + * @return ReflectionProperty|null */ - public function getProperty($name) : ?ReflectionProperty + public function getProperty($name) { $betterReflectionProperty = $this->betterReflectionClass->getProperty($name); diff --git a/src/Reflection/Adapter/ReflectionObject.php b/src/Reflection/Adapter/ReflectionObject.php index c7f5d8361..f64a07d05 100644 --- a/src/Reflection/Adapter/ReflectionObject.php +++ b/src/Reflection/Adapter/ReflectionObject.php @@ -175,9 +175,11 @@ public function hasProperty($name) } /** - * {@inheritDoc} + * @param string $name + * + * @return ReflectionProperty|null */ - public function getProperty($name) : ?ReflectionProperty + public function getProperty($name) { $property = $this->betterReflectionObject->getProperty($name); diff --git a/src/Reflection/ReflectionParameter.php b/src/Reflection/ReflectionParameter.php index 619a6d3a2..627b01fd9 100644 --- a/src/Reflection/ReflectionParameter.php +++ b/src/Reflection/ReflectionParameter.php @@ -192,21 +192,16 @@ private function parseDefaultValueNode() : void $defaultValueNode = $this->node->default; if ($defaultValueNode instanceof Node\Expr\ClassConstFetch) { - if ($defaultValueNode->class instanceof Node\Expr) { - throw new LogicException('Class constant classes cannot be expressions'); - } - - if ($defaultValueNode->name instanceof Node\Expr\Error) { - throw new LogicException('Cannot deal with error nodes'); - } - + /** @var Node\Name $defaultValueNode->class */ $className = $defaultValueNode->class->toString(); if ($className === 'self' || $className === 'static') { + /** @var string $defaultValueNode->name */ $className = $this->findParentClassDeclaringConstant($defaultValueNode->name); } $this->isDefaultValueConstant = true; + /** @var string $defaultValueNode->name */ $this->defaultValueConstantName = $className . '::' . $defaultValueNode->name; } From b09c165443f2c9fc95ff330a4773a130cc18ee4c Mon Sep 17 00:00:00 2001 From: Matt Brown Date: Mon, 29 Jan 2018 15:59:04 -0500 Subject: [PATCH 04/11] Remove ternary to empty array --- src/Reflection/ReflectionFunctionAbstract.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Reflection/ReflectionFunctionAbstract.php b/src/Reflection/ReflectionFunctionAbstract.php index bb84db079..410ffd763 100644 --- a/src/Reflection/ReflectionFunctionAbstract.php +++ b/src/Reflection/ReflectionFunctionAbstract.php @@ -515,7 +515,8 @@ public function setBodyFromClosure(Closure $newBody) : void */ public function setBodyFromString(string $newBody) : void { - $this->node->stmts = $this->loadStaticParser()->parse('node->stmts = $this->loadStaticParser()->parse(' Date: Tue, 30 Jan 2018 15:05:17 -0500 Subject: [PATCH 05/11] Fix coding standard violations --- src/Reflection/ReflectionFunctionAbstract.php | 5 ++++- src/Reflection/ReflectionObject.php | 2 -- src/Reflection/ReflectionParameter.php | 5 ++++- src/Reflection/ReflectionProperty.php | 3 +++ src/SourceLocator/Ast/FindReflectionsInTree.php | 6 ++++++ src/SourceLocator/Reflection/SourceStubber.php | 3 --- .../Type/AnonymousClassObjectSourceLocator.php | 3 +++ src/SourceLocator/Type/ClosureSourceLocator.php | 6 ++++++ 8 files changed, 26 insertions(+), 7 deletions(-) diff --git a/src/Reflection/ReflectionFunctionAbstract.php b/src/Reflection/ReflectionFunctionAbstract.php index 410ffd763..0247952b1 100644 --- a/src/Reflection/ReflectionFunctionAbstract.php +++ b/src/Reflection/ReflectionFunctionAbstract.php @@ -67,6 +67,9 @@ protected function __construct() { } + /** + * {@inheritDoc} + */ public static function export() { throw new Exception('Unable to export statically'); @@ -515,7 +518,7 @@ public function setBodyFromClosure(Closure $newBody) : void */ public function setBodyFromString(string $newBody) : void { - /** @var Node[] */ + /** @var Node[] $this->node->stmts */ $this->node->stmts = $this->loadStaticParser()->parse('findParentClassDeclaringConstant($defaultValueNode->name); } - $this->isDefaultValueConstant = true; + $this->isDefaultValueConstant = true; /** @var string $defaultValueNode->name */ $this->defaultValueConstantName = $className . '::' . $defaultValueNode->name; } diff --git a/src/Reflection/ReflectionProperty.php b/src/Reflection/ReflectionProperty.php index b7f49c4c4..03cc21b8a 100644 --- a/src/Reflection/ReflectionProperty.php +++ b/src/Reflection/ReflectionProperty.php @@ -71,6 +71,9 @@ private function __construct() { } + /** + * {@inheritDoc} + */ public static function export() { throw new Exception('Unable to export statically'); diff --git a/src/SourceLocator/Ast/FindReflectionsInTree.php b/src/SourceLocator/Ast/FindReflectionsInTree.php index 34124cc91..1fb90a9fe 100644 --- a/src/SourceLocator/Ast/FindReflectionsInTree.php +++ b/src/SourceLocator/Ast/FindReflectionsInTree.php @@ -87,6 +87,9 @@ public function __construct( $this->astConversionStrategy = $astConversionStrategy; } + /** + * {@inheritDoc} + */ public function enterNode(Node $node) { if ($node instanceof Namespace_) { @@ -115,6 +118,9 @@ public function enterNode(Node $node) } } + /** + * {@inheritDoc} + */ public function leaveNode(Node $node) { if ($node instanceof Namespace_) { diff --git a/src/SourceLocator/Reflection/SourceStubber.php b/src/SourceLocator/Reflection/SourceStubber.php index a3c4643bb..1f646289e 100644 --- a/src/SourceLocator/Reflection/SourceStubber.php +++ b/src/SourceLocator/Reflection/SourceStubber.php @@ -115,9 +115,6 @@ private function addDocComment(BuilderAbstract $node, CoreReflector $reflection) } } - /** - * @param Class_ $classNode - */ private function addClassModifiers(Class_ $classNode, CoreReflectionClass $classReflection) : void { if (! $classReflection->isInterface() && $classReflection->isAbstract()) { diff --git a/src/SourceLocator/Type/AnonymousClassObjectSourceLocator.php b/src/SourceLocator/Type/AnonymousClassObjectSourceLocator.php index f3a44bc4e..8cb31c5bd 100644 --- a/src/SourceLocator/Type/AnonymousClassObjectSourceLocator.php +++ b/src/SourceLocator/Type/AnonymousClassObjectSourceLocator.php @@ -123,6 +123,9 @@ public function __construct(string $fileName, int $startLine) $this->startLine = $startLine; } + /** + * {@inheritDoc} + */ public function enterNode(Node $node) { if ($node instanceof Node\Stmt\Class_ && $node->name === null) { diff --git a/src/SourceLocator/Type/ClosureSourceLocator.php b/src/SourceLocator/Type/ClosureSourceLocator.php index 575c18266..15c11ddf4 100644 --- a/src/SourceLocator/Type/ClosureSourceLocator.php +++ b/src/SourceLocator/Type/ClosureSourceLocator.php @@ -112,6 +112,9 @@ public function __construct(string $fileName, int $startLine) $this->startLine = $startLine; } + /** + * {@inheritDoc} + */ public function enterNode(Node $node) { if ($node instanceof Namespace_) { @@ -125,6 +128,9 @@ public function enterNode(Node $node) } } + /** + * {@inheritDoc} + */ public function leaveNode(Node $node) { if ($node instanceof Namespace_) { From b6a4914afb376804b357230a132ef19b198bc215 Mon Sep 17 00:00:00 2001 From: Matt Brown Date: Wed, 31 Jan 2018 11:44:57 -0500 Subject: [PATCH 06/11] Revert changes to void return types --- src/Reflection/ReflectionClassConstant.php | 5 +---- src/Reflection/ReflectionFunctionAbstract.php | 6 +----- src/Reflection/ReflectionParameter.php | 5 +---- src/Reflection/ReflectionProperty.php | 5 +---- src/SourceLocator/Ast/FindReflectionsInTree.php | 10 ++-------- .../Type/AnonymousClassObjectSourceLocator.php | 5 +---- src/SourceLocator/Type/ClosureSourceLocator.php | 10 ++-------- 7 files changed, 9 insertions(+), 37 deletions(-) diff --git a/src/Reflection/ReflectionClassConstant.php b/src/Reflection/ReflectionClassConstant.php index 49f32ec13..e5acdfd4b 100644 --- a/src/Reflection/ReflectionClassConstant.php +++ b/src/Reflection/ReflectionClassConstant.php @@ -185,10 +185,7 @@ public function __toString() : string return ReflectionClassConstantStringCast::toString($this); } - /** - * {@inheritDoc} - */ - public static function export() + public static function export() : void { throw new Exception('Unable to export statically'); } diff --git a/src/Reflection/ReflectionFunctionAbstract.php b/src/Reflection/ReflectionFunctionAbstract.php index 0247952b1..feba490a6 100644 --- a/src/Reflection/ReflectionFunctionAbstract.php +++ b/src/Reflection/ReflectionFunctionAbstract.php @@ -67,10 +67,7 @@ protected function __construct() { } - /** - * {@inheritDoc} - */ - public static function export() + public static function export() : void { throw new Exception('Unable to export statically'); } @@ -518,7 +515,6 @@ public function setBodyFromClosure(Closure $newBody) : void */ public function setBodyFromString(string $newBody) : void { - /** @var Node[] $this->node->stmts */ $this->node->stmts = $this->loadStaticParser()->parse('astConversionStrategy = $astConversionStrategy; } - /** - * {@inheritDoc} - */ - public function enterNode(Node $node) + public function enterNode(Node $node) : void { if ($node instanceof Namespace_) { $this->currentNamespace = $node; @@ -118,10 +115,7 @@ public function enterNode(Node $node) } } - /** - * {@inheritDoc} - */ - public function leaveNode(Node $node) + public function leaveNode(Node $node) : void { if ($node instanceof Namespace_) { $this->currentNamespace = null; diff --git a/src/SourceLocator/Type/AnonymousClassObjectSourceLocator.php b/src/SourceLocator/Type/AnonymousClassObjectSourceLocator.php index 8cb31c5bd..e7d6236f5 100644 --- a/src/SourceLocator/Type/AnonymousClassObjectSourceLocator.php +++ b/src/SourceLocator/Type/AnonymousClassObjectSourceLocator.php @@ -123,10 +123,7 @@ public function __construct(string $fileName, int $startLine) $this->startLine = $startLine; } - /** - * {@inheritDoc} - */ - public function enterNode(Node $node) + public function enterNode(Node $node) : void { if ($node instanceof Node\Stmt\Class_ && $node->name === null) { $this->anonymousClassNodes[] = $node; diff --git a/src/SourceLocator/Type/ClosureSourceLocator.php b/src/SourceLocator/Type/ClosureSourceLocator.php index 15c11ddf4..ff4af7dad 100644 --- a/src/SourceLocator/Type/ClosureSourceLocator.php +++ b/src/SourceLocator/Type/ClosureSourceLocator.php @@ -112,10 +112,7 @@ public function __construct(string $fileName, int $startLine) $this->startLine = $startLine; } - /** - * {@inheritDoc} - */ - public function enterNode(Node $node) + public function enterNode(Node $node) : void { if ($node instanceof Namespace_) { $this->currentNamespace = $node; @@ -128,10 +125,7 @@ public function enterNode(Node $node) } } - /** - * {@inheritDoc} - */ - public function leaveNode(Node $node) + public function leaveNode(Node $node) : void { if ($node instanceof Namespace_) { $this->currentNamespace = null; From 633de247e0487324b38e7de0dbfdc8ab73cbf91b Mon Sep 17 00:00:00 2001 From: Matt Brown Date: Wed, 31 Jan 2018 11:47:30 -0500 Subject: [PATCH 07/11] Revert generic iterator change --- src/SourceLocator/Type/FileIteratorSourceLocator.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/SourceLocator/Type/FileIteratorSourceLocator.php b/src/SourceLocator/Type/FileIteratorSourceLocator.php index a6db7a00f..17b38ba76 100644 --- a/src/SourceLocator/Type/FileIteratorSourceLocator.php +++ b/src/SourceLocator/Type/FileIteratorSourceLocator.php @@ -31,7 +31,7 @@ class FileIteratorSourceLocator implements SourceLocator private $aggregateSourceLocator; /** - * @var \Iterator<\SplFileInfo> + * @var \Iterator|\SplFileInfo[] */ private $fileSystemIterator; @@ -41,7 +41,7 @@ class FileIteratorSourceLocator implements SourceLocator private $astLocator; /** - * @param \Iterator<\SplFileInfo> $fileInfoIterator note: only \SplFileInfo allowed in this iterator + * @param \Iterator|\SplFileInfo[] $fileInfoIterator note: only \SplFileInfo allowed in this iterator * * @throws InvalidFileInfo In case of iterator not contains only SplFileInfo. */ From 60ac692f9c022cb5e981033e18fffb7bc13eb6d7 Mon Sep 17 00:00:00 2001 From: Matthew Brown Date: Fri, 2 Feb 2018 16:21:33 -0500 Subject: [PATCH 08/11] Revert changes to exports that will always throw exceptions --- src/Reflection/Adapter/ReflectionFunction.php | 2 +- src/Reflection/Adapter/ReflectionMethod.php | 2 +- src/Reflection/Adapter/ReflectionParameter.php | 2 +- src/Reflection/Adapter/ReflectionProperty.php | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Reflection/Adapter/ReflectionFunction.php b/src/Reflection/Adapter/ReflectionFunction.php index bb1fe11f4..72c6cec1c 100644 --- a/src/Reflection/Adapter/ReflectionFunction.php +++ b/src/Reflection/Adapter/ReflectionFunction.php @@ -27,7 +27,7 @@ public function __construct(BetterReflectionFunction $betterReflectionFunction) */ public static function export($name, $return = null) { - return BetterReflectionFunction::export(...func_get_args()); + BetterReflectionFunction::export(...func_get_args()); } /** diff --git a/src/Reflection/Adapter/ReflectionMethod.php b/src/Reflection/Adapter/ReflectionMethod.php index 4e467a688..8e52c1a21 100644 --- a/src/Reflection/Adapter/ReflectionMethod.php +++ b/src/Reflection/Adapter/ReflectionMethod.php @@ -34,7 +34,7 @@ public function __construct(BetterReflectionMethod $betterReflectionMethod) */ public static function export($class, $name, $return = null) { - return BetterReflectionMethod::export(...func_get_args()); + BetterReflectionMethod::export(...func_get_args()); } /** diff --git a/src/Reflection/Adapter/ReflectionParameter.php b/src/Reflection/Adapter/ReflectionParameter.php index 78323da5a..280bc00e0 100644 --- a/src/Reflection/Adapter/ReflectionParameter.php +++ b/src/Reflection/Adapter/ReflectionParameter.php @@ -26,7 +26,7 @@ public function __construct(BetterReflectionParameter $betterReflectionParameter */ public static function export($function, $parameter, $return = null) { - return BetterReflectionParameter::export(...func_get_args()); + BetterReflectionParameter::export(...func_get_args()); } /** diff --git a/src/Reflection/Adapter/ReflectionProperty.php b/src/Reflection/Adapter/ReflectionProperty.php index 6601efd31..0e07464c2 100644 --- a/src/Reflection/Adapter/ReflectionProperty.php +++ b/src/Reflection/Adapter/ReflectionProperty.php @@ -34,7 +34,7 @@ public function __construct(BetterReflectionProperty $betterReflectionProperty) */ public static function export($class, $name, $return = null) { - return BetterReflectionProperty::export(...func_get_args()); + BetterReflectionProperty::export(...func_get_args()); } /** From c33fedf9eb69ec8f43cced42991ff62ca9751ed2 Mon Sep 17 00:00:00 2001 From: Matthew Brown Date: Sat, 3 Feb 2018 09:27:21 -0500 Subject: [PATCH 09/11] Revert ReflectionClass::getProperty return type change --- src/Reflection/Adapter/ReflectionClass.php | 4 +--- src/Reflection/Adapter/ReflectionObject.php | 4 +--- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/src/Reflection/Adapter/ReflectionClass.php b/src/Reflection/Adapter/ReflectionClass.php index ac1412929..b92502001 100644 --- a/src/Reflection/Adapter/ReflectionClass.php +++ b/src/Reflection/Adapter/ReflectionClass.php @@ -184,9 +184,7 @@ public function hasProperty($name) } /** - * @param string $name - * - * @return ReflectionProperty|null + * {@inheritDoc} */ public function getProperty($name) { diff --git a/src/Reflection/Adapter/ReflectionObject.php b/src/Reflection/Adapter/ReflectionObject.php index f64a07d05..88ab766a4 100644 --- a/src/Reflection/Adapter/ReflectionObject.php +++ b/src/Reflection/Adapter/ReflectionObject.php @@ -175,9 +175,7 @@ public function hasProperty($name) } /** - * @param string $name - * - * @return ReflectionProperty|null + * {@inheritDoc} */ public function getProperty($name) { From 688934cd12e36a43f00ae7dc12f28a4b933b8076 Mon Sep 17 00:00:00 2001 From: Matthew Brown Date: Sat, 3 Feb 2018 10:16:16 -0500 Subject: [PATCH 10/11] Add asserts to testAdapterMethods --- .../Adapter/ReflectionMethodTest.php | 25 ++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/test/unit/Reflection/Adapter/ReflectionMethodTest.php b/test/unit/Reflection/Adapter/ReflectionMethodTest.php index 1a53c9a5a..58e71d3ce 100644 --- a/test/unit/Reflection/Adapter/ReflectionMethodTest.php +++ b/test/unit/Reflection/Adapter/ReflectionMethodTest.php @@ -11,6 +11,8 @@ use Roave\BetterReflection\Reflection\Adapter\Exception\NotImplemented; use Roave\BetterReflection\Reflection\Adapter\ReflectionClass as ReflectionClassAdapter; use Roave\BetterReflection\Reflection\Adapter\ReflectionMethod as ReflectionMethodAdapter; +use Roave\BetterReflection\Reflection\Adapter\ReflectionParameter as ReflectionParameterAdapter; +use Roave\BetterReflection\Reflection\Adapter\ReflectionType as ReflectionTypeAdapter; use Roave\BetterReflection\Reflection\Exception\NoObjectProvided; use Roave\BetterReflection\Reflection\Exception\NotAnObject; use Roave\BetterReflection\Reflection\Exception\ObjectNotInstanceOfClass; @@ -67,7 +69,7 @@ public function methodExpectationProvider() : array ['isUserDefined', null, true, []], ['getClosureThis', NotImplemented::class, null, []], ['getClosureScopeClass', NotImplemented::class, null, []], - ['getDocComment', null, '', []], + ['getDocComment', null, false, []], ['getStartLine', null, 123, []], ['getEndLine', null, 123, []], ['getExtension', NotImplemented::class, null, []], @@ -121,8 +123,25 @@ public function testAdapterMethods(string $methodName, ?string $expectedExceptio $this->expectException($expectedException); } - $adapter = new ReflectionMethodAdapter($reflectionStub); - $adapter->{$methodName}(...$args); + $adapter = new ReflectionMethodAdapter($reflectionStub); + $adapterReturnValue = $adapter->{$methodName}(...$args); + + switch ($methodName) { + case 'getParameters': + $this->assertContainsOnly(ReflectionParameterAdapter::class, $adapterReturnValue); + break; + + case 'getReturnType': + $this->assertInstanceOf(ReflectionTypeAdapter::class, $adapterReturnValue); + break; + + case 'getPrototype': + $this->assertInstanceOf(ReflectionMethodAdapter::class, $adapterReturnValue); + break; + + default: + $this->assertEquals($returnValue, $adapterReturnValue); + } } public function testExport() : void From b55473499384385e671fa14a8fadf9c8ed0eced4 Mon Sep 17 00:00:00 2001 From: Matthew Brown Date: Sat, 3 Feb 2018 10:46:07 -0500 Subject: [PATCH 11/11] Add back docblock annotation --- src/Reflection/ReflectionClass.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Reflection/ReflectionClass.php b/src/Reflection/ReflectionClass.php index bbfb9b28b..b71b2e16f 100644 --- a/src/Reflection/ReflectionClass.php +++ b/src/Reflection/ReflectionClass.php @@ -350,6 +350,7 @@ function (ReflectionMethod $method) use ($filter) { */ public function getImmediateMethods(?int $filter = null) : array { + /** @var ReflectionMethod[] $methods */ $methods = array_map( function (ClassMethod $methodNode) : ReflectionMethod { return ReflectionMethod::createFromNode(