Skip to content

Commit

Permalink
fix string match return type
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Apr 10, 2021
1 parent 9517556 commit 00a3e43
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
}
12 changes: 7 additions & 5 deletions packages/ChangesReporting/Annotation/AnnotationExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
use Nette\Utils\Strings;
use ReflectionClass;

/**
* @see \Rector\Tests\ChangesReporting\Annotation\AnnotationExtractorTest
*/
final class AnnotationExtractor
{
/**
Expand All @@ -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*(?<annotation>[a-zA-Z0-9, ()_].*)#';
// @see https://regex101.com/r/oYGaWU/1
$pattern = '#' . preg_quote($annotation, '#') . '\s+(?<content>.*?)$#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;
}
}

0 comments on commit 00a3e43

Please sign in to comment.