From 00a3e431178225c71098843ea6b479e47deaa3ff Mon Sep 17 00:00:00 2001 From: TomasVotruba Date: Sat, 10 Apr 2021 17:29:36 +0200 Subject: [PATCH] fix string match return type --- .../Annotation/AnnotationExtractorTest.php | 25 ++++++++----------- .../Annotation/AnnotationExtractor.php | 12 +++++---- 2 files changed, 18 insertions(+), 19 deletions(-) diff --git a/packages-tests/ChangesReporting/Annotation/AnnotationExtractorTest.php b/packages-tests/ChangesReporting/Annotation/AnnotationExtractorTest.php index 9e57afa46135..43c6dc8e7108 100644 --- a/packages-tests/ChangesReporting/Annotation/AnnotationExtractorTest.php +++ b/packages-tests/ChangesReporting/Annotation/AnnotationExtractorTest.php @@ -21,26 +21,23 @@ protected function setUp(): void $this->annotationExtractor = new AnnotationExtractor(); } - public function extractAnnotationProvider(): Iterator + /** + * @dataProvider extractAnnotationProvider() + */ + public function testExtractAnnotationFromClass(string $className, string $annotation, ?string $expected): void { - $rectorWithChangelog = new RectorWithChangelog(); + $value = $this->annotationExtractor->extractAnnotationFromClass($className, $annotation); + $this->assertSame($expected, $value); + } + public function extractAnnotationProvider(): Iterator + { yield 'Class with changelog annotation' => [ - get_class($rectorWithChangelog), + RectorWithChangelog::class, '@changelog', 'https://github.com/rectorphp/rector/blob/master/docs/rector_rules_overview.md', ]; - $rectorWithOutChangelog = new RectorWithOutChangelog(); - yield 'Class without changelog annotation' => [get_class($rectorWithOutChangelog), '@changelog', null]; - } - - /** - * @dataProvider extractAnnotationProvider - */ - public function testExtractAnnotationFromClass(string $className, string $annotation, ?string $expected): void - { - $value = $this->annotationExtractor->extractAnnotationFromClass($className, $annotation); - $this->assertSame($expected, $value); + yield 'Class without changelog annotation' => [RectorWithOutChangelog::class, '@changelog', null]; } } diff --git a/packages/ChangesReporting/Annotation/AnnotationExtractor.php b/packages/ChangesReporting/Annotation/AnnotationExtractor.php index f55a7b233aa3..686826669b4a 100644 --- a/packages/ChangesReporting/Annotation/AnnotationExtractor.php +++ b/packages/ChangesReporting/Annotation/AnnotationExtractor.php @@ -6,6 +6,9 @@ use Nette\Utils\Strings; use ReflectionClass; +/** + * @see \Rector\Tests\ChangesReporting\Annotation\AnnotationExtractorTest + */ final class AnnotationExtractor { /** @@ -16,18 +19,17 @@ public function extractAnnotationFromClass(string $className, string $annotation $reflectionClass = new ReflectionClass($className); $docComment = $reflectionClass->getDocComment(); - if (! is_string($docComment)) { return null; } - $pattern = '#' . preg_quote($annotation, '#') . '\s*(?[a-zA-Z0-9, ()_].*)#'; + // @see https://regex101.com/r/oYGaWU/1 + $pattern = '#' . preg_quote($annotation, '#') . '\s+(?.*?)$#m'; $matches = Strings::match($docComment, $pattern); - - if (! array_key_exists('annotation', $matches)) { + if ($matches === false) { return null; } - return trim((string) $matches['annotation']); + return $matches['content'] ?? null; } }