Skip to content

Commit 6296137

Browse files
committed
Code quality improvements
1 parent 9926e7c commit 6296137

File tree

7 files changed

+141
-83
lines changed

7 files changed

+141
-83
lines changed

FileIterator.php

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use MaplePHP\Blunder\Run;
1010
use RecursiveDirectoryIterator;
1111
use RecursiveIteratorIterator;
12+
use SplFileInfo;
1213

1314
class FileIterator
1415
{
@@ -31,18 +32,21 @@ public function executeAll(string $directory): void
3132
{
3233
$files = $this->findFiles($directory);
3334
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\" ");
3536
} else {
3637
foreach ($files as $file) {
3738
extract($this->args, EXTR_PREFIX_SAME, "wddx");
3839
Unit::resetUnit();
3940
Unit::setHeaders([
4041
"args" => $this->args,
4142
"file" => $file,
42-
"checksum" => md5($file)
43+
"checksum" => md5((string)$file)
4344
]);
4445

45-
$this->requireUnitFile($file)();
46+
$call = $this->requireUnitFile((string)$file);
47+
if (!is_null($call)) {
48+
$call();
49+
}
4650
if(!Unit::hasUnit()) {
4751
throw new RuntimeException("The Unitary Unit class has not been initiated inside \"$file\".");
4852
}
@@ -54,19 +58,22 @@ public function executeAll(string $directory): void
5458

5559
/**
5660
* Will Scan and find all unitary test files
57-
* @param $dir
61+
* @param string $dir
5862
* @return array
5963
*/
60-
private function findFiles($dir): array
64+
private function findFiles(string $dir): array
6165
{
6266
$files = [];
6367
$realDir = realpath($dir);
64-
if(!$realDir) {
68+
if($realDir === false) {
6569
throw new RuntimeException("Directory \"$dir\" does not exist. Try using a absolut path!");
6670
}
6771
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir));
72+
73+
/** @var string $pattern */
74+
$pattern = static::PATTERN;
6875
foreach ($iterator as $file) {
69-
if (fnmatch(static::PATTERN, $file->getFilename()) &&
76+
if (($file instanceof SplFileInfo) && fnmatch($pattern, $file->getFilename()) &&
7077
(isset($this->args['path']) || !str_contains($file->getPathname(), DIRECTORY_SEPARATOR . "vendor" . DIRECTORY_SEPARATOR ))) {
7178
if(!$this->findExcluded($this->exclude(), $dir, $file->getPathname())) {
7279
$files[] = $file->getPathname();
@@ -83,7 +90,7 @@ private function findFiles($dir): array
8390
function exclude(): array
8491
{
8592
$excl = array();
86-
if(isset($this->args['exclude'])) {
93+
if(isset($this->args['exclude']) && is_string($this->args['exclude'])) {
8794
$exclude = explode(',', $this->args['exclude']);
8895
foreach ($exclude as $file) {
8996
$file = str_replace(['"', "'"], "", $file);
@@ -109,7 +116,7 @@ function findExcluded(array $exclArr, string $relativeDir, string $file): bool
109116
{
110117
$file = $this->getNaturalPath($file);
111118
foreach ($exclArr as $excl) {
112-
$relativeExclPath = $this->getNaturalPath($relativeDir . DIRECTORY_SEPARATOR . $excl);
119+
$relativeExclPath = $this->getNaturalPath($relativeDir . DIRECTORY_SEPARATOR . (string)$excl);
113120
if(fnmatch($relativeExclPath, $file)) {
114121
return true;
115122
}
@@ -130,13 +137,13 @@ function getNaturalPath(string $path): string
130137
/**
131138
* Require file without inheriting any class information
132139
* @param string $file
133-
* @return Closure
140+
* @return Closure|null
134141
*/
135-
private function requireUnitFile(string $file): Closure
142+
private function requireUnitFile(string $file): ?Closure
136143
{
137-
$call = function() use ($file): void
144+
$clone = clone $this;
145+
$call = function() use ($file, $clone): void
138146
{
139-
140147
$cli = new CliHandler();
141148
if(isset(self::$headers['args']['trace'])) {
142149
$cli->enableTraceLines(true);
@@ -145,14 +152,32 @@ private function requireUnitFile(string $file): Closure
145152
$run->load();
146153

147154
ob_start();
155+
if (!is_file($file)) {
156+
throw new RuntimeException("File \"$file\" do not exists.");
157+
}
148158
require_once ($file);
149-
Unit::getUnit()->execute();
159+
160+
$clone->getUnit()->execute();
150161

151162
$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);
154165
}
155166
};
156167
return $call->bindTo(null);
157168
}
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+
}
158183
}

README.md

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,20 +63,18 @@ $unit = new MaplePHP\Unitary\Unit();
6363

6464
// If you build your library right it will become very easy to mock, like I have below.
6565
$request = new MaplePHP\Http\Request(
66-
"POST", // The HTTP Method (GET, POST, PUT, DELETE, PATCH)
67-
"https://admin:mypass@example.com:65535/test.php?id=5221&place=stockholm", // The Request URI
68-
["Content-Type" => "application/x-www-form-urlencoded"], // Add Headers, empty array is allowed
69-
["email" => "john.doe@example.com"] // Post data
66+
"GET",
67+
"https://admin:mypass@example.com:65535/test.php?id=5221&greeting=hello",
7068
);
7169

7270
// Begin by adding a test
7371
$unit->case("MaplePHP Request URI path test", function() use($request) {
7472

7573
// Test 1
7674
$this->add($request->getMethod(), function() {
77-
return $this->equal("POST");
75+
return $this->equal("GET");
7876

79-
}, "HTTP Request method Type is not POST");
77+
}, "HTTP Request method Type does not equal GET");
8078
// Adding a error message is not required, but it is highly recommended
8179

8280
// Test 2
@@ -96,7 +94,7 @@ $unit->case("MaplePHP Request URI path test", function() use($request) {
9694
return ($this->withValue($arr[0])->equal("admin") && $this->withValue($arr[1])->equal("mypass"));
9795
}
9896

99-
], "Is not a valid port number");
97+
], "Did not get the expected user Info credentials");
10098
});
10199

102100
$unit->execute();

TestCase.php

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@
44
namespace MaplePHP\Unitary;
55

66
use BadMethodCallException;
7-
use Closure;
87
use ErrorException;
9-
use MaplePHP\Validate\Inp;
108
use RuntimeException;
9+
use Closure;
1110
use Throwable;
11+
use MaplePHP\Validate\Inp;
1212

1313
class TestCase
1414
{
1515
private mixed $value;
1616
private ?string $message;
17-
private array $test;
17+
private array $test = [];
1818
private int $count = 0;
19-
private Closure $bind;
19+
private ?Closure $bind = null;
2020

2121
function __construct(?string $message = null)
2222
{
@@ -40,7 +40,9 @@ function bind(Closure $bind): void
4040
function dispatchTest(): array
4141
{
4242
$test = $this->bind;
43-
$test($this);
43+
if (!is_null($test)) {
44+
$test($this);
45+
}
4446
return $this->test;
4547
}
4648

@@ -56,11 +58,11 @@ function add(mixed $expect, array|Closure $validation, ?string $message = null):
5658
{
5759
$this->value = $expect;
5860
$test = new TestUnit($this->value, $message);
59-
if(is_callable($validation)) {
61+
if($validation instanceof Closure) {
6062
$test->setUnit($this->buildClosureTest($validation));
6163
} else {
6264
foreach($validation as $method => $args) {
63-
if(!is_callable($args) && !is_array($args)) {
65+
if(!($args instanceof Closure) && !is_array($args)) {
6466
$args = [$args];
6567
}
6668
$test->setUnit($this->buildArrayTest($method, $args), $method, (is_array($args) ? $args : []));
@@ -145,8 +147,11 @@ public function getTest(): array
145147
*/
146148
public function buildClosureTest(Closure $validation): bool
147149
{
150+
$bool = false;
148151
$validation = $validation->bindTo($this->valid($this->value));
149-
$bool = $validation($this->value);
152+
if(!is_null($validation)) {
153+
$bool = $validation($this->value);
154+
}
150155
if(!is_bool($bool)) {
151156
throw new RuntimeException("A callable validation must return a boolean!");
152157
}
@@ -167,8 +172,11 @@ public function buildClosureTest(Closure $validation): bool
167172
*/
168173
public function buildArrayTest(string $method, array|Closure $args): bool
169174
{
170-
if(is_callable($args)) {
175+
if($args instanceof Closure) {
171176
$args = $args->bindTo($this->valid($this->value));
177+
if(is_null($args)) {
178+
throw new ErrorException("The argument is not returning a callable Closure!");
179+
}
172180
$bool = $args($this->value);
173181
if(!is_bool($bool)) {
174182
throw new RuntimeException("A callable validation must return a boolean!");
@@ -177,17 +185,15 @@ public function buildArrayTest(string $method, array|Closure $args): bool
177185
if(!method_exists(Inp::class, $method)) {
178186
throw new BadMethodCallException("The validation $method does not exist!");
179187
}
180-
if(!is_array($args)) {
181-
$args = [];
182-
}
188+
183189
try {
184190
$bool = call_user_func_array([$this->valid($this->value), $method], $args);
185191
} catch (Throwable $e) {
186-
throw new RuntimeException($e->getMessage(), $e->getCode(), $e);
192+
throw new RuntimeException($e->getMessage(), (int)$e->getCode(), $e);
187193
}
188194
}
189195

190-
return $bool;
196+
return (bool)$bool;
191197
}
192198

193199
/**

TestUnit.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class TestUnit
1111
private bool $valid;
1212
private mixed $value;
1313
private ?string $message;
14-
private array $unit;
14+
private array $unit = [];
1515
private int $count = 0;
1616

1717
/**
@@ -136,7 +136,7 @@ public function getReadValue(): string
136136
final protected function excerpt(string $value): string
137137
{
138138
$format = new Str($value);
139-
return $format->excerpt(40)->get();
139+
return (string)$format->excerpt(42)->get();
140140
}
141141

142142
}

0 commit comments

Comments
 (0)