2021-04-10 14:54:24 +00:00
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Rector\ChangesReporting\Annotation;
|
|
|
|
|
2021-04-10 15:23:00 +00:00
|
|
|
use Nette\Utils\Strings;
|
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
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @param class-string<object> $className
|
|
|
|
*/
|
|
|
|
public function extractAnnotationFromClass(string $className, string $annotation): ?string
|
|
|
|
{
|
2021-04-10 15:23:00 +00:00
|
|
|
$reflectionClass = new ReflectionClass($className);
|
2021-04-10 14:54:24 +00:00
|
|
|
|
2021-04-10 15:23:00 +00:00
|
|
|
$docComment = $reflectionClass->getDocComment();
|
2021-04-10 14:54:24 +00:00
|
|
|
if (! is_string($docComment)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-04-10 15:29:36 +00:00
|
|
|
// @see https://regex101.com/r/oYGaWU/1
|
|
|
|
$pattern = '#' . preg_quote($annotation, '#') . '\s+(?<content>.*?)$#m';
|
2021-04-10 15:23:00 +00:00
|
|
|
$matches = Strings::match($docComment, $pattern);
|
2021-04-10 14:54:24 +00:00
|
|
|
|
2021-04-10 15:29:36 +00:00
|
|
|
return $matches['content'] ?? null;
|
2021-04-10 14:54:24 +00:00
|
|
|
}
|
|
|
|
}
|