9
9
use MaplePHP \Blunder \Run ;
10
10
use RecursiveDirectoryIterator ;
11
11
use RecursiveIteratorIterator ;
12
+ use SplFileInfo ;
12
13
13
14
class FileIterator
14
15
{
@@ -31,18 +32,21 @@ public function executeAll(string $directory): void
31
32
{
32
33
$ files = $ this ->findFiles ($ directory );
33
34
if (empty ($ files )) {
34
- throw new RuntimeException ("No files found matching the pattern \"" . static ::PATTERN . "\" in directory \"$ directory \" " );
35
+ throw new RuntimeException ("No files found matching the pattern \"" . ( string )( static ::PATTERN ?? "" ) . "\" in directory \"$ directory \" " );
35
36
} else {
36
37
foreach ($ files as $ file ) {
37
38
extract ($ this ->args , EXTR_PREFIX_SAME , "wddx " );
38
39
Unit::resetUnit ();
39
40
Unit::setHeaders ([
40
41
"args " => $ this ->args ,
41
42
"file " => $ file ,
42
- "checksum " => md5 ($ file )
43
+ "checksum " => md5 (( string ) $ file )
43
44
]);
44
45
45
- $ this ->requireUnitFile ($ file )();
46
+ $ call = $ this ->requireUnitFile ((string )$ file );
47
+ if (!is_null ($ call )) {
48
+ $ call ();
49
+ }
46
50
if (!Unit::hasUnit ()) {
47
51
throw new RuntimeException ("The Unitary Unit class has not been initiated inside \"$ file \". " );
48
52
}
@@ -54,19 +58,22 @@ public function executeAll(string $directory): void
54
58
55
59
/**
56
60
* Will Scan and find all unitary test files
57
- * @param $dir
61
+ * @param string $dir
58
62
* @return array
59
63
*/
60
- private function findFiles ($ dir ): array
64
+ private function findFiles (string $ dir ): array
61
65
{
62
66
$ files = [];
63
67
$ realDir = realpath ($ dir );
64
- if (! $ realDir ) {
68
+ if ($ realDir === false ) {
65
69
throw new RuntimeException ("Directory \"$ dir \" does not exist. Try using a absolut path! " );
66
70
}
67
71
$ iterator = new RecursiveIteratorIterator (new RecursiveDirectoryIterator ($ dir ));
72
+
73
+ /** @var string $pattern */
74
+ $ pattern = static ::PATTERN ;
68
75
foreach ($ iterator as $ file ) {
69
- if (fnmatch (static :: PATTERN , $ file ->getFilename ()) &&
76
+ if (( $ file instanceof SplFileInfo) && fnmatch ($ pattern , $ file ->getFilename ()) &&
70
77
(isset ($ this ->args ['path ' ]) || !str_contains ($ file ->getPathname (), DIRECTORY_SEPARATOR . "vendor " . DIRECTORY_SEPARATOR ))) {
71
78
if (!$ this ->findExcluded ($ this ->exclude (), $ dir , $ file ->getPathname ())) {
72
79
$ files [] = $ file ->getPathname ();
@@ -83,7 +90,7 @@ private function findFiles($dir): array
83
90
function exclude (): array
84
91
{
85
92
$ excl = array ();
86
- if (isset ($ this ->args ['exclude ' ])) {
93
+ if (isset ($ this ->args ['exclude ' ]) && is_string ( $ this -> args [ ' exclude ' ]) ) {
87
94
$ exclude = explode (', ' , $ this ->args ['exclude ' ]);
88
95
foreach ($ exclude as $ file ) {
89
96
$ file = str_replace (['" ' , "' " ], "" , $ file );
@@ -109,7 +116,7 @@ function findExcluded(array $exclArr, string $relativeDir, string $file): bool
109
116
{
110
117
$ file = $ this ->getNaturalPath ($ file );
111
118
foreach ($ exclArr as $ excl ) {
112
- $ relativeExclPath = $ this ->getNaturalPath ($ relativeDir . DIRECTORY_SEPARATOR . $ excl );
119
+ $ relativeExclPath = $ this ->getNaturalPath ($ relativeDir . DIRECTORY_SEPARATOR . ( string ) $ excl );
113
120
if (fnmatch ($ relativeExclPath , $ file )) {
114
121
return true ;
115
122
}
@@ -130,13 +137,13 @@ function getNaturalPath(string $path): string
130
137
/**
131
138
* Require file without inheriting any class information
132
139
* @param string $file
133
- * @return Closure
140
+ * @return Closure|null
134
141
*/
135
- private function requireUnitFile (string $ file ): Closure
142
+ private function requireUnitFile (string $ file ): ? Closure
136
143
{
137
- $ call = function () use ($ file ): void
144
+ $ clone = clone $ this ;
145
+ $ call = function () use ($ file , $ clone ): void
138
146
{
139
-
140
147
$ cli = new CliHandler ();
141
148
if (isset (self ::$ headers ['args ' ]['trace ' ])) {
142
149
$ cli ->enableTraceLines (true );
@@ -145,14 +152,32 @@ private function requireUnitFile(string $file): Closure
145
152
$ run ->load ();
146
153
147
154
ob_start ();
155
+ if (!is_file ($ file )) {
156
+ throw new RuntimeException ("File \"$ file \" do not exists. " );
157
+ }
148
158
require_once ($ file );
149
- Unit::getUnit ()->execute ();
159
+
160
+ $ clone ->getUnit ()->execute ();
150
161
151
162
$ outputBuffer = ob_get_clean ();
152
- if ( $ outputBuffer && Unit::hasUnit ()) {
153
- Unit:: getUnit ()->buildNotice ("Note: " , $ outputBuffer , 80 );
163
+ if ( strlen ( $ outputBuffer) && Unit::hasUnit ()) {
164
+ $ clone -> getUnit ()->buildNotice ("Note: " , $ outputBuffer , 80 );
154
165
}
155
166
};
156
167
return $ call ->bindTo (null );
157
168
}
169
+
170
+ /**
171
+ * @return Unit
172
+ * @throws RuntimeException|\Exception
173
+ */
174
+ protected function getUnit (): Unit
175
+ {
176
+ $ unit = Unit::getUnit ();
177
+ if (is_null ($ unit )) {
178
+ throw new RuntimeException ("The Unit instance has not been initiated. " );
179
+ }
180
+ return $ unit ;
181
+
182
+ }
158
183
}
0 commit comments