rector/packages/ChangesReporting/Annotation/AnnotationExtractor.php

34 lines
897 B
PHP
Raw Normal View History

<?php
declare(strict_types=1);
namespace Rector\ChangesReporting\Annotation;
use Nette\Utils\Strings;
use Rector\Core\Contract\Rector\RectorInterface;
use ReflectionClass;
2021-04-10 15:29:36 +00:00
/**
* @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;
}
2021-04-10 15:29:36 +00:00
// @see https://regex101.com/r/oYGaWU/1
$pattern = '#' . preg_quote($annotation, '#') . '\s+(?<content>.*?)$#m';
$matches = Strings::match($docComment, $pattern);
2021-04-10 15:29:36 +00:00
return $matches['content'] ?? null;
}
}