diff --git a/rules/coding-style/src/Rector/Throw_/AnnotateThrowablesRector.php b/rules/coding-style/src/Rector/Throw_/AnnotateThrowablesRector.php index 7ba8eb3b5464..173f1c665c9b 100644 --- a/rules/coding-style/src/Rector/Throw_/AnnotateThrowablesRector.php +++ b/rules/coding-style/src/Rector/Throw_/AnnotateThrowablesRector.php @@ -24,7 +24,7 @@ use Rector\Core\RectorDefinition\CodeSample; use Rector\Core\RectorDefinition\RectorDefinition; use Rector\Core\Reflection\ClassMethodReflectionHelper; -use Rector\Core\Reflection\FunctionReflectionHelper; +use Rector\Core\Reflection\FunctionAnnotationResolver; use Rector\NodeTypeResolver\Node\AttributeKey; use ReflectionFunction; @@ -39,9 +39,9 @@ final class AnnotateThrowablesRector extends AbstractRector private $throwablesToAnnotate = []; /** - * @var FunctionReflectionHelper + * @var FunctionAnnotationResolver */ - private $functionReflectionHelper; + private $functionAnnotationResolver; /** * @var ClassMethodReflectionHelper @@ -56,9 +56,9 @@ final class AnnotateThrowablesRector extends AbstractRector public function __construct( ClassMethodReflectionHelper $classMethodReflectionHelper, ClassResolver $classResolver, - FunctionReflectionHelper $functionReflectionHelper + FunctionAnnotationResolver $functionAnnotationResolver ) { - $this->functionReflectionHelper = $functionReflectionHelper; + $this->functionAnnotationResolver = $functionAnnotationResolver; $this->classMethodReflectionHelper = $classMethodReflectionHelper; $this->classResolver = $classResolver; } @@ -197,7 +197,7 @@ private function analyzeStmtFuncCall(FuncCall $funcCall): int } $reflectedFunction = new ReflectionFunction($functionFqn); - $foundThrownThrowables = $this->functionReflectionHelper->extractFunctionAnnotatedThrows($reflectedFunction); + $foundThrownThrowables = $this->functionAnnotationResolver->extractFunctionAnnotatedThrows($reflectedFunction); $alreadyAnnotatedThrowables = $this->extractAlreadyAnnotatedThrowables($funcCall); return $this->diffThrowables($foundThrownThrowables, $alreadyAnnotatedThrowables); } diff --git a/src/PhpDoc/PhpDocTagsFinder.php b/src/PhpDoc/PhpDocTagsFinder.php index 2780f70845ab..1adda02c8009 100644 --- a/src/PhpDoc/PhpDocTagsFinder.php +++ b/src/PhpDoc/PhpDocTagsFinder.php @@ -6,6 +6,9 @@ use Nette\Utils\Strings; +/** + * @see \Rector\Core\Tests\PhpDoc\PhpDocTagsFinderTest + */ final class PhpDocTagsFinder { /** @@ -31,6 +34,7 @@ public function extractTagsFromStringedDocblock(string $dockblock, string $tagNa // This is required as @return, for example, can be written as "@return ClassOne|ClassTwo|ClassThree" return explode('|', str_replace($tagName . ' ', '', $matchingTag)); }; + $matchingTags = array_map($explode, $matchingTags); return array_merge(...$matchingTags); diff --git a/src/Reflection/FunctionReflectionHelper.php b/src/Reflection/FunctionAnnotationResolver.php similarity index 98% rename from src/Reflection/FunctionReflectionHelper.php rename to src/Reflection/FunctionAnnotationResolver.php index 57454436f367..d4103cd2e80f 100644 --- a/src/Reflection/FunctionReflectionHelper.php +++ b/src/Reflection/FunctionAnnotationResolver.php @@ -12,7 +12,7 @@ use Rector\Core\PhpParser\Parser\FunctionParser; use ReflectionFunction; -final class FunctionReflectionHelper +final class FunctionAnnotationResolver { /** * @var ClassNaming diff --git a/tests/PhpDoc/PhpDocTagsFinderTest.php b/tests/PhpDoc/PhpDocTagsFinderTest.php new file mode 100644 index 000000000000..41e50627a28e --- /dev/null +++ b/tests/PhpDoc/PhpDocTagsFinderTest.php @@ -0,0 +1,34 @@ +bootKernel(RectorKernel::class); + $this->phpDocTagsFinder = self::$container->get(PhpDocTagsFinder::class); + } + + public function test(): void + { + $docContent = FileSystem::read(__DIR__ . '/Source/doc_block_throws.txt'); + + $throwsTags = $this->phpDocTagsFinder->extractTagsFromStringedDocblock($docContent, 'throws'); + + $this->assertCount(3, $throwsTags); + $this->assertSame(['A', 'B', 'C'], $throwsTags); + } +} diff --git a/tests/PhpDoc/Source/doc_block_throws.txt b/tests/PhpDoc/Source/doc_block_throws.txt new file mode 100644 index 000000000000..67996b1f03e3 --- /dev/null +++ b/tests/PhpDoc/Source/doc_block_throws.txt @@ -0,0 +1,3 @@ +/** + * @throws A|B|C + */ diff --git a/tests/Rector/MagicDisclosure/UnsetAndIssetToMethodCallRector/Fixture/fixture.php.inc b/tests/Rector/MagicDisclosure/UnsetAndIssetToMethodCallRector/Fixture/fixture.php.inc index fdbb379b3430..66ee98b111c5 100644 --- a/tests/Rector/MagicDisclosure/UnsetAndIssetToMethodCallRector/Fixture/fixture.php.inc +++ b/tests/Rector/MagicDisclosure/UnsetAndIssetToMethodCallRector/Fixture/fixture.php.inc @@ -1,21 +1,31 @@ ----- hasService('someService'); - $container->removeService('someService'); + function unsetAndIsset() + { + $container = new \Rector\Core\Tests\Rector\MagicDisclosure\UnsetAndIssetToMethodCallRector\Source\LocalContainer; + $container->hasService('someService'); + $container->removeService('someService'); + } } ?> diff --git a/tests/Rector/MagicDisclosure/UnsetAndIssetToMethodCallRector/Source/LocalContainer.php b/tests/Rector/MagicDisclosure/UnsetAndIssetToMethodCallRector/Source/LocalContainer.php new file mode 100644 index 000000000000..30b825ec3f23 --- /dev/null +++ b/tests/Rector/MagicDisclosure/UnsetAndIssetToMethodCallRector/Source/LocalContainer.php @@ -0,0 +1,11 @@ + [ '$typeToMethodCalls' => [ - Container::class => [ + LocalContainer::class => [ 'isset' => 'hasService', 'unset' => 'removeService', ],