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