Skip to content

Commit

Permalink
Update to latest PHP 8.1 syntax
Browse files Browse the repository at this point in the history
Signed-off-by: Abdul Malik Ikhsan <[email protected]>
  • Loading branch information
samsonasik committed Nov 2, 2024
1 parent 79f9302 commit edb3930
Show file tree
Hide file tree
Showing 13 changed files with 36 additions and 77 deletions.
5 changes: 1 addition & 4 deletions src/EmptyPipelineHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@

final class EmptyPipelineHandler implements RequestHandlerInterface
{
private string $className;

public function __construct(string $className)
public function __construct(private readonly string $className)
{
$this->className = $className;
}

public function handle(ServerRequestInterface $request): ResponseInterface
Expand Down
7 changes: 2 additions & 5 deletions src/Exception/MissingResponseException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@

use OutOfBoundsException;

use function gettype;
use function is_object;
use function get_debug_type;
use function sprintf;

/**
Expand All @@ -18,9 +17,7 @@ class MissingResponseException extends OutOfBoundsException implements Exception
{
public static function forCallableMiddleware(callable $middleware): self
{
$type = is_object($middleware)
? $middleware::class
: gettype($middleware);
$type = get_debug_type($middleware);

return new self(sprintf(
'Decorated callable middleware of type %s failed to produce a response.',
Expand Down
5 changes: 1 addition & 4 deletions src/Handler/NotFoundHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,8 @@

final class NotFoundHandler implements RequestHandlerInterface
{
private ResponseFactoryInterface $responseFactory;

public function __construct(ResponseFactoryInterface $responseFactory)
public function __construct(private readonly ResponseFactoryInterface $responseFactory)
{
$this->responseFactory = $responseFactory;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Middleware/DoublePassMiddlewareDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ final class DoublePassMiddlewareDecorator implements MiddlewareInterface
/** @var callable */
private $middleware;

private ResponseInterface $responsePrototype;
private readonly ResponseInterface $responsePrototype;

/**
* @throws Exception\MissingResponsePrototypeException If no response
Expand Down
9 changes: 4 additions & 5 deletions src/Middleware/ErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,14 @@ class ErrorHandler implements MiddlewareInterface
/** @var callable Routine that will generate the error response. */
private $responseGenerator;

private ResponseFactoryInterface $responseFactory;

/**
* @param null|callable $responseGenerator Callback that will generate the final
* error response; if none is provided, ErrorResponseGenerator is used.
*/
public function __construct(ResponseFactoryInterface $responseFactory, ?callable $responseGenerator = null)
{
$this->responseFactory = $responseFactory;
public function __construct(
private readonly ResponseFactoryInterface $responseFactory,
?callable $responseGenerator = null
) {
$this->responseGenerator = $responseGenerator ?? new ErrorResponseGenerator();
}

Expand Down
5 changes: 1 addition & 4 deletions src/Middleware/ErrorResponseGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,8 @@

final class ErrorResponseGenerator
{
private bool $isDevelopmentMode;

public function __construct(bool $isDevelopmentMode = false)
public function __construct(private readonly bool $isDevelopmentMode = false)
{
$this->isDevelopmentMode = $isDevelopmentMode;
}

/**
Expand Down
14 changes: 5 additions & 9 deletions src/Middleware/HostMiddlewareDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,11 @@

final class HostMiddlewareDecorator implements MiddlewareInterface
{
private MiddlewareInterface $middleware;

/** @var string Host name under which the middleware is segregated. */
private string $host;

public function __construct(string $host, MiddlewareInterface $middleware)
{
$this->host = $host;
$this->middleware = $middleware;
public function __construct(
/** @var string Host name under which the middleware is segregated. */
private readonly string $host,
private readonly MiddlewareInterface $middleware
) {
}

public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
Expand Down
25 changes: 9 additions & 16 deletions src/Middleware/PathMiddlewareDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,19 @@
use Psr\Http\Server\RequestHandlerInterface;

use function rtrim;
use function str_starts_with;
use function stripos;
use function strlen;
use function strpos;
use function substr;

final class PathMiddlewareDecorator implements MiddlewareInterface
{
private MiddlewareInterface $middleware;

/** @var string Path prefix under which the middleware is segregated. */
private string $prefix;
private readonly string $prefix;

public function __construct(string $prefix, MiddlewareInterface $middleware)
public function __construct(string $prefix, private readonly MiddlewareInterface $middleware)
{
$this->prefix = $this->normalizePrefix($prefix);
$this->middleware = $middleware;
$this->prefix = $this->normalizePrefix($prefix);
}

public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
Expand Down Expand Up @@ -95,14 +92,10 @@ private function getTruncatedPath(string $segment, string $path): string
private function prepareHandlerForOriginalRequest(RequestHandlerInterface $handler): RequestHandlerInterface
{
return new class ($handler, $this->prefix) implements RequestHandlerInterface {
private RequestHandlerInterface $handler;

private string $prefix;

public function __construct(RequestHandlerInterface $handler, string $prefix)
{
$this->handler = $handler;
$this->prefix = $prefix;
public function __construct(
private readonly RequestHandlerInterface $handler,
private readonly string $prefix
) {
}

/**
Expand Down Expand Up @@ -131,7 +124,7 @@ public function handle(ServerRequestInterface $request): ResponseInterface
private function normalizePrefix(string $prefix): string
{
$prefix = strlen($prefix) > 1 ? rtrim($prefix, '/') : $prefix;
if (0 !== strpos($prefix, '/')) {
if (! str_starts_with($prefix, '/')) {
$prefix = '/' . $prefix;
}
return $prefix;
Expand Down
10 changes: 4 additions & 6 deletions src/Middleware/RequestHandlerMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,10 @@
*/
final class RequestHandlerMiddleware implements MiddlewareInterface, RequestHandlerInterface
{
/** @var RequestHandlerInterface Decorated handler to invoke. */
private $handler;

public function __construct(RequestHandlerInterface $handler)
{
$this->handler = $handler;
public function __construct(
/** @var RequestHandlerInterface Decorated handler to invoke. */
private readonly RequestHandlerInterface $handler
) {
}

/**
Expand Down
7 changes: 2 additions & 5 deletions src/Next.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
*/
final class Next implements RequestHandlerInterface
{
private RequestHandlerInterface $fallbackHandler;

/** @var SplQueue<MiddlewareInterface>|null */
private ?SplQueue $queue;

Expand All @@ -28,10 +26,9 @@ final class Next implements RequestHandlerInterface
* @param RequestHandlerInterface $fallbackHandler Fallback handler to
* invoke when the queue is exhausted.
*/
public function __construct(SplQueue $queue, RequestHandlerInterface $fallbackHandler)
public function __construct(SplQueue $queue, private RequestHandlerInterface $fallbackHandler)
{
$this->queue = clone $queue;
$this->fallbackHandler = $fallbackHandler;
$this->queue = clone $queue;
}

public function handle(ServerRequestInterface $request): ResponseInterface
Expand Down
5 changes: 1 addition & 4 deletions test/Middleware/PathMiddlewareDecoratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,11 +262,8 @@ public function process(
});

$topLevel = new PathMiddlewareDecorator($prefix, new class ($nested) implements MiddlewareInterface {
private MiddlewareInterface $middleware;

public function __construct(MiddlewareInterface $middleware)
public function __construct(private readonly MiddlewareInterface $middleware)
{
$this->middleware = $middleware;
}

public function process(
Expand Down
4 changes: 2 additions & 2 deletions test/MiddlewarePipeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
use function iterator_to_array;
use function sort;
use function spl_object_hash;
use function strpos;
use function str_starts_with;
use function var_export;

class MiddlewarePipeTest extends TestCase
Expand Down Expand Up @@ -208,7 +208,7 @@ public function testMiddlewarePipeOnlyImplementsMiddlewarePipeInterfaceApi(): vo
$methods = $r->getMethods(ReflectionMethod::IS_PUBLIC);
$actual = [];
foreach ($methods as $method) {
if (strpos($method->getName(), '__') !== 0) {
if (! str_starts_with($method->getName(), '__')) {
$actual[] = $method->getName();
}
}
Expand Down
15 changes: 3 additions & 12 deletions test/NextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,8 @@ public function createFallbackHandler(?ResponseInterface $response = null): Requ
{
$response = $response ?: $this->createDefaultResponse();
return new class ($response) implements RequestHandlerInterface {
private ResponseInterface $response;

public function __construct(ResponseInterface $response)
public function __construct(private readonly ResponseInterface $response)
{
$this->response = $response;
}

public function handle(ServerRequestInterface $request): ResponseInterface
Expand Down Expand Up @@ -82,11 +79,8 @@ public function testMiddlewareCallingNextWithRequestPassesRequestToNextMiddlewar

$middleware1 = new class ($cannedRequest) implements MiddlewareInterface
{
private ServerRequestInterface $cannedRequest;

public function __construct(ServerRequestInterface $cannedRequest)
public function __construct(private readonly ServerRequestInterface $cannedRequest)
{
$this->cannedRequest = $cannedRequest;
}

public function process(
Expand All @@ -99,11 +93,8 @@ public function process(

$middleware2 = new class ($cannedRequest) implements MiddlewareInterface
{
private ServerRequestInterface $cannedRequest;

public function __construct(ServerRequestInterface $cannedRequest)
public function __construct(private readonly ServerRequestInterface $cannedRequest)
{
$this->cannedRequest = $cannedRequest;
}

public function process(
Expand Down

0 comments on commit edb3930

Please sign in to comment.