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