-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fixers.php
91 lines (71 loc) · 2.38 KB
/
Fixers.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
<?php declare(strict_types = 1);
/*
* This file is part of the Vairogs package.
*
* (c) Dāvis Zālītis (k0d3r1s) <davis@vairogs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Vairogs\PhpCsFixerCustomFixers;
use DirectoryIterator;
use Generator;
use IteratorAggregate;
use PhpCsFixer\Fixer\FixerInterface;
use Symfony\Component\Finder\Finder;
use Vairogs\Component\Functions\Preg;
use Vairogs\PhpCsFixerCustomFixers\PhpCsFixer\AbstractFixer;
use function assert;
use function class_exists;
use function file_get_contents;
use function in_array;
use function sort;
final class Fixers implements IteratorAggregate
{
public function getIterator(): Generator
{
$classNames = [];
foreach (new DirectoryIterator(__DIR__ . '/Fixer') as $fileInfo) {
$fileName = $fileInfo->getBasename('.php');
if (in_array($fileName, ['.', '..', ], true)) {
continue;
}
$classNames[] = __NAMESPACE__ . '\\Fixer\\' . $fileName;
}
sort($classNames);
foreach ($classNames as $className) {
$fixer = new $className();
assert($fixer instanceof FixerInterface);
yield $fixer;
}
}
public static function getFixers(): array
{
$finder = new Finder();
$finder->files()->in(__DIR__ . '/Fixer/')->name('*.php');
$files = [];
static $_helper = null;
if (null === $_helper) {
$_helper = new class {
use Preg\_Match;
};
}
$classMatches = $namespaceMatches = ['', ''];
foreach ($finder as $file) {
$fileContents = file_get_contents($file->getRealPath());
if ($_helper->match('/namespace\s+(.+?);/', $fileContents, $namespaceMatches)
&& $_helper->match('/class\s+(\w+)/', $fileContents, $classMatches)) {
$className = $classMatches[1];
$fullClassName = $namespaceMatches[1] . '\\' . $className;
if (class_exists($fullClassName)) {
$files[] = $fullClassName;
}
}
}
$fixers = [];
foreach ($files as $fixer) {
$fixers[AbstractFixer::getNameForClass($fixer)] = true;
}
return $fixers;
}
}