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