2021-04-10 14:54:24 +00:00
|
|
|
<?php
|
2021-04-10 16:22:25 +00:00
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\ChangesReporting\Annotation;
|
2021-04-10 14:54:24 +00:00
|
|
|
|
2023-02-02 09:24:12 +00:00
|
|
|
use RectorPrefix202302\Nette\Utils\Strings;
|
2022-06-06 17:12:56 +00:00
|
|
|
use Rector\Core\Contract\Rector\RectorInterface;
|
2021-04-10 14:54:24 +00:00
|
|
|
use ReflectionClass;
|
2021-04-10 15:29:36 +00:00
|
|
|
/**
|
|
|
|
* @see \Rector\Tests\ChangesReporting\Annotation\AnnotationExtractorTest
|
|
|
|
*/
|
2021-04-10 14:54:24 +00:00
|
|
|
final class AnnotationExtractor
|
|
|
|
{
|
|
|
|
/**
|
2021-04-10 16:22:25 +00:00
|
|
|
* @param class-string<RectorInterface> $className
|
2021-04-10 14:54:24 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function extractAnnotationFromClass(string $className, string $annotation) : ?string
|
2021-04-10 14:54:24 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
$reflectionClass = new ReflectionClass($className);
|
2021-04-10 15:23:00 +00:00
|
|
|
$docComment = $reflectionClass->getDocComment();
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!\is_string($docComment)) {
|
2021-04-10 14:54:24 +00:00
|
|
|
return null;
|
|
|
|
}
|
2021-05-30 10:12:56 +00:00
|
|
|
// @see https://3v4l.org/ouYfB
|
2022-06-07 09:46:15 +00:00
|
|
|
// uses 'r?\n' instead of '$' because windows compat
|
2021-05-30 10:12:56 +00:00
|
|
|
$pattern = '#' . \preg_quote($annotation, '#') . '\\s+(?<content>.*?)\\r?\\n#m';
|
2022-06-07 08:22:29 +00:00
|
|
|
$matches = Strings::match($docComment, $pattern);
|
2021-04-10 15:29:36 +00:00
|
|
|
return $matches['content'] ?? null;
|
2021-04-10 14:54:24 +00:00
|
|
|
}
|
|
|
|
}
|