Skip to content

Commit

Permalink
Refactor and update dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagodp committed Dec 1, 2023
1 parent 9317d28 commit 5dbbb76
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 85 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@
"test": "kahlan",
"test2": "kahlan --reporter=verbose",
"cov": "kahlan --coverage",
"check": "phpstan analyze src --level=3"
"lint": "phpstan analyze src --level=4"
}
}
25 changes: 13 additions & 12 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions src/HttpRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function queries(): array;
function headers(): array;

/** Returns the header with the given case-insensitive name, or `null` if not found. */
function header( $name ): ?string;
function header( string $name ): ?string;

/** Returns the raw body or `null` on failure. */
function rawBody(): ?string;
Expand All @@ -44,7 +44,7 @@ function cookies(): array;
* @param string $key Cookie key.
* @return string|null
*/
function cookie( $key ): ?string;
function cookie( string $key ): ?string;

/**
* Returns a URL query or route parameter with the given name (key),
Expand All @@ -53,7 +53,7 @@ function cookie( $key ): ?string;
* @param string $name Parameter name.
* @return string
*/
function param( $name ): ?string;
function param( string $name ): ?string;

/**
* Returns all the URL queries and route parameters as an array (map).
Expand Down
28 changes: 14 additions & 14 deletions src/HttpResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ interface HttpResponse {
/**
* Sets the HTTP status code.
*
* @param int code HTTP status code.
* @param int $code HTTP status code.
* @return HttpResponse
*/
function status( int $code ): HttpResponse;
Expand All @@ -23,18 +23,18 @@ function header( string $header, $value ): HttpResponse;
/**
* Sets a redirect response.
*
* @param int statusCode HTTP status code.
* @param string|null path Path.
* @param int $statusCode HTTP status code.
* @param string|null $path Path.
* @return HttpResponse
*/
function redirect( int $statusCode, $path = null ): HttpResponse;

/**
* Sets a cookie.
*
* @param string name Name (key)
* @param string value Value.
* @param array options Optional map with the following options:
* @param string $name Name (key)
* @param string $value Value.
* @param array $options Optional map with the following options:
* - `domain`: string
* - `path`: string
* - `httpOnly`: true|1
Expand All @@ -51,33 +51,33 @@ function cookie( string $name, string $value, array $options = [] ): HttpRespons
/**
* Clears a cookie with the given name (key).
*
* @param string name Name (key)
* @param array options Optional map with the same options as #cookie()'s.
* @param string $name Name (key)
* @param array $options Optional map with the same options as #cookie()'s.
* @return HttpResponse
*/
function clearCookie( string $name, array $options = [] ): HttpResponse;

/**
* Sets the `Content-Type` header with the given MIME type.
*
* @param string mime MIME type.
* @param string $mime MIME type.
* @return HttpResponse
*/
function type( string $mime ): HttpResponse;

/**
* Sends the given HTTP response body.
*
* @param mixed body Response body.
* @param mixed $body Response body.
* @return HttpResponse
*/
function send( $body ): HttpResponse;

/**
* Sends a file based on its path.
*
* @param string path File path
* @param array options Optional map with the options:
* @param string $path File path
* @param array $options Optional map with the options:
* - `mime`: string - MIME type, such as `application/pdf`.
* @return HttpResponse
*/
Expand All @@ -86,15 +86,15 @@ function sendFile( string $path, array $options = [] ): HttpResponse;
/**
* Send the given content as JSON, also setting the needed headers.
*
* @param mixed body Content to send as JSON.
* @param mixed $body Content to send as JSON.
* @return HttpResponse
*/
function json( $body ): HttpResponse;

/**
* Ends the HTTP response.
*
* @param bool clear If it is desired to clear the headers and the body after sending them. It defaults to `true`.
* @param bool $clear If it is desired to clear the headers and the body after sending them. It defaults to `true`.
*/
function end( bool $clear = true ): HttpResponse;
}
Expand Down
4 changes: 2 additions & 2 deletions src/MiddlewareEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

class MiddlewareEntry implements Entry {

/** @var callback Callback like function ( $req, $res, &$stop ) */
/** @var callable Callback like function ( $req, $res, &$stop ) */
public $callback = null;

public function __construct( $callback ) {
public function __construct( callable $callback ) {
$this->callback = $callback;
}

Expand Down
52 changes: 16 additions & 36 deletions src/RealHttpRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,13 @@
*/
class RealHttpRequest implements HttpRequest {

private $_cookies = null;
private $_cookies = [];
private $_params = [];
private $_extra = null;

/** @inheritDoc */
function url(): ?string {
if ( ! isset( $_SERVER, $_SERVER[ 'REQUEST_URI' ] ) ) {
return null;
}
return $_SERVER[ 'REQUEST_URI' ];
return $_SERVER[ 'REQUEST_URI' ] ?? null;
}

/** @inheritDoc */
Expand All @@ -32,31 +29,22 @@ function urlWithoutQueries(): ?string {

/** @inheritDoc */
function queries(): array {
if ( ! isset( $_GET ) ) {
return [];
}
return $_GET;
}

/** @inheritDoc */
function headers(): array {
if ( ! isset( $_SERVER ) ) {
return [];
}
return extractHeaders( $_SERVER );
}

/** @inheritDoc */
function header( $name ): ?string {
if ( ! isset( $_SERVER ) ) {
return null;
}
function header( string $name ): ?string {
return headerWithName( $name, $_SERVER );
}

/** @inheritDoc */
function rawBody(): ?string {
$content = file_get_contents( 'php://input' );
$content = @file_get_contents( 'php://input' );
return $content === false ? null : $content;
}

Expand All @@ -67,39 +55,31 @@ function body() {

/** @inheritDoc */
function method(): ?string {
if ( ! isset( $_SERVER, $_SERVER[ 'REQUEST_METHOD' ] ) ) {
return null;
}
return $_SERVER[ 'REQUEST_METHOD' ];
return $_SERVER[ 'REQUEST_METHOD' ] ?? null;
}

/** @inheritDoc */
function cookies(): array {
if ( isset( $_COOKIE ) ) {
return $_COOKIE;
}
if ( ! isset( $this->_cookies ) ) {
$this->_cookies = extractCookies( $this->headers() );
}
return $this->_cookies;

return $_COOKIE;
}

/** @inheritDoc */
function cookie( $key ): ?string {
$cookies = $this->cookies();
return isset( $cookies[ $key ] ) ? $cookies[ $key ] : null;
function cookie( string $key ): ?string {
if ( isset( $_COOKIE[ $key ] ) ) {
return $_COOKIE[ $key ];
}
if ( empty( $this->_cookies ) ) {
$this->_cookies = extractCookies( $this->headers() );
}
return $this->_cookies[ $key ] ?? null;
}

/** @inheritDoc */
function param( $name ): ?string {
function param( string $name ): ?string {
if ( isset( $_GET[ $name ] ) ) {
return urldecode( $_GET[ $name ] );
}
if ( isset( $this->_params[ $name ] ) ) {
return $this->_params[ $name ];
}
return null;
return $this->_params[ $name ] ?? null;
}

/** @inheritDoc */
Expand Down
2 changes: 1 addition & 1 deletion src/RealHttpResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ function sendFile( $path, array $options = [] ): HttpResponse {

$mime = isset( $options[ 'mime' ] )
? $options[ 'mime' ]
: ( isset( $headers[ 'Content-Type' ] ) ? $headers[ 'Content-Type' ] : getFileMime( $path ) );
: ( isset( $this->headers[ 'Content-Type' ] ) ? $this->headers[ 'Content-Type' ] : getFileMime( $path ) );

if ( ! isset( $mime ) ) {
throw new RuntimeException( 'MIME type could not be defined. Please inform it.' );
Expand Down
22 changes: 7 additions & 15 deletions src/form.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,9 @@ function extractFormDataAndFiles( $httpMethod, $contentType ) {

// HTTP POST: $_POST and $_FILES works for both "application/x-www-form-urlencoded" and "multipart/form-data"
if ( $httpMethod === 'POST' ) {
$hasPost = isset( $_POST );
$hasFiles = isset( $_FILES );
if ( $hasPost ) {
$result->data = $_POST; // copy
}
if ( $hasFiles ) {
$result->files = $_FILES; // copy
}
if ( $hasPost && $hasFiles ) {
return $result;
}
$result->data = $_POST; // copy
$result->files = $_FILES; // copy
return $result;
}

$content = @file_get_contents( 'php://input' );
Expand Down Expand Up @@ -225,12 +217,12 @@ function handleAsHeader( Context &$current, $line ) {
} else if ( mb_stripos( 'Content-Type', $line ) === 0 ) {
$current->lastLineWasHeader = true;
$matches = [];
preg_match( '/^Content\-Type\: ([a-z -\/]+)(?:; boundary=)?([a-zA-Z0-9]+)?$/i', $line, $matches );
if ( ! preg_match( '/^Content\-Type\: ([a-z -\/]+)(?:; boundary=)?([a-zA-Z0-9]+)?$/i', $line, $matches ) ) {
return false;
}
list( , $contentType, $boundary ) = $matches;
$current->contentType = $contentType;
if ( isset( $boundary ) ) {
$current->boundary = $boundary;
}
$current->boundary = $boundary;
return true;
}
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/request.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function extractHeaders( array &$array ) {
}


function headerWithName( $name, array $array ) {
function headerWithName( $name, array $array ): ?string {
if ( isset( $array[ $name ] ) ) {
return $array[ $name ];
}
Expand Down

0 comments on commit 5dbbb76

Please sign in to comment.