‪TYPO3CMS  10.4
InterfaceMethodChangedMatcher.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\MethodCall;
22 use PhpParser\Node\Stmt\Class_;
23 use PhpParser\Node\Stmt\ClassMethod;
24 
35 {
41  public function ‪__construct(array ‪$matcherDefinitions)
42  {
43  $this->matcherDefinitions = ‪$matcherDefinitions;
44  // newNumberOfArguments must exist in all matcherDefinitions
45  $this->‪validateMatcherDefinitions(['newNumberOfArguments']);
46  }
47 
56  public function ‪enterNode(Node $node)
57  {
58  if ($this->‪isFileIgnored($node) || $this->‪isLineIgnored($node)) {
59  return;
60  }
61 
62  // Match method name of a class, must be public, wouldn't make sense as interface if protected/private
63  if ($node instanceof ClassMethod
64  && array_key_exists($node->name->name, $this->matcherDefinitions)
65  && $node->flags & Class_::MODIFIER_PUBLIC // public
66  && ($node->flags & Class_::MODIFIER_STATIC) !== Class_::MODIFIER_STATIC // not static
67  ) {
68  $methodName = $node->name->name;
69  $numberOfUsedArguments = 0;
70  if (isset($node->params) && is_array($node->params)) {
71  $numberOfUsedArguments = count($node->params);
72  }
73  $numberOfAllowedArguments = $this->matcherDefinitions[$methodName]['newNumberOfArguments'];
74  if ($numberOfUsedArguments > $numberOfAllowedArguments) {
75  $this->matches[] = [
76  'restFiles' => $this->matcherDefinitions[$methodName]['restFiles'],
77  'line' => $node->getAttribute('startLine'),
78  'message' => 'Implementation of dropped interface argument for method "' . $methodName . '()"',
79  'indicator' => 'weak',
80  ];
81  }
82  }
83 
84  // Match method call (not static) with number of arguments
85  if ($node instanceof MethodCall
86  && array_key_exists($node->name->name, $this->matcherDefinitions)
87  ) {
88  $methodName = $node->name->name;
89  $numberOfUsedArguments = 0;
90  if (isset($node->args) && is_array($node->args)) {
91  $numberOfUsedArguments = count($node->args);
92  }
93  // @todo: Test for argument unpacking
94  $numberOfAllowedArguments = $this->matcherDefinitions[$methodName]['newNumberOfArguments'];
95  if ($numberOfUsedArguments > $numberOfAllowedArguments) {
96  $this->matches[] = [
97  'restFiles' => $this->matcherDefinitions[$methodName]['restFiles'],
98  'line' => $node->getAttribute('startLine'),
99  'message' => 'Call to interface method "' . $methodName . '()"',
100  'indicator' => 'weak',
101  ];
102  }
103  }
104  }
105 }
‪TYPO3\CMS\Install\ExtensionScanner\Php\Matcher\InterfaceMethodChangedMatcher\__construct
‪__construct(array $matcherDefinitions)
Definition: InterfaceMethodChangedMatcher.php:41
‪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\AbstractCoreMatcher\validateMatcherDefinitions
‪validateMatcherDefinitions(array $requiredArrayKeys=[])
Definition: AbstractCoreMatcher.php:88
‪TYPO3\CMS\Install\ExtensionScanner\Php\Matcher\InterfaceMethodChangedMatcher\enterNode
‪void null enterNode(Node $node)
Definition: InterfaceMethodChangedMatcher.php:56
‪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\InterfaceMethodChangedMatcher
Definition: InterfaceMethodChangedMatcher.php:35