1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Todolo; |
6
|
|
|
|
7
|
|
|
use Todolo\Helper\FileHelper; |
8
|
|
|
|
9
|
|
|
class TodoFinder |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var FileHelper |
13
|
|
|
*/ |
14
|
|
|
protected $fileHelper; |
15
|
|
|
|
16
|
4 |
|
public function __construct(FileHelper $fileHelper) |
17
|
|
|
{ |
18
|
4 |
|
$this->fileHelper = $fileHelper; |
19
|
4 |
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @return array<array> |
23
|
|
|
*/ |
24
|
1 |
|
public function extractAllTodos(string $fileContent): array |
25
|
|
|
{ |
26
|
1 |
|
$result = []; |
27
|
|
|
|
28
|
1 |
|
$result = array_merge( |
29
|
1 |
|
$result, |
30
|
1 |
|
$this->extractSingleLineAtTODOs($fileContent), |
31
|
1 |
|
$this->extractSingleLineFIXMEs($fileContent), |
32
|
1 |
|
$this->extractSingleLineTODOs($fileContent) |
33
|
|
|
); |
34
|
|
|
|
35
|
1 |
|
return $result; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Extracted single line @todo. |
40
|
|
|
* |
41
|
|
|
* @return array<int, array> |
42
|
|
|
*/ |
43
|
2 |
|
public function extractSingleLineAtTODOs(string $fileContent): array |
44
|
|
|
{ |
45
|
2 |
|
$result = []; |
46
|
|
|
|
47
|
2 |
|
preg_match_all('/\*\s+@todo\s*(.*)/mi', $fileContent, $matches); |
48
|
|
|
|
49
|
2 |
|
if (isset($matches[1])) { |
50
|
2 |
|
foreach ($matches[1] as $match) { |
51
|
2 |
|
$result[] = [ |
52
|
2 |
|
'message' => trim($match), |
53
|
|
|
]; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
2 |
|
return $result; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Extracted single line FIXME. |
62
|
|
|
* |
63
|
|
|
* @return array<int, array> |
64
|
|
|
*/ |
65
|
2 |
|
public function extractSingleLineFIXMEs(string $fileContent): array |
66
|
|
|
{ |
67
|
2 |
|
$result = []; |
68
|
|
|
|
69
|
2 |
|
preg_match_all('/[\/\/|\*]\s+FIXME[:]*\s*(.*)/mi', $fileContent, $matches); |
70
|
|
|
|
71
|
2 |
|
if (isset($matches[1])) { |
72
|
2 |
|
foreach ($matches[1] as $match) { |
73
|
2 |
|
$result[] = [ |
74
|
2 |
|
'message' => trim($match), |
75
|
|
|
]; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
2 |
|
return $result; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Extracted single line TODO. |
84
|
|
|
* |
85
|
|
|
* @return array<int, array> |
86
|
|
|
*/ |
87
|
2 |
|
public function extractSingleLineTODOs(string $fileContent): array |
88
|
|
|
{ |
89
|
2 |
|
$result = []; |
90
|
|
|
|
91
|
2 |
|
preg_match_all('/[\/\/|\*]\s+TODO[:]*\s*(.*)/mi', $fileContent, $matches1); |
92
|
|
|
|
93
|
2 |
|
if (isset($matches1[1])) { |
94
|
2 |
|
foreach ($matches1[1] as $match) { |
95
|
2 |
|
$result[] = [ |
96
|
2 |
|
'message' => trim($match), |
97
|
|
|
]; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
2 |
|
return $result; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return array<array> |
106
|
|
|
*/ |
107
|
1 |
|
public function getAllTodosForPHPFilesIn(string $dir): array |
108
|
|
|
{ |
109
|
1 |
|
$result = []; |
110
|
|
|
|
111
|
1 |
|
$phpFiles = $this->fileHelper->getListOfAllPHPFiles($dir); |
112
|
|
|
|
113
|
1 |
|
foreach ($phpFiles as $filePathInDir) { |
114
|
1 |
|
$fullpath = $dir.$filePathInDir; |
115
|
|
|
|
116
|
1 |
|
$fileContent = (string) file_get_contents($fullpath); |
117
|
|
|
|
118
|
|
|
// if file is not empty |
119
|
1 |
|
if (0 < \strlen($fileContent)) { |
120
|
1 |
|
$result[$filePathInDir] = $this->extractAllTodos($fileContent); |
121
|
|
|
} else { |
122
|
|
|
continue; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
1 |
|
return $result; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|