1
|
|
|
<?php |
2
|
|
|
namespace nochso\Omni\PhpCsFixer; |
3
|
|
|
|
4
|
|
|
use nochso\Omni\Path; |
5
|
|
|
use Symfony\Component\Finder\Finder; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* DefaultFinder respects ignore files for Git, Mercurial and Darcs. |
9
|
|
|
*/ |
10
|
|
|
class DefaultFinder extends Finder { |
11
|
|
|
/** |
12
|
|
|
* @param array|string $dirs |
13
|
|
|
* |
14
|
|
|
* @return \nochso\Omni\PhpCsFixer\DefaultFinder |
15
|
|
|
*/ |
16
|
|
|
public static function createIn($dirs) { |
17
|
|
|
$finder = new self(); |
18
|
|
|
if (!is_array($dirs)) { |
19
|
|
|
$dirs = [$dirs]; |
20
|
|
|
} |
21
|
|
|
$finder->in($dirs); |
22
|
|
|
$excludes = []; |
23
|
|
|
foreach ($finder->getVcsIgnoreFiles($dirs) as $ignore) { |
24
|
|
|
$excludes = array_merge($excludes, $finder->readExcludeLines($ignore)); |
25
|
|
|
} |
26
|
|
|
$finder->exclude($excludes); |
27
|
|
|
return $finder; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function __construct() { |
31
|
|
|
parent::__construct(); |
32
|
|
|
foreach ($this->getNames() as $name) { |
33
|
|
|
$this->name($name); |
34
|
|
|
} |
35
|
|
|
$this->files()->ignoreDotFiles(true)->ignoreVCS(true); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @return array |
40
|
|
|
*/ |
41
|
|
|
protected function getNames() { |
42
|
|
|
return ['*.php', '*.twig', '#.*.ya?ml#i', '*.xml']; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param array $dirs |
47
|
|
|
* |
48
|
|
|
* @return array |
49
|
|
|
*/ |
50
|
|
|
protected function getVcsIgnoreFiles(array $dirs) { |
51
|
|
|
$files = ['.gitignore', '.hgignore', '_darcs/prefs/boring']; |
52
|
|
|
$filepaths = []; |
53
|
|
|
foreach ($dirs as $dir) { |
54
|
|
|
foreach ($files as $file) { |
55
|
|
|
$filepaths[] = Path::combine($dir, $file); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
return $filepaths; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param $ignoreFile |
63
|
|
|
* |
64
|
|
|
* @return array |
65
|
|
|
*/ |
66
|
|
|
private function readExcludeLines($ignoreFile) { |
67
|
|
|
if (!file_exists($ignoreFile)) { |
68
|
|
|
return []; |
69
|
|
|
} |
70
|
|
|
$lines = file($ignoreFile); |
71
|
|
|
if ($lines === false) { |
72
|
|
|
// Not sure how to test, so ignore it instead of inlining it above |
73
|
|
|
return []; |
74
|
|
|
// @codeCoverageIgnore |
75
|
|
|
|
76
|
|
|
} |
77
|
|
|
return array_map(function ($line) { |
78
|
|
|
return rtrim($line, "\r\n\\/"); |
79
|
|
|
}, $lines); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|