Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
kukulich committed Apr 21, 2023
1 parent 34e1375 commit 9cb94ab
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 44 deletions.
61 changes: 22 additions & 39 deletions SlevomatCodingStandard/Helpers/AnnotationHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,65 +66,34 @@
class AnnotationHelper
{

public const PREFIXES = ['psalm', 'phpstan'];
public const STATIC_ANALYSIS_PREFIXES = ['psalm', 'phpstan'];

private const MAPPING = [
'@param' => ParameterAnnotation::class,
'@psalm-param' => ParameterAnnotation::class,
'@phpstan-param' => ParameterAnnotation::class,
'@return' => ReturnAnnotation::class,
'@psalm-return' => ReturnAnnotation::class,
'@phpstan-return' => ReturnAnnotation::class,
'@var' => VariableAnnotation::class,
'@psalm-var' => VariableAnnotation::class,
'@phpstan-var' => VariableAnnotation::class,
'@throws' => ThrowsAnnotation::class,
'@phpstan-throws' => ThrowsAnnotation::class,
'@property' => PropertyAnnotation::class,
'@psalm-property' => PropertyAnnotation::class,
'@phpstan-property' => PropertyAnnotation::class,
'@property-read' => PropertyAnnotation::class,
'@psalm-property-read' => PropertyAnnotation::class,
'@phpstan-property-read' => PropertyAnnotation::class,
'@property-write' => PropertyAnnotation::class,
'@psalm-property-write' => PropertyAnnotation::class,
'@phpstan-property-write' => PropertyAnnotation::class,
'@method' => MethodAnnotation::class,
'@psalm-method' => MethodAnnotation::class,
'@phpstan-method' => MethodAnnotation::class,
'@template' => TemplateAnnotation::class,
'@psalm-template' => TemplateAnnotation::class,
'@phpstan-template' => TemplateAnnotation::class,
'@template-covariant' => TemplateAnnotation::class,
'@psalm-template-covariant' => TemplateAnnotation::class,
'@phpstan-template-covariant' => TemplateAnnotation::class,
'@extends' => ExtendsAnnotation::class,
'@template-extends' => ExtendsAnnotation::class,
'@phpstan-extends' => ExtendsAnnotation::class,
'@implements' => ImplementsAnnotation::class,
'@template-implements' => ImplementsAnnotation::class,
'@phpstan-implements' => ImplementsAnnotation::class,
'@use' => UseAnnotation::class,
'@template-use' => UseAnnotation::class,
'@phpstan-use' => UseAnnotation::class,
'@psalm-type' => TypeAliasAnnotation::class,
'@phpstan-type' => TypeAliasAnnotation::class,
'@psalm-import-type' => TypeImportAnnotation::class,
'@phpstan-import-type' => TypeImportAnnotation::class,
'@type' => TypeAliasAnnotation::class,
'@import-type' => TypeImportAnnotation::class,
'@mixin' => MixinAnnotation::class,
'@phpstan-assert' => AssertAnnotation::class,
'@phpstan-assert-if-true' => AssertAnnotation::class,
'@phpstan-assert-if-false' => AssertAnnotation::class,
'@psalm-assert' => AssertAnnotation::class,
'@psalm-assert-if-true' => AssertAnnotation::class,
'@psalm-assert-if-false' => AssertAnnotation::class,
'@assert' => AssertAnnotation::class,
'@assert-if-true' => AssertAnnotation::class,
'@assert-if-false' => AssertAnnotation::class,
'@param-out' => ParameterOutAnnotation::class,
'@psalm-param-out' => ParameterOutAnnotation::class,
'@phpstan-param-out' => ParameterOutAnnotation::class,
'@psalm-self-out' => SelfOutAnnotation::class,
'@phpstan-self-out' => SelfOutAnnotation::class,
'@psalm-this-out' => SelfOutAnnotation::class,
'@phpstan-this-out' => SelfOutAnnotation::class,
'@self-out' => SelfOutAnnotation::class,
'@this-out' => SelfOutAnnotation::class,
];

/**
Expand Down Expand Up @@ -366,9 +335,23 @@ static function () use ($phpcsFile, $pointer): array {
}
}

$className = null;
if (array_key_exists($annotationName, self::MAPPING)) {
$className = self::MAPPING[$annotationName];
} else {
foreach (self::STATIC_ANALYSIS_PREFIXES as $prefix) {
$annotationNameWithoutPrefix = preg_replace('~^@' . $prefix . '-~', '@', $annotationName);

if (!array_key_exists($annotationNameWithoutPrefix, self::MAPPING)) {
continue;
}

$className = self::MAPPING[$annotationNameWithoutPrefix];
break;
}
}

if ($className !== null) {
$parsedContent = null;
if ($annotationContent !== null) {
$parsedContent = self::parseAnnotationContent($annotationName, $annotationContent);
Expand Down
4 changes: 2 additions & 2 deletions SlevomatCodingStandard/Helpers/FunctionHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ public static function getValidPrefixedParametersAnnotations(File $phpcsFile, in
$tokens = $phpcsFile->getTokens();

$parametersAnnotations = [];
foreach (AnnotationHelper::PREFIXES as $prefix) {
foreach (AnnotationHelper::STATIC_ANALYSIS_PREFIXES as $prefix) {
if (self::getName($phpcsFile, $functionPointer) === '__construct') {
for ($i = $tokens[$functionPointer]['parenthesis_opener'] + 1; $i < $tokens[$functionPointer]['parenthesis_closer']; $i++) {
if ($tokens[$i]['code'] !== T_VARIABLE) {
Expand Down Expand Up @@ -435,7 +435,7 @@ public static function getValidPrefixedReturnAnnotations(File $phpcsFile, int $f
{
$returnAnnotations = [];

foreach (AnnotationHelper::PREFIXES as $prefix) {
foreach (AnnotationHelper::STATIC_ANALYSIS_PREFIXES as $prefix) {
/** @var ReturnAnnotation[] $annotations */
$annotations = AnnotationHelper::getAnnotationsByName($phpcsFile, $functionPointer, sprintf('@%s-return', $prefix));
foreach ($annotations as $annotation) {
Expand Down
4 changes: 2 additions & 2 deletions SlevomatCodingStandard/Helpers/TypeHintHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ private static function isTemplate(File $phpcsFile, int $docCommentOpenPointer,
if ($templateAnnotationNames === null) {
foreach (['template', 'template-covariant'] as $annotationName) {
$templateAnnotationNames[] = sprintf('@%s', $annotationName);
foreach (AnnotationHelper::PREFIXES as $prefixAnnotationName) {
foreach (AnnotationHelper::STATIC_ANALYSIS_PREFIXES as $prefixAnnotationName) {
$templateAnnotationNames[] = sprintf('@%s-%s', $prefixAnnotationName, $annotationName);
}
}
Expand Down Expand Up @@ -323,7 +323,7 @@ private static function isAlias(File $phpcsFile, int $docCommentOpenPointer, str
static $aliasAnnotationNames = null;
if ($aliasAnnotationNames === null) {
foreach (['type', 'import-type'] as $annotationName) {
foreach (AnnotationHelper::PREFIXES as $prefixAnnotationName) {
foreach (AnnotationHelper::STATIC_ANALYSIS_PREFIXES as $prefixAnnotationName) {
$aliasAnnotationNames[] = sprintf('@%s-%s', $prefixAnnotationName, $annotationName);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,7 @@ private function getValidPrefixedAnnotations(File $phpcsFile, int $propertyPoint
{
$returnAnnotations = [];

foreach (AnnotationHelper::PREFIXES as $prefix) {
foreach (AnnotationHelper::STATIC_ANALYSIS_PREFIXES as $prefix) {
/** @var VariableAnnotation[] $annotations */
$annotations = AnnotationHelper::getAnnotationsByName($phpcsFile, $propertyPointer, sprintf('@%s-var', $prefix));
foreach ($annotations as $annotation) {
Expand Down

0 comments on commit 9cb94ab

Please sign in to comment.