‪TYPO3CMS  ‪main
PropertyAnnotationMatcher.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use PhpParser\Comment\Doc;
21 use PhpParser\Node;
22 use PhpParser\Node\PropertyItem;
23 use PhpParser\Node\Stmt\Property;
24 
30 {
36  public function ‪__construct(array ‪$matcherDefinitions)
37  {
38  $this->matcherDefinitions = ‪$matcherDefinitions;
40  }
41 
46  public function ‪enterNode(Node $node): null
47  {
48  if ($node instanceof Property
49  && ($property = reset($node->props)) instanceof PropertyItem
50  && ($docComment = $node->getDocComment()) instanceof Doc
51  && !$this->isFileIgnored($node)
52  && !$this->isLineIgnored($node)
53  ) {
55  $isPossibleMatch = false;
56  $match = [
57  'restFiles' => [],
58  'line' => $property->getAttribute('startLine'),
59  'indicator' => 'strong',
60  ];
61 
62  ‪$matches = [];
63  preg_match_all(
64  '/\s*\s@(?<annotations>[^\s.]*).*\n/',
65  $docComment->getText(),
67  );
68 
69  foreach (‪$matches['annotations'] as $annotation) {
70  $annotation = '@' . $annotation;
71 
72  if (!isset($this->matcherDefinitions[$annotation])) {
73  continue;
74  }
75 
76  $isPossibleMatch = true;
77  $match['message'] = 'Property "' . $property->name . '" uses an ' . $annotation . ' annotation.';
78  $match['restFiles'] = array_unique(array_merge(
79  $match['restFiles'],
80  $this->matcherDefinitions[$annotation]['restFiles']
81  ));
82  }
83 
84  if ($isPossibleMatch) {
85  $this->matches[] = $match;
86  }
87  }
88  return null;
89  }
90 }
‪TYPO3\CMS\Install\ExtensionScanner\Php\Matcher
Definition: AbstractCoreMatcher.php:18
‪TYPO3\CMS\Install\ExtensionScanner\Php\Matcher\AbstractCoreMatcher
Definition: AbstractCoreMatcher.php:34
‪TYPO3\CMS\Install\ExtensionScanner\Php\Matcher\PropertyAnnotationMatcher\enterNode
‪enterNode(Node $node)
Definition: PropertyAnnotationMatcher.php:46
‪TYPO3\CMS\Install\ExtensionScanner\Php\Matcher\AbstractCoreMatcher\$matches
‪array $matches
Definition: AbstractCoreMatcher.php:45
‪TYPO3\CMS\Install\ExtensionScanner\Php\Matcher\PropertyAnnotationMatcher
Definition: PropertyAnnotationMatcher.php:30
‪TYPO3\CMS\Install\ExtensionScanner\Php\Matcher\AbstractCoreMatcher\validateMatcherDefinitions
‪validateMatcherDefinitions(array $requiredArrayKeys=[])
Definition: AbstractCoreMatcher.php:86
‪TYPO3\CMS\Install\ExtensionScanner\Php\Matcher\AbstractCoreMatcher\$matcherDefinitions
‪array $matcherDefinitions
Definition: AbstractCoreMatcher.php:41
‪TYPO3\CMS\Install\ExtensionScanner\Php\Matcher\PropertyAnnotationMatcher\__construct
‪__construct(array $matcherDefinitions)
Definition: PropertyAnnotationMatcher.php:36