Skip to content

Commit 11dc744

Browse files
committed
Code quality
1 parent 2965cdb commit 11dc744

File tree

3 files changed

+64
-28
lines changed

3 files changed

+64
-28
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
._*
22
.DS_Store
3-
.sass-cache
3+
.sass-cache
4+
vendor

Stream.php

+26-27
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ class Stream implements StreamInterface
2424
private const READABLE_MATCH = '/r|a\+|ab\+|w\+|wb\+|x\+|xb\+|c\+|cb\+/';
2525
private const WRITABLE_MATCH = '/a|w|r\+|rb\+|rw|x|c/';
2626

27-
private $stream;
28-
private $permission;
29-
private $resource;
30-
private $size;
31-
private $meta = array();
32-
private $readable;
33-
private $writable;
34-
private $seekable;
27+
private mixed $stream;
28+
private string $permission;
29+
private mixed $resource;
30+
private ?int $size = null;
31+
private array $meta;
32+
private bool $readable;
33+
private bool $writable;
34+
private bool $seekable;
3535

3636
/**
3737
* PSR-7 Stream
@@ -47,11 +47,11 @@ public function __construct(mixed $stream = null, string $permission = "r+")
4747
if (is_resource($stream)) {
4848
$this->resource = $stream;
4949
$this->meta = $this->getMetadata();
50-
51-
if (is_null($this->meta)) {
52-
throw new RuntimeException("Could not access the stream meta data.", 1);
50+
/*
51+
if (is_null($this->meta)) {
52+
throw new RuntimeException("Could not access the stream metadata.", 1);
5353
}
54-
54+
*/
5555
$this->stream = $this->meta['stream_type'];
5656
$this->permission = $this->meta['mode'];
5757
} else {
@@ -112,7 +112,7 @@ public function detach()
112112
}
113113

114114
/**
115-
* Returns whether or not the stream is seekable.
115+
* Returns whether the stream is seekable.
116116
* @return bool
117117
*/
118118
public function isSeekable(): bool
@@ -121,7 +121,7 @@ public function isSeekable(): bool
121121
}
122122

123123
/**
124-
* Returns whether or not the stream is writable.
124+
* Returns whether the stream is writable.
125125
* @return bool
126126
*/
127127
public function isWritable(): bool
@@ -130,7 +130,7 @@ public function isWritable(): bool
130130
}
131131

132132
/**
133-
* Returns whether or not the stream is readable.
133+
* Returns whether the stream is readable.
134134
* @return bool
135135
*/
136136
public function isReadable(): bool
@@ -159,14 +159,14 @@ public function stats(?string $key = null): mixed
159159
public function getSize(): ?int
160160
{
161161
$size = $this->stats('size');
162-
$this->size = isset($size) ? $size : null;
162+
$this->size = $size ?? null;
163163
return $this->size;
164164
}
165165

166166
/**
167167
* Returns the current position of the file read/write pointer
168168
* @return int Position of the file pointer
169-
* @throws \RuntimeException on error.
169+
* @throws RuntimeException on error.
170170
*/
171171
public function tell(): int
172172
{
@@ -189,7 +189,7 @@ public function getLine(): string|bool
189189
*/
190190
public function eof(): bool
191191
{
192-
return (is_resource($this->resource)) ? feof($this->resource) : true;
192+
return !(is_resource($this->resource)) || feof($this->resource);
193193
}
194194

195195
/**
@@ -210,24 +210,24 @@ public function clean(): void
210210
* PHP $whence values for `fseek()`. SEEK_SET: Set position equal to
211211
* offset bytes SEEK_CUR: Set position to current location plus offset
212212
* SEEK_END: Set position to end-of-stream plus offset.
213-
* @throws \RuntimeException on failure.
213+
* @throws RuntimeException on failure.
214214
*/
215215
public function seek($offset, $whence = SEEK_SET): void
216216
{
217217
if ($this->isSeekable()) {
218218
fseek($this->resource, $offset, $whence);
219219
} else {
220-
throw new RuntimeException("The stream \"{$this->stream} ({$this->permission})\" is not seekable!", 1);
220+
throw new RuntimeException("The stream \"$this->stream ($this->permission)\" is not seekable!", 1);
221221
}
222222
}
223223

224224
/**
225225
* Seek to the beginning of the stream.
226226
* If the stream is not seekable, this method will raise an exception;
227227
* otherwise, it will perform a seek(0).
228+
* @throws RuntimeException on failure.
229+
*@link http://www.php.net/manual/en/function.fseek.php
228230
* @see seek()
229-
* @link http://www.php.net/manual/en/function.fseek.php
230-
* @throws \RuntimeException on failure.
231231
*/
232232
public function rewind(): void
233233
{
@@ -244,8 +244,7 @@ public function write(string $string): int
244244
if (is_null($this->size)) {
245245
$this->size = 0;
246246
}
247-
$byte = fwrite($this->resource, $string);
248-
return $byte;
247+
return fwrite($this->resource, $string);
249248
}
250249

251250
/**
@@ -264,7 +263,7 @@ public function read(int $length): string
264263
/**
265264
* Returns the remaining contents in a string
266265
* @return string
267-
* @throws \RuntimeException if unable to read or an error occurs while reading.
266+
* @throws RuntimeException if unable to read or an error occurs while reading.
268267
*/
269268
public function getContents(): string
270269
{
@@ -276,7 +275,7 @@ public function getContents(): string
276275

277276
/**
278277
* Get stream metadata as an associative array or retrieve a specific key.
279-
* @param string $key Specific metadata to retrieve.
278+
* @param string|null $key Specific metadata to retrieve.
280279
* @return array|mixed|null Returns an associative array
281280
*/
282281
public function getMetadata(?string $key = null): mixed
@@ -305,7 +304,7 @@ public function withContext(array $opts): StreamInterface
305304
* Open a resource correct with the right resource
306305
* @param ...$fArgs
307306
* @return false|resource
308-
* @throws \RuntimeException on error.
307+
* @throws RuntimeException on error.
309308
*/
310309
private function fopen(...$fArgs)
311310
{

tests/unitary-request.php

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
$unit = new MaplePHP\Unitary\Unit();
4+
5+
6+
$unit = new MaplePHP\Unitary\Unit();
7+
8+
// If you build your library right it will become very easy to mock, like I have below.
9+
$request = new MaplePHP\Http\Request(
10+
"POST", // The HTTP Method (GET, POST, PUT, DELETE, PATCH)
11+
"https://admin:[email protected]:443/test.php?id=5221&place=stockholm", // The Request URI
12+
["Content-Type" => "application/x-www-form-urlencoded"], // Add Headers, empty array is allowed
13+
["email" => "[email protected]"] // Post data
14+
);
15+
16+
// Begin by adding a test
17+
$unit->case("Checking data type", function() use($request) {
18+
19+
// Add a test, each test
20+
$this->add($request->getMethod(), function() {
21+
22+
return $this->equal("POST");
23+
24+
}, "HTTP Request method Type is not POST");
25+
26+
$this->add("ww", [
27+
"isInt" => [],
28+
"max" => [65535],
29+
], "Is not a valid port number");
30+
31+
32+
});
33+
34+
echo "ww";
35+
36+
$unit->execute();

0 commit comments

Comments
 (0)