‪TYPO3CMS  10.4
MethodCallStaticMatcher.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 
38 {
44  public function ‪__construct(array ‪$matcherDefinitions)
45  {
46  $this->matcherDefinitions = ‪$matcherDefinitions;
47  $this->‪validateMatcherDefinitions(['numberOfMandatoryArguments', 'maximumNumberOfArguments']);
49  }
50 
58  public function ‪enterNode(Node $node)
59  {
60  // Static call, not method call
61  if (!$this->‪isFileIgnored($node)
62  && !$this->‪isLineIgnored($node)
63  && $node instanceof StaticCall
64  ) {
65  if ($node->class instanceof FullyQualified) {
66  // 'Foo\Bar::deprecated()' -> strong match
67  $fqdnClassWithMethod = $node->class->toString() . '::' . $node->name->name;
68  if (array_key_exists($fqdnClassWithMethod, $this->matcherDefinitions)) {
69  $this->matches[] = [
70  'restFiles' => $this->matcherDefinitions[$fqdnClassWithMethod]['restFiles'],
71  'line' => $node->getAttribute('startLine'),
72  'message' => 'Use of static class method call "' . $fqdnClassWithMethod . '()"',
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  'message' => 'Use of static class method call "' . $node->name->name . '()"',
83  'indicator' => 'weak',
84  ];
85 
86  $numberOfArguments = count($node->args);
87  $isArgumentUnpackingUsed = $this->‪isArgumentUnpackingUsed($node->args);
88 
89  $isPossibleMatch = false;
90  foreach ($this->flatMatcherDefinitions[$node->name->name]['candidates'] as $candidate) {
91  // A method call is considered a match if it is called with argument unpacking, or
92  // if the number of given arguments is within range of mandatory / max number of arguments
93  if ($isArgumentUnpackingUsed
94  || ($numberOfArguments >= $candidate['numberOfMandatoryArguments']
95  && $numberOfArguments <= $candidate['maximumNumberOfArguments'])
96  ) {
97  $isPossibleMatch = true;
98  $match['restFiles'] = array_unique(array_merge($match['restFiles'], $candidate['restFiles']));
99  }
100  }
101  if ($isPossibleMatch) {
102  $this->matches[] = $match;
103  }
104  }
105  }
106  }
107 }
‪TYPO3\CMS\Install\ExtensionScanner\Php\Matcher\MethodCallStaticMatcher
Definition: MethodCallStaticMatcher.php:38
‪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\MethodCallStaticMatcher\enterNode
‪enterNode(Node $node)
Definition: MethodCallStaticMatcher.php:58
‪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\MethodCallStaticMatcher\__construct
‪__construct(array $matcherDefinitions)
Definition: MethodCallStaticMatcher.php:44
‪TYPO3\CMS\Install\ExtensionScanner\Php\Matcher\AbstractCoreMatcher\initializeFlatMatcherDefinitions
‪initializeFlatMatcherDefinitions()
Definition: AbstractCoreMatcher.php:134