Skip to content

Code quality improvements #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ protected function prepareRequest(RequestInterface $request): void
$this->setOption(CURLOPT_RETURNTRANSFER, true);

// Default auth option if get user name
if (!$this->hasOption(CURLOPT_HTTPAUTH) && !is_null($request->getUri()->getPart("user"))) {
if (!$this->hasOption(CURLOPT_HTTPAUTH) && $request->getUri()->getPart("user") !== null) {
$this->setOption(CURLOPT_HTTPAUTH, static::DEFAULT_AUTH);
}

Expand Down
4 changes: 2 additions & 2 deletions Dir.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function getRoot(string $path = ""): string
*/
public function getLogs(string $path = ""): string
{
if(!is_null($this->handler)) {
if ($this->handler !== null) {
return $this->handler->getLogs($path);
}
return $this->getRoot("storage/logs/" . $path);
Expand All @@ -72,7 +72,7 @@ public function getLogs(string $path = ""): string
*/
public function __call($method, $args): mixed
{
if (!is_null($this->handler) && method_exists($this->handler, $method)) {
if ($this->handler !== null && method_exists($this->handler, $method)) {
return call_user_func_array([$this->handler, $method], $args);
} else {
throw new \BadMethodCallException("The method ({$method}) does not exist in \"".__CLASS__."\" (DirInterface or DirHandlerInterface).", 1);
Expand Down
2 changes: 1 addition & 1 deletion Env.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Env

public function __construct(?string $file = null)
{
if (!is_null($file) && is_file($file)) {
if ($file !== null && is_file($file)) {
$this->loadEnvFile($file);
}
}
Expand Down
2 changes: 1 addition & 1 deletion Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function getUriParts(array $add = []): array
*/
public function getPath(): string
{
if (is_null($this->path)) {
if ($this->path === null) {
$basePath = '';
$requestName = Format\Str::value($this->get("SCRIPT_NAME"))->getUrlPath()->get();
$requestDir = dirname($requestName);
Expand Down
2 changes: 1 addition & 1 deletion Headers.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public function normalizeKey(string $key, bool $preserveCase = false): string
*/
final public static function getGlobalHeaders($skip = false): array
{
//if(is_null(static::$getGlobalHeaders)) {
//if(static::$getGlobalHeaders === null) {
if (!$skip && function_exists("getallheaders")) {
static::$getGlobalHeaders = getallheaders();
} else {
Expand Down
1 change: 1 addition & 0 deletions Interfaces/DirHandlerInterface.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* DirHandlerInterface
* Is used to extend upon the Dir instance with more dir methods
Expand Down
1 change: 1 addition & 0 deletions Interfaces/UrlHandlerInterface.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* UrlHandlerInterface
* Is used to extend upon the Url instance with more url methods
Expand Down
4 changes: 2 additions & 2 deletions Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ abstract class Message implements MessageInterface
*/
public function getProtocolVersion()
{
if (is_null($this->version)) {
if ($this->version === null) {
$prot = explode("/", ($this->env['SERVER_PROTOCOL'] ?? "HTTP/1.1"));
$this->version = end($prot);
}
Expand Down Expand Up @@ -72,7 +72,7 @@ public function getHeader($name): array
*/
public function hasHeader($name): bool
{
if (is_null($this->headers)) {
if ($this->headers === null) {
throw new RequestException("Missing The HTTP Headers instance", 1);
}
return $this->headers->hasHeader($name);
Expand Down
13 changes: 8 additions & 5 deletions Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ public function __construct(
$this->uri = is_string($uri) ? new Uri($uri) : $uri;
$this->headers = is_array($headers) ? new Headers($headers) : $headers;
$this->body = $this->resolveRequestStream($body);
if ($this->env === null) {
$this->env = new Environment();
}
$this->setHostHeader();
}

Expand Down Expand Up @@ -118,11 +121,11 @@ public function isSSL(): bool
public function getPort(): int
{
$serverPort = $this->env->get("SERVER_PORT");
return (int)(($serverPort) ? $serverPort : $this->uri->getPort());
return (int)(((int)$serverPort > 0) ? $serverPort : $this->uri->getPort());
}

/**
* Set host header if missing or overwrite if custom is set.
* Set the host header if missing or overwrite if custom is set.
* @return void
*/
final protected function setHostHeader(): void
Expand All @@ -146,7 +149,7 @@ private function resolveRequestStream(StreamInterface|array|string|null $body):
$body = http_build_query($body);
}
$stream = new Stream(Stream::TEMP);
if (!is_null($body)) {
if ($body !== null) {
$stream->write($body);
$stream->rewind();
}
Expand All @@ -160,7 +163,7 @@ private function resolveRequestStream(StreamInterface|array|string|null $body):
*/
public function getCliKeyword(): ?string
{
if (is_null($this->cliKeywords)) {
if ($this->cliKeywords === null) {
$new = [];
$arg = $this->getUri()->getArgv();
foreach ($arg as $val) {
Expand All @@ -185,7 +188,7 @@ public function getCliKeyword(): ?string
*/
public function getCliArgs(): array
{
if (is_null($this->cliArgs)) {
if ($this->cliArgs === null) {
$args = $this->getUri()->getArgv();
$this->cliArgs = [];
foreach ($args as $arg) {
Expand Down
16 changes: 8 additions & 8 deletions Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,12 @@ public function __construct(
) {
$this->body = $body;
$this->statusCode = $status;
$this->headers = is_null($headers) ? new Headers() : $headers;
$this->body = $body;
if (!is_null($version)) {
$this->headers = $headers === null ? new Headers() : $headers;
//$this->body = $body;
if ($version !== null) {
$this->version = $version;
}
if (!is_null($phrase)) {
if ($phrase !== null) {
$this->phrase = $phrase;
}
}
Expand All @@ -116,7 +116,7 @@ public function withStatus(int $code, string $reasonPhrase = ''): ResponseInterf
* Get current response status code
* @return int
*/
public function getStatusCode()
public function getStatusCode(): int
{
return $this->statusCode;
}
Expand All @@ -125,9 +125,9 @@ public function getStatusCode()
* Get current response status phrase
* @return string
*/
public function getReasonPhrase()
public function getReasonPhrase(): string
{
if (is_null($this->phrase)) {
if ($this->phrase === null) {
$this->phrase = ($this::PHRASE[$this->statusCode] ?? "");
}
return $this->phrase;
Expand Down Expand Up @@ -230,7 +230,7 @@ public function location(string $url, int $statusCode = 302): void
*/
public function createHeaders(): void
{
if (is_null($this->hasHeadersInit)) {
if ($this->hasHeadersInit === null) {
$this->hasHeadersInit = true;
foreach ($this->getHeaders() as $key => $_unusedVal) {
$value = $this->getHeaderLine($key);
Expand Down
4 changes: 2 additions & 2 deletions ServerRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public function withCookieParams(array $cookies): self
*/
public function getQueryParams(): array
{
if (is_null($this->queryParams)) {
if ($this->queryParams === null) {
parse_str($this->getUri()->getQuery(), $this->queryParams);
}
return $this->queryParams;
Expand Down Expand Up @@ -188,7 +188,7 @@ public function withUploadedFiles(array $uploadedFiles): self
*/
public function getParsedBody(): null|array|object
{
if (is_null($this->parsedBody) && $this->getMethod() === "POST") {
if ($this->parsedBody === null && $this->getMethod() === "POST") {
$header = $this->getHeader('Content-Type');
$contents = (string)$this->getBody();
switch (($header[0] ?? null)) {
Expand Down
10 changes: 5 additions & 5 deletions Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ class Stream implements StreamInterface
*/
public function __construct(mixed $stream = null, string $permission = "r+")
{
if (is_null($stream)) {
if ($stream === null) {
$stream = $this::DEFAULT_WRAPPER;
}

if (is_resource($stream)) {
$this->resource = $stream;
$this->meta = $this->getMetadata();
/*
if (is_null($this->meta)) {
if ($this->meta === null) {
throw new RuntimeException("Could not access the stream metadata.", 1);
}
*/
Expand Down Expand Up @@ -147,7 +147,7 @@ public function stats(?string $key = null): mixed
{
$stats = fstat($this->resource);
if (is_array($stats)) {
return is_null($key) ? $stats : ($stats[$key] ?? false);
return $key === null ? $stats : ($stats[$key] ?? false);
}
return false;
}
Expand Down Expand Up @@ -241,7 +241,7 @@ public function rewind(): void
*/
public function write(string $string): int
{
if (is_null($this->size)) {
if ($this->size === null) {
$this->size = 0;
}
return fwrite($this->resource, $string);
Expand Down Expand Up @@ -284,7 +284,7 @@ public function getMetadata(?string $key = null): mixed
$this->readable = (bool)preg_match(self::READABLE_MATCH, $this->meta['mode']);
$this->writable = (bool)preg_match(self::WRITABLE_MATCH, $this->meta['mode']);
$this->seekable = $this->meta['seekable'];
return (!is_null($key) ? ($this->meta[$key] ?? null) : $this->meta);
return ($key !== null ? ($this->meta[$key] ?? null) : $this->meta);
}

/**
Expand Down
10 changes: 5 additions & 5 deletions UploadedFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class UploadedFile implements UploadedFileInterface
public function __construct(StreamInterface|array|string $stream, mixed ...$vars)
{

if(count($vars) > 0 && is_string($stream)) {
if (count($vars) > 0 && is_string($stream)) {
array_unshift($vars, $stream);
$stream = array_combine(['name', 'type', 'tmp_name', 'error', 'size'], $vars);
}
Expand All @@ -68,7 +68,7 @@ public function __construct(StreamInterface|array|string $stream, mixed ...$vars
*/
public function getStream(): StreamInterface
{
if (is_null($this->stream)) {
if ($this->stream === null) {
throw new RuntimeException("The no stream exists. You need to construct a new stream", 1);
}
if (is_string($this->stream)) {
Expand All @@ -91,9 +91,9 @@ public function moveTo($targetPath): void
throw new RuntimeException('Target directory is not writable');
}

if (!is_null($this->stream)) {
if ($this->stream !== null) {
$this->streamFile($targetPath);
} elseif (!is_null($this->tmp)) {
} elseif ($this->tmp !== null) {
$this->moveUploadedFile($targetPath);
}

Expand Down Expand Up @@ -148,7 +148,7 @@ public function moveUploadedFile(string $targetPath)
*/
public function getSize(): ?int
{
return (is_null($this->size)) ? (($this->stream instanceof StreamInterface) ? $this->stream->getSize() : null) : $this->size;
return ($this->size === null) ? (($this->stream instanceof StreamInterface) ? $this->stream->getSize() : null) : $this->size;
}

/**
Expand Down
22 changes: 10 additions & 12 deletions Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,13 @@ class Uri implements UriInterface

private $parts = [];
private $scheme;
//private $uri;
private $host;
private $port;
private $user;
private $pass;
private $path;
private $query;
private $fragment; // Anchor/after hash
private $fragment;
private $dir;
private $rootDir;
private $userInfo;
Expand All @@ -41,7 +40,6 @@ class Uri implements UriInterface
private $encoded;
private $build;


/**
* URI in parts
* @param array|string $uri
Expand Down Expand Up @@ -122,7 +120,7 @@ public function getRootDir(): string
*/
public function getAuthority(): string
{
if (is_null($this->authority)) {
if ($this->authority === null) {
$this->authority = "";

if (($host = $this->getHost()) && ($userInfo = $this->getUserInfo())) {
Expand All @@ -143,7 +141,7 @@ public function getAuthority(): string
*/
public function getUserInfo(): string
{
if (is_null($this->userInfo)) {
if ($this->userInfo === null) {
$this->userInfo = "";
if ($user = $this->getUniquePart("user")) {
$this->encoded['user'] = $user;
Expand All @@ -154,7 +152,7 @@ public function getUserInfo(): string

if (is_string($user) && !empty($user)) {
$this->userInfo .= "{$user}";
if (!is_null($pass)) {
if ($pass !== null) {
$this->userInfo .= ":{$pass}";
}
}
Expand All @@ -169,7 +167,7 @@ public function getUserInfo(): string
public function getHost(): string
{
if ($val = $this->getUniquePart("host")) {
if(($pos = strpos($val, ":")) !== false) {
if (($pos = strpos($val, ":")) !== false) {
$val = substr($val, 0, $pos);
}
$this->encoded['host'] = Format\Str::value($val)->tolower()->get();
Expand Down Expand Up @@ -197,7 +195,7 @@ public function getPort(): ?int
*/
public function getDefaultPort(): ?int
{
if (is_null($this->port) && !is_null($this->scheme)) {
if ($this->port === null && $this->scheme !== null) {
$this->port = ($this::DEFAULT_PORTS[$this->getScheme()] ?? null);
}
if ($val = $this->getUniquePart("port")) {
Expand All @@ -217,7 +215,7 @@ public function getPath(): string
->normalizeUrlEncoding()
->replace(['%2F'], ['/'])
->get();
if($this->encoded['path']) {
if ($this->encoded['path']) {
$this->encoded['path'] = "/".ltrim($this->encoded['path'], "/");
}
}
Expand Down Expand Up @@ -257,7 +255,7 @@ public function getFragment(): string
*/
public function getUri(): string
{
if (is_null($this->build)) {
if ($this->build === null) {
$this->build = "";
if ($scheme = $this->getScheme()) {
$this->build .= "{$scheme}:";
Expand Down Expand Up @@ -393,7 +391,7 @@ public function withUriParts(array $parts): self
*/
private function getUniquePart(string $key): string|int|float|null
{
return (!is_null($this->{$key}) && is_null($this->encoded[$key])) ? $this->{$key} : null;
return ($this->{$key} !== null && $this->encoded[$key] === null) ? $this->{$key} : null;
}

/**
Expand All @@ -416,7 +414,7 @@ private function fillParts(): void
foreach ($vars as $key => $_valueNotUsed) {
$this->encoded[$key] = null;
$part = ($this->parts[$key] ?? null);
if (!is_null($part)) {
if ($part !== null) {
$this->{$key} = $part;
}
}
Expand Down
Loading