Skip to content

Commit 428f317

Browse files
committed
Code quality improvements
1 parent 11dc744 commit 428f317

20 files changed

+77
-59
lines changed

Client.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function hasOption(int $key): bool
5353
{
5454
return (isset($this->options[$key]));
5555
}
56-
56+
5757
/**
5858
* Sends a PSR-7 request and returns a PSR-7 response.
5959
* @param RequestInterface $request
@@ -232,7 +232,7 @@ private function buildOptions(): void
232232
*/
233233
private function buildHeaders(RequestInterface $request): void
234234
{
235-
$data = array();
235+
$data = [];
236236
foreach ($request->getHeaders() as $name => $_unUsedVal) {
237237
$data[] = "{$name}: " . $request->getHeaderLine($name);
238238
}

Cookies.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function __construct(
3535
$this->secure = $secure;
3636
$this->httpOnly = $httpOnly;
3737
}
38-
38+
3939
/**
4040
* Set cookie allowed path
4141
* @param string $path URI Path

Dir.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class Dir implements DirInterface
1212
private $dir;
1313
private $getRootDir;
1414
private $handler;
15-
15+
1616

1717
public function __construct($dir, ?string $getRootDir = null)
1818
{

Env.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99

1010
class Env
1111
{
12-
private $fileData = array();
13-
private $data = array();
14-
private $set = array();
15-
private $drop = array();
12+
private $fileData = [];
13+
private $data = [];
14+
private $set = [];
15+
private $drop = [];
1616

1717
public function __construct(?string $file = null)
1818
{
@@ -65,7 +65,7 @@ public function generateOutput(array $fromArr = ["data", "fileData", "set"])
6565
{
6666
$out = "";
6767

68-
$data = array();
68+
$data = [];
6969
$validData = ["data", "fileData", "set"];
7070
foreach ($validData as $d) {
7171
if (in_array($d, $fromArr)) {

Environment.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function has($key): bool
3636
{
3737
return (bool)($this->get($key, null));
3838
}
39-
39+
4040
/**
4141
* Return all env
4242
* @return array
@@ -52,7 +52,7 @@ public function fetch(): array
5252
*/
5353
public function getUriParts(array $add = []): array
5454
{
55-
$arr = array();
55+
$arr = [];
5656
$arr['scheme'] = ($this->get("HTTPS") === 'on') ? 'https' : 'http';
5757
$arr['user'] = $this->get("PHP_AUTH_USER");
5858
$arr['pass'] = $this->get("PHP_AUTH_PW");

Headers.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
class Headers implements HeadersInterface
88
{
9-
protected $headers = array();
9+
protected $headers = [];
1010

1111
private static $getGlobalHeaders;
1212

@@ -110,7 +110,7 @@ final public static function getGlobalHeaders($skip = false): array
110110
if (!$skip && function_exists("getallheaders")) {
111111
static::$getGlobalHeaders = getallheaders();
112112
} else {
113-
static::$getGlobalHeaders = array();
113+
static::$getGlobalHeaders = [];
114114
foreach ($_SERVER as $key => $value) {
115115
if (substr($key, 0, 5) <> 'HTTP_') {
116116
continue;

Interfaces/CookiesInterface.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function sethttpOnly(bool $httpOnly): self;
3939
* @return self
4040
*/
4141
public function setSameSite(string $samesite): self;
42-
42+
4343
/**
4444
* Set cookie
4545
* @param string $name
@@ -56,12 +56,12 @@ public function set(string $name, string $value, int $expires, bool $force = fal
5656
*/
5757
public function has(string $name): bool;
5858

59-
/**
60-
* Get cookie
61-
* @param string $name
62-
* @param string|null $default
63-
* @return string|null
64-
*/
59+
/**
60+
* Get cookie
61+
* @param string $name
62+
* @param string|null $default
63+
* @return string|null
64+
*/
6565
public function get(string $name, ?string $default = null): ?string;
6666

6767
/**

Interfaces/DirHandlerInterface.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
* DirHandlerInterface
44
* Is used to extend upon the Dir instance with more dir methods
55
*/
6+
67
namespace MaplePHP\Http\Interfaces;
78

89
interface DirHandlerInterface
910
{
10-
1111
}

Interfaces/MessageInterface.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ public function withBody(StreamInterface $body);
161161
* But is used by the framework
162162
*
163163
*/
164-
164+
165165
/**
166166
* Get header value data items
167167
* @param string $name name/key (case insensitive)

Interfaces/ResponseInterface.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function getReasonPhrase();
7373
* But is used by the framework
7474
*
7575
*/
76-
76+
7777
/**
7878
* Clear cache. No exceptions!
7979
* Out of security reasons it is actually good practice to call this BY default on a framework

Interfaces/StreamInterface.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,16 @@ public function getContents();
155155
* value is found, or null if the key is not found.
156156
*/
157157
public function getMetadata(?string $key = null);
158-
158+
159159
/**
160160
* Get current resource
161161
* @return resource
162162
*/
163163
public function getResource();
164+
165+
/**
166+
* Gets line from file pointer
167+
* @return string|false
168+
*/
169+
public function getLine(): string|bool;
164170
}

Interfaces/UrlHandlerInterface.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
* UrlHandlerInterface
44
* Is used to extend upon the Url instance with more url methods
55
*/
6+
67
namespace MaplePHP\Http\Interfaces;
78

89
interface UrlHandlerInterface
910
{
10-
/**
11+
/**
1112
* Get the public dir path
1213
* @return string|null
1314
*/

Message.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public function getHeaderLine($name)
101101
*/
102102
public function getHeaderLineData(string $name): array
103103
{
104-
$this->headerLine = array();
104+
$this->headerLine = [];
105105
if ($this->hasHeader($name)) {
106106
$headerArr = $this->getHeader($name);
107107
foreach ($headerArr as $key => $val) {

Request.php

+10-12
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use MaplePHP\Http\Interfaces\UriInterface;
77
use MaplePHP\Http\Interfaces\HeadersInterface;
88
use MaplePHP\Http\Interfaces\StreamInterface;
9-
use MaplePHP\Http\Uri;
109

1110
class Request extends Message implements RequestInterface
1211
{
@@ -119,8 +118,7 @@ public function isSSL(): bool
119118
public function getPort(): int
120119
{
121120
$serverPort = $this->env->get("SERVER_PORT");
122-
$port = (int)(($serverPort) ? $serverPort : $this->uri->getPort());
123-
return $port;
121+
return (int)(($serverPort) ? $serverPort : $this->uri->getPort());
124122
}
125123

126124
/**
@@ -135,7 +133,7 @@ final protected function setHostHeader(): void
135133
}
136134

137135
/**
138-
* This will resolve the Request Stream and make the call user friendly
136+
* This will resolve the Request Stream and make the call user-friendly
139137
* @param StreamInterface|array|string|null $body
140138
* @return StreamInterface
141139
*/
@@ -156,18 +154,18 @@ private function resolveRequestStream(StreamInterface|array|string|null $body):
156154
return $stream;
157155
}
158156

159-
/**
160-
* Get Cli keyword
161-
* @return string|null
162-
*/
157+
/**
158+
* Get Cli keyword
159+
* @return string|null
160+
*/
163161
public function getCliKeyword(): ?string
164162
{
165163
if (is_null($this->cliKeywords)) {
166-
$new = array();
164+
$new = [];
167165
$arg = $this->getUri()->getArgv();
168166
foreach ($arg as $val) {
169167
if (is_string($val)) {
170-
if ((strpos($val, "--") === 0) || (strpos($val, "-") === 0)) {
168+
if ((str_starts_with($val, "--")) || (str_starts_with($val, "-"))) {
171169
break;
172170
} else {
173171
$new[] = $val;
@@ -189,11 +187,11 @@ public function getCliArgs(): array
189187
{
190188
if (is_null($this->cliArgs)) {
191189
$args = $this->getUri()->getArgv();
192-
$this->cliArgs = array();
190+
$this->cliArgs = [];
193191
foreach ($args as $arg) {
194192
if (is_string($arg)) {
195193
$arg = str_replace("&", "#", $arg);
196-
if ((($pos1 = strpos($arg, "--")) === 0) || (strpos($arg, "-") === 0)) {
194+
if ((($pos1 = strpos($arg, "--")) === 0) || (str_starts_with($arg, "-"))) {
197195
parse_str(substr($arg, ($pos1 !== false ? 2 : 1)), $result);
198196
foreach ($result as &$val) {
199197
$val = str_replace("#", "&", $val);

Response.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
class Response extends Message implements ResponseInterface
1212
{
13-
public const PHRASE = array(
13+
public const PHRASE = [
1414
100 => 'Continue',
1515
101 => 'Switching Protocols',
1616
102 => 'Processing',
@@ -71,7 +71,7 @@ class Response extends Message implements ResponseInterface
7171
507 => 'Insufficient Storage',
7272
510 => 'Not Extended',
7373
511 => 'Network Authentication Required',
74-
);
74+
];
7575

7676
private $statusCode = 200;
7777
private $phrase;

ServerRequest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
class ServerRequest extends Request implements ServerRequestInterface
1111
{
12-
protected $attr = array();
12+
protected $attr = [];
1313
protected $env;
1414
protected $queryParams;
1515
protected $parsedBody;

Uri.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class Uri implements UriInterface
2323
'ldap' => 389,
2424
];
2525

26-
private $parts = array();
26+
private $parts = [];
2727
private $scheme;
2828
//private $uri;
2929
private $host;
@@ -214,7 +214,9 @@ public function getPath(): string
214214
{
215215
if ($val = $this->getUniquePart("path")) {
216216
$this->encoded['path'] = Format\Str::value($val)->toggleUrlencode(['%2F'], ['/'])->get();
217-
if($this->encoded['path']) $this->encoded['path'] = "/".ltrim($this->encoded['path'], "/");
217+
if($this->encoded['path']) {
218+
$this->encoded['path'] = "/".ltrim($this->encoded['path'], "/");
219+
}
218220
}
219221
return (string)$this->encoded['path'];
220222
}

Url.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function withType(null|string|array $type = null): self
6161

6262

6363
$inst = clone $this;
64-
$parts = $vars = array();
64+
$parts = $vars = [];
6565
foreach ($inst->parts as $sel => $row) {
6666
if (in_array($sel, $type)) {
6767
if (is_array($row)) {
@@ -158,7 +158,7 @@ public function getRealPath(): string
158158
/**
159159
* Extract and get directories from the simulated htaccess path
160160
* @return string
161-
*/
161+
*/
162162
public function getDirPath(): string
163163
{
164164
if (is_null($this->dirPath)) {
@@ -282,7 +282,7 @@ public function getRootDir(string $path = "", bool $endSlash = false): string
282282
if ($host = $this->getHost()) {
283283
$url .= "//{$host}";
284284
}
285-
285+
286286
// Do not show standard ports becouse they are not needed.
287287
if (($port = $this->getPort()) && $port !== 80 && $port !== 443) {
288288
$url .= ":{$port}";
@@ -348,7 +348,7 @@ public function __call($method, $args): mixed
348348
{
349349
if (!is_null($this->handler) && method_exists($this->handler, $method)) {
350350
return call_user_func_array([$this->handler, $method], $args);
351-
} else if (method_exists($this->uri, $method)) {
351+
} elseif (method_exists($this->uri, $method)) {
352352
return call_user_func_array([$this->uri, $method], $args);
353353
} else {
354354
throw new \BadMethodCallException("The method ({$method}) does not exist in \"".__CLASS__."\" (UrlInterface or UriInterface).", 1);

composer.json

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"name": "maplephp/http",
3+
"version": "v1.0.6",
34
"type": "library",
45
"description": "The library is fully integrated with PSR-7 Http Message and designed for use with MaplePHP framework.",
56
"keywords": ["psr7", "http", "message", "http message", "server", "request", "client", "upload", "stream", "headers"],
@@ -19,6 +20,9 @@
1920
"php": ">=8.0",
2021
"maplephp/dto": "^1.0"
2122
},
23+
"require-dev": {
24+
"maplephp/unitary": "^1.0"
25+
},
2226
"autoload": {
2327
"psr-4": {
2428
"MaplePHP\\Http\\": ""

0 commit comments

Comments
 (0)