‪TYPO3CMS  10.4
MethodArgumentUnusedMatcherTest.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\NodeTraverser;
21 use PhpParser\NodeVisitor\NameResolver;
22 use PhpParser\ParserFactory;
24 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
25 
29 class ‪MethodArgumentUnusedMatcherTest extends UnitTestCase
30 {
34  public function ‪hitsFromFixtureAreFound()
35  {
36  ‪$parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7);
37  $fixtureFile = __DIR__ . '/Fixtures/MethodArgumentUnusedMatcherFixture.php';
38  $statements = ‪$parser->parse(file_get_contents($fixtureFile));
39 
40  $traverser = new NodeTraverser();
41  $traverser->addVisitor(new NameResolver());
42 
43  $configuration = [
44  'TYPO3\CMS\Core\Html\RteHtmlParser->RTE_transform' => [
45  'unusedArgumentNumbers' => [ 2 ],
46  'restFiles' => [
47  'Breaking-80700-DeprecatedFunctionalityRemoved.rst',
48  'Deprecation-79341-MethodsRelatedToRichtextConfiguration.rst',
49  ],
50  ],
51  ];
52  $subject = new ‪MethodArgumentUnusedMatcher($configuration);
53  $traverser->addVisitor($subject);
54  $traverser->traverse($statements);
55  $expectedHitLineNumbers = [
56  28,
57  29,
58  ];
59  $actualHitLineNumbers = [];
60  foreach ($subject->getMatches() as $hit) {
61  $actualHitLineNumbers[] = $hit['line'];
62  }
63  self::assertEquals($expectedHitLineNumbers, $actualHitLineNumbers);
64  }
65 
70  {
71  return [
72  'two rest candidates with same number of arguments' => [
73  [
74  'Foo->aMethod' => [
75  'unusedArgumentNumbers' => [ 1 ],
76  'restFiles' => [
77  'Foo-1.rst',
78  'Foo-2.rst',
79  ],
80  ],
81  'Bar->aMethod' => [
82  'unusedArgumentNumbers' => [ 1 ],
83  'restFiles' => [
84  'Bar-1.rst',
85  'Bar-2.rst',
86  ],
87  ],
88  ],
89  '<?php
90  $someVar->aMethod(\'foo\');',
91  [
92  0 => [
93  'restFiles' => [
94  'Foo-1.rst',
95  'Foo-2.rst',
96  'Bar-1.rst',
97  'Bar-2.rst',
98  ],
99  ],
100  ],
101  ],
102  'two candidates, multiple matches' => [
103  [
104  'Foo->aMethod' => [
105  'unusedArgumentNumbers' => [ 1, 2 ],
106  'restFiles' => [
107  'Foo-1.rst',
108  ],
109  ],
110  'Bar->aMethod' => [
111  'unusedArgumentNumbers' => [ 3 ],
112  'restFiles' => [
113  'Bar-1.rst',
114  ],
115  ],
116  ],
117  '<?php
118  $someVar->aMethod(\'arg1\', \'arg2\', \'arg3\');',
119  [
120  0 => [
121  'restFiles' => [
122  'Foo-1.rst',
123  'Bar-1.rst',
124  ],
125  ],
126  ],
127  ],
128  'one candidate, no hit, not enough arguments' => [
129  [
130  'Foo->aMethod' => [
131  'unusedArgumentNumbers' => [ 2, 3 ],
132  'restFiles' => [
133  'Foo-1.rst',
134  ],
135  ],
136  ],
137  '<?php
138  $someVar->aMethod(\'arg1\');',
139  [], // no hit
140  ],
141  'one candidate, no hit, given as null is ok' => [
142  [
143  'Foo->aMethod' => [
144  'unusedArgumentNumbers' => [ 2, 3 ],
145  'restFiles' => [
146  'Foo-1.rst',
147  ],
148  ],
149  ],
150  '<?php
151  $someVar->aMethod(\'arg1\', null, null);',
152  [], // no hit
153  ],
154  'one match, third argument still given not null' => [
155  [
156  'Foo->aMethod' => [
157  'unusedArgumentNumbers' => [ 2, 3 ],
158  'restFiles' => [
159  'aRest.rst',
160  ],
161  ],
162  ],
163  '<?php
164  $someVar->aMethod(\'arg1\', null, \'arg3\');',
165  [
166  0 => [
167  'restFiles' => [
168  'aRest.rst',
169  ],
170  ],
171  ],
172  ],
173  'no match, scanning ignored as soon as argument unpacking is used' => [
174  [
175  'Foo->aMethod' => [
176  'unusedArgumentNumbers' => [ 1, 3 ],
177  'restFiles' => [
178  'Foo-1.rst',
179  ],
180  ],
181  ],
182  '<?php
183  $args1 = [\'arg1\', \'arg2\', \'arg3\'];
184  $args2 = [\'arg4\', \'arg5\', \'arg6\'];
185  $args3 = [\'arg7\', \'arg8\', \'arg9\'];
186  $someVar->aMethod(...$args1, ...$args2, ...$args3);',
187  [],
188  ],
189  'double linked .rst file is returned only once' => [
190  [
191  'Foo->aMethod' => [
192  'unusedArgumentNumbers' => [ 1 ],
193  'restFiles' => [
194  'aRest.rst',
195  ],
196  ],
197  'Bar->aMethod' => [
198  'unusedArgumentNumbers' => [ 1 ],
199  'restFiles' => [
200  'aRest.rst',
201  ],
202  ],
203  ],
204  '<?php
205  $someVar->aMethod(\'foo\');',
206  [
207  0 => [
208  'restFiles' => [
209  'aRest.rst',
210  ],
211  ],
212  ],
213  ],
214  ];
215  }
216 
221  public function ‪matchesReturnsExpectedRestFiles(array $configuration, string $phpCode, array $expected)
222  {
223  ‪$parser = (new ParserFactory())->create(ParserFactory::ONLY_PHP7);
224  $statements = ‪$parser->parse($phpCode);
225 
226  $subject = new ‪MethodArgumentUnusedMatcher($configuration);
227 
228  $traverser = new NodeTraverser();
229  $traverser->addVisitor($subject);
230  $traverser->traverse($statements);
231 
232  $result = $subject->getMatches();
233  if (isset($expected[0], $result[0])) {
234  self::assertEquals($expected[0]['restFiles'], $result[0]['restFiles']);
235  } else {
236  self::assertEquals($expected, $result);
237  }
238  }
239 }
‪$parser
‪$parser
Definition: annotationChecker.php:108
‪TYPO3\CMS\Install\Tests\Unit\ExtensionScanner\Php\Matcher\MethodArgumentUnusedMatcherTest
Definition: MethodArgumentUnusedMatcherTest.php:30
‪TYPO3\CMS\Install\Tests\Unit\ExtensionScanner\Php\Matcher\MethodArgumentUnusedMatcherTest\hitsFromFixtureAreFound
‪hitsFromFixtureAreFound()
Definition: MethodArgumentUnusedMatcherTest.php:34
‪TYPO3\CMS\Install\Tests\Unit\ExtensionScanner\Php\Matcher
Definition: AbstractCoreMatcherTest.php:18
‪TYPO3\CMS\Install\Tests\Unit\ExtensionScanner\Php\Matcher\MethodArgumentUnusedMatcherTest\matchesReturnsExpectedRestFilesDataProvider
‪array matchesReturnsExpectedRestFilesDataProvider()
Definition: MethodArgumentUnusedMatcherTest.php:69
‪TYPO3\CMS\Install\ExtensionScanner\Php\Matcher\MethodArgumentUnusedMatcher
Definition: MethodArgumentUnusedMatcher.php:33
‪TYPO3\CMS\Install\Tests\Unit\ExtensionScanner\Php\Matcher\MethodArgumentUnusedMatcherTest\matchesReturnsExpectedRestFiles
‪matchesReturnsExpectedRestFiles(array $configuration, string $phpCode, array $expected)
Definition: MethodArgumentUnusedMatcherTest.php:221