rector/packages/ChangesReporting/Annotation/AnnotationExtractor.php
Tomas Votruba 3395b050ee Updated Rector to commit 028dcb15e4ce3af9a1651b25a9d945ba962907f1
028dcb15e4 [CodingStyle] Skip RemoveUnusedAliasRector on alias used as namespace (#634)
2021-08-10 07:12:30 +00:00

31 lines
1011 B
PHP

<?php
declare (strict_types=1);
namespace Rector\ChangesReporting\Annotation;
use RectorPrefix20210810\Nette\Utils\Strings;
use Rector\Core\Contract\Rector\RectorInterface;
use ReflectionClass;
/**
* @see \Rector\Tests\ChangesReporting\Annotation\AnnotationExtractorTest
*/
final class AnnotationExtractor
{
/**
* @param class-string<RectorInterface> $className
*/
public function extractAnnotationFromClass(string $className, string $annotation) : ?string
{
$reflectionClass = new \ReflectionClass($className);
$docComment = $reflectionClass->getDocComment();
if (!\is_string($docComment)) {
return null;
}
// @see https://3v4l.org/ouYfB
// uses 'r?\n' instead of '$' because windows compat
$pattern = '#' . \preg_quote($annotation, '#') . '\\s+(?<content>.*?)\\r?\\n#m';
$matches = \RectorPrefix20210810\Nette\Utils\Strings::match($docComment, $pattern);
return $matches['content'] ?? null;
}
}