forked from infection/infection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProjectCodeProvider.php
318 lines (282 loc) · 10.8 KB
/
ProjectCodeProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
<?php
/**
* This code is licensed under the BSD 3-Clause License.
*
* Copyright (c) 2017, Maks Rafalko
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
declare(strict_types=1);
namespace Infection\Tests\AutoReview\ProjectCode;
use function array_filter;
use function array_map;
use const DIRECTORY_SEPARATOR;
use function in_array;
use Infection\CannotBeInstantiated;
use Infection\Command\ConfigureCommand;
use Infection\Command\RunCommand;
use Infection\Config\ConsoleHelper;
use Infection\Config\Guesser\SourceDirGuesser;
use Infection\Configuration\Schema\SchemaConfigurationFactory;
use Infection\Configuration\Schema\SchemaConfigurationFileLoader;
use Infection\Configuration\Schema\SchemaValidator;
use Infection\Console\Application;
use Infection\Console\OutputFormatter\FormatterName;
use Infection\Console\OutputFormatter\OutputFormatter;
use Infection\Console\OutputFormatter\ProgressFormatter;
use Infection\Console\XdebugHandler;
use Infection\Event\Subscriber\MutationGeneratingConsoleLoggerSubscriber;
use Infection\Event\Subscriber\NullSubscriber;
use Infection\FileSystem\DummyFileSystem;
use Infection\FileSystem\Finder\ComposerExecutableFinder;
use Infection\FileSystem\Finder\NonExecutableFinder;
use Infection\FileSystem\Finder\TestFrameworkFinder;
use Infection\Logger\Http\StrykerCurlClient;
use Infection\Logger\Http\StrykerDashboardClient;
use Infection\Metrics\MetricsCalculator;
use Infection\Mutant\DetectionStatus;
use Infection\Mutation\MutationAttributeKeys;
use Infection\Mutator\NodeMutationGenerator;
use Infection\Process\Runner\IndexedProcessBearer;
use Infection\Process\ShellCommandLineExecutor;
use Infection\TestFramework\AdapterInstaller;
use Infection\TestFramework\Coverage\JUnit\TestFileTimeData;
use Infection\TestFramework\Coverage\NodeLineRangeData;
use Infection\TestFramework\Coverage\SourceMethodLineRange;
use Infection\TestFramework\Coverage\TestLocations;
use Infection\TestFramework\PhpUnit\Config\Builder\InitialConfigBuilder as PhpUnitInitalConfigBuilder;
use Infection\TestFramework\PhpUnit\Config\Builder\MutationConfigBuilder as PhpUnitMutationConfigBuilder;
use Infection\TestFramework\TestFrameworkTypes;
use Infection\Tests\AutoReview\ConcreteClassReflector;
use function Infection\Tests\generator_to_phpunit_data_provider;
use function iterator_to_array;
use function ltrim;
use ReflectionClass;
use function Safe\sort;
use function Safe\sprintf;
use const SORT_STRING;
use function str_replace;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;
final class ProjectCodeProvider
{
use CannotBeInstantiated;
/**
* This array contains all classes that don't have tests yet, due to legacy
* reasons. This list should never be added to, only removed from.
*/
public const NON_TESTED_CONCRETE_CLASSES = [
ConfigureCommand::class,
RunCommand::class,
Application::class,
ProgressFormatter::class,
ComposerExecutableFinder::class,
StrykerCurlClient::class,
MutationGeneratingConsoleLoggerSubscriber::class,
TestFrameworkTypes::class,
NodeMutationGenerator::class,
NonExecutableFinder::class,
AdapterInstaller::class,
DetectionStatus::class,
DummyFileSystem::class,
MutationAttributeKeys::class,
XdebugHandler::class,
NullSubscriber::class,
FormatterName::class,
ShellCommandLineExecutor::class,
];
/**
* This array contains all classes that are not extension points, but not final due to legacy
* reasons. This list should never be added to, only removed from.
*/
public const NON_FINAL_EXTENSION_CLASSES = [
ConsoleHelper::class,
SourceDirGuesser::class,
TestFrameworkFinder::class,
StrykerDashboardClient::class,
MetricsCalculator::class,
PhpUnitInitalConfigBuilder::class,
PhpUnitMutationConfigBuilder::class,
];
/**
* This array contains all classes that can be extended by our users.
*/
public const EXTENSION_POINTS = [
OutputFormatter::class,
SchemaConfigurationFactory::class,
SchemaConfigurationFileLoader::class,
SchemaValidator::class,
];
/**
* @var string[]|null
*/
private static $sourceClasses;
/**
* @var string[]|null
*/
private static $sourceClassesToCheckForPublicProperties;
/**
* @var string[]|null
*/
private static $testClasses;
public static function provideSourceClasses(): iterable
{
if (self::$sourceClasses !== null) {
yield from self::$sourceClasses;
return;
}
$finder = Finder::create()
->files()
->name('*.php')
->in(__DIR__ . '/../../../../src')
;
$classes = array_map(
static function (SplFileInfo $file): string {
return sprintf(
'%s\\%s%s%s',
'Infection',
str_replace(DIRECTORY_SEPARATOR, '\\', $file->getRelativePath()),
$file->getRelativePath() !== '' ? '\\' : '',
$file->getBasename('.' . $file->getExtension())
);
},
iterator_to_array($finder, false)
);
sort($classes, SORT_STRING);
self::$sourceClasses = $classes;
yield from self::$sourceClasses;
}
public static function sourceClassesProvider(): iterable
{
yield from generator_to_phpunit_data_provider(
self::provideSourceClasses()
);
}
public static function provideConcreteSourceClasses(): iterable
{
yield from ConcreteClassReflector::filterByConcreteClasses(iterator_to_array(
self::provideSourceClasses(),
true
));
}
public static function concreteSourceClassesProvider(): iterable
{
yield from generator_to_phpunit_data_provider(
self::provideConcreteSourceClasses()
);
}
public static function provideSourceClassesToCheckForPublicProperties(): iterable
{
if (self::$sourceClassesToCheckForPublicProperties !== null) {
yield from self::$sourceClassesToCheckForPublicProperties;
return;
}
self::$sourceClassesToCheckForPublicProperties = array_filter(
iterator_to_array(self::provideSourceClasses(), true),
static function (string $className): bool {
$reflectionClass = new ReflectionClass($className);
return !$reflectionClass->isInterface()
&& !in_array(
$className,
[
// having public properties on DTO is for performance reasons
TestLocations::class,
SourceMethodLineRange::class,
NodeLineRangeData::class,
TestFileTimeData::class,
IndexedProcessBearer::class,
],
true
)
;
}
);
yield from self::$sourceClassesToCheckForPublicProperties;
}
public static function sourceClassesToCheckForPublicPropertiesProvider(): iterable
{
yield from generator_to_phpunit_data_provider(
self::provideSourceClassesToCheckForPublicProperties()
);
}
public static function provideTestClasses(): iterable
{
if (self::$testClasses !== null) {
yield from self::$testClasses;
return;
}
$finder = Finder::create()
->files()
->name('*.php')
->in(__DIR__ . '/../../../../tests')
->notName('Helpers.php')
->notPath('xdebug-filter.php')
->exclude([
'autoloaded',
'benchmark',
'e2e',
'Fixtures',
])
;
$classes = array_map(
static function (SplFileInfo $file): string {
$fqcnPart = ltrim(str_replace('phpunit', '', $file->getRelativePath()), DIRECTORY_SEPARATOR);
$fqcnPart = str_replace(DIRECTORY_SEPARATOR, '\\', $fqcnPart);
return sprintf(
'Infection\\Tests\\%s%s%s',
$fqcnPart,
$file->getRelativePath() === 'phpunit' ? '' : '\\',
$file->getBasename('.' . $file->getExtension())
);
},
iterator_to_array($finder, false)
);
sort($classes, SORT_STRING);
self::$testClasses = $classes;
yield from self::$testClasses;
}
// "testClassesProvider" would be more correct but PHPUnit will then detect this method as a
// test instead of a test provider.
public static function classesTestProvider(): iterable
{
yield from generator_to_phpunit_data_provider(
self::provideTestClasses()
);
}
public static function nonTestedConcreteClassesProvider(): iterable
{
yield from generator_to_phpunit_data_provider(
self::NON_TESTED_CONCRETE_CLASSES
);
}
public static function nonFinalExtensionClasses(): iterable
{
yield from generator_to_phpunit_data_provider(
self::NON_FINAL_EXTENSION_CLASSES
);
}
}