‪TYPO3CMS  10.4
MethodArgumentRequiredStaticMatcher.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\Node;
21 use PhpParser\Node\Expr\StaticCall;
22 use PhpParser\Node\Expr\Variable;
23 use PhpParser\Node\Name\FullyQualified;
24 
31 {
37  public function ‪__construct(array ‪$matcherDefinitions)
38  {
39  $this->matcherDefinitions = ‪$matcherDefinitions;
40  $this->‪validateMatcherDefinitions(['numberOfMandatoryArguments', 'maximumNumberOfArguments']);
42  }
43 
50  public function ‪enterNode(Node $node)
51  {
52  // Match static method call
53  if (!$this->‪isFileIgnored($node)
54  && !$this->‪isLineIgnored($node)
55  && $node instanceof StaticCall
56  ) {
57  $isArgumentUnpackingUsed = $this->‪isArgumentUnpackingUsed($node->args);
58 
59  if ($node->class instanceof FullyQualified) {
60  // 'Foo\Bar::aMethod()' -> strong match
61  $fqdnClassWithMethod = $node->class->toString() . '::' . $node->name->name;
62  $numberOfArguments = count($node->args);
63  if (!$isArgumentUnpackingUsed
64  && array_key_exists($fqdnClassWithMethod, $this->matcherDefinitions)
65  && $numberOfArguments < $this->matcherDefinitions[$fqdnClassWithMethod]['numberOfMandatoryArguments']
66  // maximum number of arguments is just a measure against false positives
67  && $numberOfArguments <= $this->matcherDefinitions[$fqdnClassWithMethod]['maximumNumberOfArguments']
68  ) {
69  $this->matches[] = [
70  'restFiles' => $this->matcherDefinitions[$fqdnClassWithMethod]['restFiles'],
71  'line' => $node->getAttribute('startLine'),
72  'message' => 'Method "' . $node->name->name . '()" needs at least '
73  . $this->matcherDefinitions[$fqdnClassWithMethod]['numberOfMandatoryArguments']
74  . ' arguments.',
75  'indicator' => 'strong',
76  ];
77  }
78  } elseif ($node->class instanceof Variable
79  && array_key_exists($node->name->name, $this->flatMatcherDefinitions)
80  ) {
81  $match = [
82  'restFiles' => [],
83  'line' => $node->getAttribute('startLine'),
84  'indicator' => 'weak',
85  ];
86 
87  $numberOfArguments = count($node->args);
88  $isPossibleMatch = false;
89  foreach ($this->flatMatcherDefinitions[$node->name->name]['candidates'] as $candidate) {
90  // A method call is considered a match if it is not called with argument unpacking
91  // and number of used arguments is lesser than numberOfMandatoryArguments
92  if (!$isArgumentUnpackingUsed
93  && $numberOfArguments < $candidate['numberOfMandatoryArguments']
94  // maximum number of arguments is just a measure against false positives
95  && $numberOfArguments <= $candidate['maximumNumberOfArguments']
96  ) {
97  $isPossibleMatch = true;
98  $match['message'] = 'Method "' . $node->name->name . '()" needs at least '
99  . $candidate['numberOfMandatoryArguments'] . ' arguments.';
100  $match['restFiles'] = array_unique(array_merge($match['restFiles'], $candidate['restFiles']));
101  }
102  }
103  if ($isPossibleMatch) {
104  $this->matches[] = $match;
105  }
106  }
107  }
108  }
109 }
‪TYPO3\CMS\Install\ExtensionScanner\Php\Matcher\MethodArgumentRequiredStaticMatcher\__construct
‪__construct(array $matcherDefinitions)
Definition: MethodArgumentRequiredStaticMatcher.php:37
‪TYPO3\CMS\Install\ExtensionScanner\Php\Matcher\AbstractCoreMatcher\isArgumentUnpackingUsed
‪bool isArgumentUnpackingUsed(array $arguments=[])
Definition: AbstractCoreMatcher.php:164
‪TYPO3\CMS\Install\ExtensionScanner\Php\Matcher
Definition: AbstractCoreMatcher.php:18
‪TYPO3\CMS\Install\ExtensionScanner\Php\Matcher\AbstractCoreMatcher\isLineIgnored
‪bool isLineIgnored(Node $node)
Definition: AbstractCoreMatcher.php:181
‪TYPO3\CMS\Install\ExtensionScanner\Php\Matcher\AbstractCoreMatcher
Definition: AbstractCoreMatcher.php:34
‪TYPO3\CMS\Install\ExtensionScanner\Php\Matcher\AbstractCoreMatcher\validateMatcherDefinitions
‪validateMatcherDefinitions(array $requiredArrayKeys=[])
Definition: AbstractCoreMatcher.php:88
‪TYPO3\CMS\Install\ExtensionScanner\Php\Matcher\AbstractCoreMatcher\isFileIgnored
‪bool isFileIgnored(Node $node)
Definition: AbstractCoreMatcher.php:214
‪TYPO3\CMS\Install\ExtensionScanner\Php\Matcher\AbstractCoreMatcher\$matcherDefinitions
‪array $matcherDefinitions
Definition: AbstractCoreMatcher.php:41
‪TYPO3\CMS\Install\ExtensionScanner\Php\Matcher\MethodArgumentRequiredStaticMatcher
Definition: MethodArgumentRequiredStaticMatcher.php:31
‪TYPO3\CMS\Install\ExtensionScanner\Php\Matcher\MethodArgumentRequiredStaticMatcher\enterNode
‪enterNode(Node $node)
Definition: MethodArgumentRequiredStaticMatcher.php:50
‪TYPO3\CMS\Install\ExtensionScanner\Php\Matcher\AbstractCoreMatcher\initializeFlatMatcherDefinitions
‪initializeFlatMatcherDefinitions()
Definition: AbstractCoreMatcher.php:134