‪TYPO3CMS  10.4
MethodArgumentDroppedStaticMatcher.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(['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  if (!$isArgumentUnpackingUsed
63  && array_key_exists($fqdnClassWithMethod, $this->matcherDefinitions)
64  && count($node->args) > $this->matcherDefinitions[$fqdnClassWithMethod]['maximumNumberOfArguments']
65  ) {
66  $this->matches[] = [
67  'restFiles' => $this->matcherDefinitions[$fqdnClassWithMethod]['restFiles'],
68  'line' => $node->getAttribute('startLine'),
69  'message' => 'Method "' . $node->name->name . '()" supports only '
70  . $this->matcherDefinitions[$fqdnClassWithMethod]['maximumNumberOfArguments']
71  . ' arguments.',
72  'indicator' => 'strong',
73  ];
74  }
75  } elseif ($node->class instanceof Variable
76  && array_key_exists($node->name->name, $this->flatMatcherDefinitions)
77  ) {
78  $match = [
79  'restFiles' => [],
80  'line' => $node->getAttribute('startLine'),
81  'indicator' => 'weak',
82  ];
83 
84  $numberOfArguments = count($node->args);
85  $isPossibleMatch = false;
86  foreach ($this->flatMatcherDefinitions[$node->name->name]['candidates'] as $candidate) {
87  // A method call is considered a match if it is not called with argument unpacking
88  // and number of used arguments is higher than maximumNumberOfArguments
89  if (!$isArgumentUnpackingUsed
90  && $numberOfArguments > $candidate['maximumNumberOfArguments']
91  ) {
92  $isPossibleMatch = true;
93  $match['message'] = 'Method "' . $node->name->name . '()" supports only '
94  . $candidate['maximumNumberOfArguments'] . ' arguments.';
95  $match['restFiles'] = array_unique(array_merge($match['restFiles'], $candidate['restFiles']));
96  }
97  }
98  if ($isPossibleMatch) {
99  $this->matches[] = $match;
100  }
101  }
102  }
103  }
104 }
‪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\MethodArgumentDroppedStaticMatcher\enterNode
‪enterNode(Node $node)
Definition: MethodArgumentDroppedStaticMatcher.php:50
‪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\MethodArgumentDroppedStaticMatcher
Definition: MethodArgumentDroppedStaticMatcher.php:31
‪TYPO3\CMS\Install\ExtensionScanner\Php\Matcher\MethodArgumentDroppedStaticMatcher\__construct
‪__construct(array $matcherDefinitions)
Definition: MethodArgumentDroppedStaticMatcher.php:37
‪TYPO3\CMS\Install\ExtensionScanner\Php\Matcher\AbstractCoreMatcher\initializeFlatMatcherDefinitions
‪initializeFlatMatcherDefinitions()
Definition: AbstractCoreMatcher.php:134