‪TYPO3CMS  ‪main
MethodArgumentUnusedMatcher.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\ConstFetch;
22 use PhpParser\Node\Expr\MethodCall;
23 
33 {
39  public function ‪__construct(array ‪$matcherDefinitions)
40  {
41  $this->matcherDefinitions = ‪$matcherDefinitions;
42  $this->‪validateMatcherDefinitions(['unusedArgumentNumbers']);
44  }
45 
49  public function ‪enterNode(Node $node): null
50  {
51  // Match method call (not static)
52  if (!$this->‪isFileIgnored($node)
53  && !$this->‪isLineIgnored($node)
54  && $node instanceof MethodCall
55  && array_key_exists($node->name->name, $this->flatMatcherDefinitions)
56  ) {
57  $match = [
58  'restFiles' => [],
59  'line' => $node->getAttribute('startLine'),
60  'indicator' => 'weak',
61  ];
62 
63  $isArgumentUnpackingUsed = $this->‪isArgumentUnpackingUsed($node->args);
64 
65  $numberOfArguments = count($node->args);
66  $isPossibleMatch = false;
67  foreach ($this->flatMatcherDefinitions[$node->name->name]['candidates'] as $candidate) {
68  foreach ($candidate['unusedArgumentNumbers'] as $droppedArgumentNumber) {
69  // A method call is considered a match if name matches, unpacking is not used
70  // and the registered argument is not given as null.
71  if (!$isArgumentUnpackingUsed
72  && $numberOfArguments >= $droppedArgumentNumber
73  && !($node->args[$droppedArgumentNumber - 1]->value instanceof ConstFetch)
74  && (!isset($node->args[$droppedArgumentNumber - 1]->value->name->name->parts[0])
75  || $node->args[$droppedArgumentNumber - 1]->value->name->name->parts[0] !== null)
76  ) {
77  $isPossibleMatch = true;
78  $match['message'] = 'Call to method "' . $node->name->name . '()" with'
79  . ' argument ' . $droppedArgumentNumber . ' not given as null.';
80  $match['restFiles'] = array_unique(array_merge($match['restFiles'], $candidate['restFiles']));
81  }
82  }
83  }
84  if ($isPossibleMatch) {
85  $this->matches[] = $match;
86  }
87  }
88  return null;
89  }
90 }
‪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\MethodArgumentUnusedMatcher\enterNode
‪enterNode(Node $node)
Definition: MethodArgumentUnusedMatcher.php:49
‪TYPO3\CMS\Install\ExtensionScanner\Php\Matcher
Definition: AbstractCoreMatcher.php:18
‪TYPO3\CMS\Install\ExtensionScanner\Php\Matcher\MethodArgumentUnusedMatcher\__construct
‪__construct(array $matcherDefinitions)
Definition: MethodArgumentUnusedMatcher.php:39
‪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:205
‪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\MethodArgumentUnusedMatcher
Definition: MethodArgumentUnusedMatcher.php:33
‪TYPO3\CMS\Install\ExtensionScanner\Php\Matcher\AbstractCoreMatcher\initializeFlatMatcherDefinitions
‪initializeFlatMatcherDefinitions()
Definition: AbstractCoreMatcher.php:132