forked from deprecated-packages/symplify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspecify-rule.php
76 lines (56 loc) · 2.26 KB
/
specify-rule.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
<?php
use Nette\Utils\Strings;
use Symplify\SmartFileSystem\SmartFileSystem;
require __DIR__ . '/vendor/autoload.php';
$finder = new Symfony\Component\Finder\Finder();
$fileInfos = $finder->in([
__DIR__ . '/packages/phpstan-rules/packages/nette/tests',
__DIR__ . '/packages/phpstan-rules/packages/symfony/tests',
__DIR__ . '/packages/phpstan-rules/packages/cognitive-complexity/tests',
__DIR__ . '/packages/phpstan-rules/packages/object-calisthenics/tests',
])
->files()
->name('*.neon')
->getIterator();
$smartFileSystem = new SmartFileSystem();
foreach ($fileInfos as $fileInfo) {
$configFileContent = $smartFileSystem->readFile($fileInfo->getRealPath());
// 1. is generic config?
if (! str_contains($configFileContent, '-rules.neon')) {
continue;
}
dump($fileInfo->getRealPath());
// 2. detect rule class name from the test
$baseRuleDirectory = dirname($fileInfo->getPath());
$match = Strings::match($baseRuleDirectory, '#packages/(?<name>\w+)/tests#');
if ($match === null) {
continue;
}
$category = ucfirst($match['name']);
$specificRuleDirectory = Strings::after($baseRuleDirectory, 'tests/');
$specificRuleDirectory = str_replace('/', '\\', $specificRuleDirectory);
$ruleClass = 'Symplify\\PHPStanRules\\' . $category . '\\' . $specificRuleDirectory;
if (! class_exists($ruleClass)) {
continue;
}
// 3. remove symplify-rules line
$configFileContentLines = explode(PHP_EOL, $configFileContent);
foreach ($configFileContentLines as $key => $configFileContentLine) {
if (! str_contains($configFileContentLine, 'symplify-rules.neon')) {
continue;
}
unset($configFileContentLines[$key]);
}
$configFileContent = implode(PHP_EOL, $configFileContentLines);
// 4. add services section with rule registration :)
$ruleServicesTemplate = <<<'CODE_SAMPLE'
services:
-
class: %s
tags: [phpstan.rules.rule]
CODE_SAMPLE;
$ruleServicesContent = sprintf($ruleServicesTemplate, $ruleClass);
$newConfigContent = $configFileContent . PHP_EOL . $ruleServicesContent . PHP_EOL;
// 5. dump content
$smartFileSystem->dumpFile($fileInfo->getRealPath(), $newConfigContent);
}