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