Skip to content

Commit

Permalink
Merge pull request #29 from samsonasik/apply-php74
Browse files Browse the repository at this point in the history
Apply PHP 7.4 syntax and typed property
  • Loading branch information
Ocramius committed Jul 21, 2022
2 parents ba6ac4d + a1a0546 commit 29461a7
Show file tree
Hide file tree
Showing 23 changed files with 72 additions and 150 deletions.
7 changes: 0 additions & 7 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,6 @@
<code>$request</code>
<code>$response</code>
</MissingClosureParamType>
<MissingClosureReturnType occurrences="1">
<code>function ($request, $response, $next) {</code>
</MissingClosureReturnType>
<MixedArgument occurrences="1">
<code>$autoloader</code>
</MixedArgument>
Expand All @@ -163,10 +160,6 @@
<code>$response</code>
<code>$response</code>
</MissingClosureParamType>
<MissingClosureReturnType occurrences="2">
<code>function ($request, $response, $next) {</code>
<code>function ($request, $response, $next) {</code>
</MissingClosureReturnType>
<UnusedClosureParam occurrences="10">
<code>$next</code>
<code>$next</code>
Expand Down
3 changes: 1 addition & 2 deletions src/EmptyPipelineHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@

final class EmptyPipelineHandler implements RequestHandlerInterface
{
/** @var string */
private $className;
private string $className;

public function __construct(string $className)
{
Expand Down
4 changes: 1 addition & 3 deletions src/Handler/NotFoundHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ final class NotFoundHandler implements RequestHandlerInterface
*/
public function __construct(callable $responseFactory)
{
$this->responseFactory = function () use ($responseFactory): ResponseInterface {
return $responseFactory();
};
$this->responseFactory = static fn(): ResponseInterface => $responseFactory();
}

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

/** @var ResponseInterface */
private $responsePrototype;
private ResponseInterface $responsePrototype;

/**
* @throws Exception\MissingResponsePrototypeException If no response
Expand Down Expand Up @@ -77,8 +76,6 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface

private function decorateHandler(RequestHandlerInterface $handler): callable
{
return function ($request, $response) use ($handler) {
return $handler->handle($request);
};
return static fn($request, $response) => $handler->handle($request);
}
}
8 changes: 3 additions & 5 deletions src/Middleware/ErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
class ErrorHandler implements MiddlewareInterface
{
/** @var callable[] */
private $listeners = [];
private array $listeners = [];

/** @var callable Routine that will generate the error response. */
private $responseGenerator;
Expand All @@ -82,9 +82,7 @@ class ErrorHandler implements MiddlewareInterface
*/
public function __construct(callable $responseFactory, ?callable $responseGenerator = null)
{
$this->responseFactory = function () use ($responseFactory): ResponseInterface {
return $responseFactory();
};
$this->responseFactory = static fn(): ResponseInterface => $responseFactory();
$this->responseGenerator = $responseGenerator ?: new ErrorResponseGenerator();
}

Expand Down Expand Up @@ -163,7 +161,7 @@ private function createErrorHandler(): callable
/**
* @throws ErrorException if error is not within the error_reporting mask.
*/
return function (int $errno, string $errstr, string $errfile, int $errline): void {
return static function (int $errno, string $errstr, string $errfile, int $errline): void {
if (! (error_reporting() & $errno)) {
// error_reporting does not include this error
return;
Expand Down
3 changes: 1 addition & 2 deletions src/Middleware/ErrorResponseGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@

final class ErrorResponseGenerator
{
/** @var bool */
private $isDevelopmentMode;
private bool $isDevelopmentMode;

public function __construct(bool $isDevelopmentMode = false)
{
Expand Down
5 changes: 2 additions & 3 deletions src/Middleware/HostMiddlewareDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@

final class HostMiddlewareDecorator implements MiddlewareInterface
{
/** @var MiddlewareInterface */
private $middleware;
private MiddlewareInterface $middleware;

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

public function __construct(string $host, MiddlewareInterface $middleware)
{
Expand Down
3 changes: 1 addition & 2 deletions src/Middleware/NotFoundHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
*/
final class NotFoundHandler implements MiddlewareInterface
{
/** @var NotFoundRequestHandler */
private $notFoundHandler;
private NotFoundRequestHandler $notFoundHandler;

/**
* @param callable $responseFactory A factory capable of returning an
Expand Down
11 changes: 4 additions & 7 deletions src/Middleware/PathMiddlewareDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@

final class PathMiddlewareDecorator implements MiddlewareInterface
{
/** @var MiddlewareInterface */
private $middleware;
private MiddlewareInterface $middleware;

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

public function __construct(string $prefix, MiddlewareInterface $middleware)
{
Expand Down Expand Up @@ -96,11 +95,9 @@ private function getTruncatedPath(string $segment, string $path): string
private function prepareHandlerForOriginalRequest(RequestHandlerInterface $handler): RequestHandlerInterface
{
return new class ($handler, $this->prefix) implements RequestHandlerInterface {
/** @var RequestHandlerInterface */
private $handler;
private RequestHandlerInterface $handler;

/** @var string */
private $prefix;
private string $prefix;

public function __construct(RequestHandlerInterface $handler, string $prefix)
{
Expand Down
3 changes: 1 addition & 2 deletions src/MiddlewarePipe.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@
*/
final class MiddlewarePipe implements MiddlewarePipeInterface
{
/** @var SplQueue */
private $pipeline;
private SplQueue $pipeline;

/**
* Initializes the queue.
Expand Down
6 changes: 2 additions & 4 deletions src/Next.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@
*/
final class Next implements RequestHandlerInterface
{
/** @var RequestHandlerInterface */
private $fallbackHandler;
private RequestHandlerInterface $fallbackHandler;

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

/**
* Clones the queue provided to allow re-use.
Expand Down
4 changes: 1 addition & 3 deletions test/Handler/NotFoundHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ public function testReturnsResponseWith404StatusAndErrorMessageInBody(): void
->method('getUri')
->willReturn('https://example.com/foo');

$responseFactory = static function () use ($response): ResponseInterface {
return $response;
};
$responseFactory = static fn(): ResponseInterface => $response;

$middleware = new NotFoundHandler($responseFactory);

Expand Down
12 changes: 3 additions & 9 deletions test/Middleware/CallableMiddlewareDecoratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ public function testCallableMiddlewareThatDoesNotProduceAResponseRaisesAnExcepti
$request = $this->createMock(ServerRequestInterface::class);
$handler = $this->createMock(RequestHandlerInterface::class);

$middleware = function (): string {
return 'foo';
};
$middleware = static fn(): string => 'foo';

$decorator = new CallableMiddlewareDecorator($middleware);

Expand All @@ -37,9 +35,7 @@ public function testCallableMiddlewareReturningAResponseSucceedsProcessCall(): v
$handler = $this->createMock(RequestHandlerInterface::class);
$response = $this->createMock(ResponseInterface::class);

$middleware = function ($request, $handler) use ($response): ResponseInterface {
return $response;
};
$middleware = static fn($request, $handler): ResponseInterface => $response;

$decorator = new CallableMiddlewareDecorator($middleware);

Expand All @@ -48,9 +44,7 @@ public function testCallableMiddlewareReturningAResponseSucceedsProcessCall(): v

public function testMiddlewareFunction(): void
{
$toDecorate = static function (): string {
return 'foo';
};
$toDecorate = static fn(): string => 'foo';

$middleware = middleware($toDecorate);
self::assertInstanceOf(CallableMiddlewareDecorator::class, $middleware);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Laminas\Stratigility\Exception\MissingResponsePrototypeException;
use Laminas\Stratigility\Middleware\DoublePassMiddlewareDecorator;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;

use function class_exists;
use function spl_autoload_functions;
Expand All @@ -15,8 +16,7 @@

class DoublePassMiddlewareDecoratorExceptionTest extends TestCase
{
/** @var array */
private $autoloadFunctions = [];
private array $autoloadFunctions = [];

protected function setUp(): void
{
Expand All @@ -38,9 +38,7 @@ private function reloadAutoloaders(): void

public function testDiactorosIsNotAvailableAndResponsePrototypeIsNotSet(): void
{
$middleware = function ($request, $response, $next) {
return $response;
};
$middleware = static fn($request, ResponseInterface $response, $next): ResponseInterface => $response;

$this->expectException(MissingResponsePrototypeException::class);
$this->expectExceptionMessage(
Expand Down
23 changes: 8 additions & 15 deletions test/Middleware/DoublePassMiddlewareDecoratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ public function testCallableMiddlewareThatDoesNotProduceAResponseRaisesAnExcepti
$request = $this->createMock(ServerRequestInterface::class);
$handler = $this->createMock(RequestHandlerInterface::class);

$middleware = function ($request, $response, $next): string {
return 'foo';
};
$middleware = static fn($request, $response, $next): string => 'foo';

$decorator = new DoublePassMiddlewareDecorator($middleware, $response);

Expand All @@ -39,9 +37,7 @@ public function testCallableMiddlewareReturningAResponseSucceedsProcessCall(): v
$request = $this->createMock(ServerRequestInterface::class);
$handler = $this->createMock(RequestHandlerInterface::class);

$middleware = function ($request, $response, $next) {
return $response;
};
$middleware = static fn($request, ResponseInterface $response, $next): ResponseInterface => $response;

$decorator = new DoublePassMiddlewareDecorator($middleware, $response);

Expand All @@ -61,9 +57,10 @@ public function testCallableMiddlewareCanDelegateViaHandler(): void
->willReturn($response);

$middleware = /** @psalm-param callable(ServerRequestInterface,ResponseInterface):ResponseInterface $next */
function (ServerRequestInterface $request, ResponseInterface $response, callable $next): ResponseInterface {
return $next($request, $response);
};
static fn(
ServerRequestInterface $request,
ResponseInterface $response,
callable $next): ResponseInterface => $next($request, $response);

$decorator = new DoublePassMiddlewareDecorator($middleware, $response);

Expand All @@ -78,9 +75,7 @@ public function testDecoratorCreatesAResponsePrototypeIfNoneIsProvided(): void
$request = $this->createMock(ServerRequestInterface::class);
$handler = $this->createMock(RequestHandlerInterface::class);

$middleware = function ($request, $response, $next) {
return $response;
};
$middleware = static fn($request, ResponseInterface $response, $next): ResponseInterface => $response;

$decorator = new DoublePassMiddlewareDecorator($middleware);

Expand All @@ -91,9 +86,7 @@ public function testDecoratorCreatesAResponsePrototypeIfNoneIsProvided(): void

public function testDoublePassMiddlewareFunction(): void
{
$toDecorate = function ($request, $response, $next): string {
return 'foo';
};
$toDecorate = static fn($request, $response, $next): string => 'foo';

$response = $this->createMock(ResponseInterface::class);

Expand Down
11 changes: 4 additions & 7 deletions test/Middleware/ErrorHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,13 @@ class ErrorHandlerTest extends TestCase
/** @var callable():ResponseInterface */
private $responseFactory;

/** @var int */
private $errorReporting;
private int $errorReporting;

protected function setUp(): void
{
$this->response = $this->createMock(ResponseInterface::class);
$response = $this->response;
$this->responseFactory = static function () use ($response): ResponseInterface {
return $response;
};
$this->responseFactory = static fn(): ResponseInterface => $response;

$this->request = $this->createMock(ServerRequestInterface::class);
$this->body = $this->createMock(StreamInterface::class);
Expand Down Expand Up @@ -93,7 +90,7 @@ public function testReturnsErrorResponseIfHandlerRaisesAnErrorInTheErrorMask():
$this->handler
->method('handle')
->with($this->request)
->will(self::returnCallback(function () {
->will(self::returnCallback(static function () {
trigger_error('Deprecated', E_USER_DEPRECATED);
}));

Expand Down Expand Up @@ -278,7 +275,7 @@ public function testErrorHandlingTriggersListeners(): void

public function testCanProvideAlternateErrorResponseGenerator(): void
{
$generator = function (
$generator = static function (
Throwable $e,
ServerRequestInterface $request,
ResponseInterface $response
Expand Down
4 changes: 1 addition & 3 deletions test/Middleware/NotFoundHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ public function testReturnsResponseWith404StatusAndErrorMessageInBody(): void
->method('getUri')
->willReturn('https://example.com/foo');

$responseFactory = static function () use ($response): ResponseInterface {
return $response;
};
$responseFactory = static fn(): ResponseInterface => $response;

$middleware = new NotFoundHandler($responseFactory);

Expand Down
Loading

0 comments on commit 29461a7

Please sign in to comment.