1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\CodeOutline\Parser; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Spatie\CodeOutline\Page; |
7
|
|
|
use Symfony\Component\Finder\Finder; |
8
|
|
|
|
9
|
|
|
class DirectoryParser implements Parser |
10
|
|
|
{ |
11
|
|
|
/** @var string */ |
12
|
|
|
protected $path; |
13
|
|
|
|
14
|
|
|
/** @var array */ |
15
|
|
|
protected $extensions = ['php']; |
16
|
|
|
|
17
|
|
|
/** @var Closure */ |
18
|
|
|
protected $initListener; |
19
|
|
|
|
20
|
|
|
/** @var Closure */ |
21
|
|
|
protected $progressListener; |
22
|
|
|
|
23
|
|
|
public function __construct(string $path) |
24
|
|
|
{ |
25
|
|
|
$this->path = $path; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function setExtensions(array $extensions): self |
29
|
|
|
{ |
30
|
|
|
$this->extensions = $extensions; |
31
|
|
|
|
32
|
|
|
return $this; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function setExtensionsFromString(string $extensionsString): self |
36
|
|
|
{ |
37
|
|
|
$extensions = explode(',', $extensionsString); |
38
|
|
|
|
39
|
|
|
return $this->setExtensions( |
40
|
|
|
array_map(function (string $extension) { |
41
|
|
|
return str_replace(['*', '.'], '', $extension); |
42
|
|
|
}, $extensions) |
43
|
|
|
); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function onStartParsing(Closure $initListener): self |
47
|
|
|
{ |
48
|
|
|
$this->initListener = $initListener; |
49
|
|
|
|
50
|
|
|
return $this; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function onFileParsed(Closure $progressListener): self |
54
|
|
|
{ |
55
|
|
|
$this->progressListener = $progressListener; |
56
|
|
|
|
57
|
|
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function getParsed(): Page |
61
|
|
|
{ |
62
|
|
|
$parsedFiles = []; |
63
|
|
|
|
64
|
|
|
$files = Finder::create()->files()->in($this->path)->name($this->getExtensionsRegex()); |
65
|
|
|
|
66
|
|
|
if ($this->initListener) { |
67
|
|
|
call_user_func_array($this->initListener, [$files->count()]); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** @var \Symfony\Component\Finder\SplFileInfo $file */ |
71
|
|
|
foreach ($files as $file) { |
72
|
|
|
$fileParser = new FileParser($file->getRealPath()); |
73
|
|
|
|
74
|
|
|
$parsedFiles[] = $fileParser->getParsed(); |
75
|
|
|
|
76
|
|
|
if (!$this->progressListener) { |
77
|
|
|
continue; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
call_user_func($this->progressListener); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $this->summarize($parsedFiles); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param \Spatie\CodeOutline\Page[] $pages |
88
|
|
|
* |
89
|
|
|
* @return \Spatie\CodeOutline\Page |
90
|
|
|
*/ |
91
|
|
|
protected function summarize(array $pages): Page |
92
|
|
|
{ |
93
|
|
|
$summarizedPage = new Page(); |
94
|
|
|
|
95
|
|
|
foreach ($pages as $page) { |
96
|
|
|
foreach ($page as $lineNumber => $line) { |
97
|
|
|
$summarizedLine = $summarizedPage[$lineNumber] ?? new Line(); |
|
|
|
|
98
|
|
|
|
99
|
|
|
$summarizedPage[$lineNumber] = $summarizedLine->merge($line); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $summarizedPage; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
protected function getExtensionsRegex(): string |
107
|
|
|
{ |
108
|
|
|
$query = implode('|', array_map(function ($extension) { |
109
|
|
|
return "\.{$extension}\$"; |
110
|
|
|
}, $this->extensions)); |
111
|
|
|
|
112
|
|
|
return "/{$query}/"; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths