‪TYPO3CMS  ‪main
MethodAnnotationMatcher.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\Stmt\ClassMethod;
23 
29 {
35  public function ‪__construct(array ‪$matcherDefinitions)
36  {
37  $this->matcherDefinitions = ‪$matcherDefinitions;
39  }
40 
45  public function ‪enterNode(Node $node): null
46  {
47  if ($node instanceof ClassMethod
48  && ($docComment = $node->getDocComment()) instanceof Doc
49  && !$this->isFileIgnored($node)
50  && !$this->isLineIgnored($node)
51  ) {
52  $isPossibleMatch = false;
53  $match = [
54  'restFiles' => [],
55  'line' => $node->getAttribute('startLine'),
56  'indicator' => 'strong',
57  ];
58 
59  ‪$matches = [];
60  preg_match_all(
61  '/\s*\s@(?<annotations>[^\s.]*).*\n/',
62  $docComment->getText(),
64  );
65 
66  foreach (‪$matches['annotations'] as $annotation) {
67  $annotation = '@' . $annotation;
68 
69  if (!isset($this->matcherDefinitions[$annotation])) {
70  continue;
71  }
72 
73  $isPossibleMatch = true;
74  $match['message'] = 'Method "' . $node->name . '" uses an ' . $annotation . ' annotation.';
75  $match['restFiles'] = array_unique(array_merge(
76  $match['restFiles'],
77  $this->matcherDefinitions[$annotation]['restFiles']
78  ));
79  }
80 
81  if ($isPossibleMatch) {
82  $this->matches[] = $match;
83  }
84  }
85  return null;
86  }
87 }
‪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\MethodAnnotationMatcher
Definition: MethodAnnotationMatcher.php:29
‪TYPO3\CMS\Install\ExtensionScanner\Php\Matcher\AbstractCoreMatcher\$matches
‪array $matches
Definition: AbstractCoreMatcher.php:45
‪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\MethodAnnotationMatcher\enterNode
‪enterNode(Node $node)
Definition: MethodAnnotationMatcher.php:45
‪TYPO3\CMS\Install\ExtensionScanner\Php\Matcher\MethodAnnotationMatcher\__construct
‪__construct(array $matcherDefinitions)
Definition: MethodAnnotationMatcher.php:35