Skip to content

Commit

Permalink
Merge pull request #24 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 authored Sep 14, 2022
2 parents d18d751 + 7eb6302 commit b460d1d
Show file tree
Hide file tree
Showing 17 changed files with 58 additions and 68 deletions.
4 changes: 2 additions & 2 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@
</MissingClosureParamType>
<MissingClosureReturnType occurrences="3">
<code>function ($args) {</code>
<code>function ($container) use ($originalContainer) {</code>
<code>function ($container) use ($originalContainer, $routeResult) {</code>
<code>static function ($container) use ($originalContainer) {</code>
<code>static function ($container) use ($originalContainer, $routeResult) {</code>
</MissingClosureReturnType>
<MixedArgumentTypeCoercion occurrences="2"/>
<MixedArrayAccess occurrences="1">
Expand Down
6 changes: 2 additions & 4 deletions src/BodyParams/BodyParamsMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,15 @@
class BodyParamsMiddleware implements MiddlewareInterface
{
/** @var StrategyInterface[] */
private $strategies = [];
private array $strategies = [];

/**
* List of request methods that do not have any defined body semantics, and thus
* will not have the body parsed.
*
* @see https://tools.ietf.org/html/rfc7231
*
* @var array
*/
private $nonBodyRequests = [
private array $nonBodyRequests = [
'GET',
'HEAD',
'OPTIONS',
Expand Down
8 changes: 5 additions & 3 deletions src/ServerUrlHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Psr\Http\Message\UriInterface;

use function assert;
use function preg_match;
use function rtrim;

Expand All @@ -14,8 +15,7 @@
*/
class ServerUrlHelper
{
/** @var UriInterface */
private $uri;
private ?UriInterface $uri = null;

/**
* Return a path relative to the current request URI.
Expand All @@ -33,7 +33,7 @@ class ServerUrlHelper
*/
public function __invoke(?string $path = null): string
{
$path = $path ?? '';
$path ??= '';

if ($this->uri instanceof UriInterface) {
return $this->createUrlFromUri($path);
Expand Down Expand Up @@ -76,6 +76,8 @@ private function createUrlFromUri(string $specification): string
$query = $matches['query'] ?? '';
$fragment = $matches['fragment'] ?? '';

assert($this->uri instanceof UriInterface);

$uri = $this->uri
->withQuery('')
->withFragment('');
Expand Down
4 changes: 2 additions & 2 deletions src/ServerUrlMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@

namespace Mezzio\Helper;

use Mezzio\Helper\ServerUrlHelper;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;

class ServerUrlMiddleware implements MiddlewareInterface
{
/** @var ServerUrlHelper */
private $helper;
private ServerUrlHelper $helper;

public function __construct(ServerUrlHelper $helper)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Template/TemplateVariableContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
class TemplateVariableContainer implements Countable
{
/** @var array<string, mixed> */
private $variables = [];
private array $variables = [];

public function count(): int
{
Expand Down
12 changes: 4 additions & 8 deletions src/UrlHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,13 @@ class UrlHelper
*/
public const FRAGMENT_IDENTIFIER_REGEX = '/^([!$&\'()*+,;=._~:@\/?-]|%[0-9a-fA-F]{2}|[a-zA-Z0-9])+$/';

/** @var string */
private $basePath = '/';
private string $basePath = '/';

/** @var RouteResult */
private $result;
private ?RouteResult $result = null;

/** @var ServerRequestInterface */
private $request;
private ?ServerRequestInterface $request = null;

/** @var RouterInterface */
private $router;
private RouterInterface $router;

public function __construct(RouterInterface $router)
{
Expand Down
5 changes: 2 additions & 3 deletions src/UrlHelperFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@
class UrlHelperFactory
{
/** @var string Base path for the URL helper */
private $basePath;
private string $basePath;

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

/**
* Allow serialization
Expand Down
4 changes: 2 additions & 2 deletions src/UrlHelperMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Mezzio\Helper;

use Mezzio\Helper\UrlHelper;
use Mezzio\Router\RouteResult;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
Expand All @@ -15,8 +16,7 @@
*/
class UrlHelperMiddleware implements MiddlewareInterface
{
/** @var UrlHelper */
private $helper;
private UrlHelper $helper;

public function __construct(UrlHelper $helper)
{
Expand Down
3 changes: 1 addition & 2 deletions src/UrlHelperMiddlewareFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@

class UrlHelperMiddlewareFactory
{
/** @var string */
private $urlHelperServiceName;
private string $urlHelperServiceName;

/**
* Allow serialization
Expand Down
41 changes: 22 additions & 19 deletions test/BodyParams/BodyParamsMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,9 @@ class BodyParamsMiddlewareTest extends TestCase
{
use AttributeAssertionsTrait;

/** @var Stream */
private $body;
private Stream $body;

/** @var BodyParamsMiddleware */
private $bodyParams;
private BodyParamsMiddleware $bodyParams;

public function setUp(): void
{
Expand Down Expand Up @@ -80,7 +78,7 @@ public function testParsesRawBodyAndPreservesRawBodyInRequestAttribute(string $c

$this->bodyParams->process(
$serverRequest,
$this->mockHandler(function (ServerRequestInterface $request) use (&$serverRequest) {
$this->mockHandler(static function (ServerRequestInterface $request) use (&$serverRequest): Response {
$serverRequest = $request;
return new Response();
})
Expand Down Expand Up @@ -117,7 +115,7 @@ public function testRequestIsUnchangedWhenBodyParamsMiddlewareIsNotApplicable(

$this->bodyParams->process(
$originalRequest,
$this->mockHandler(function (ServerRequestInterface $request) use (&$finalRequest) {
$this->mockHandler(static function (ServerRequestInterface $request) use (&$finalRequest): Response {
$finalRequest = $request;
return new Response();
})
Expand Down Expand Up @@ -158,10 +156,12 @@ public function testCustomStrategiesCanMatchRequests(): void

$response = $middleware->process(
$serverRequest,
$this->mockHandler(function (ServerRequestInterface $request) use ($expectedReturn, $expectedResponse) {
$this->assertSame($expectedReturn, $request);
return $expectedResponse;
})
$this->mockHandler(
function (ServerRequestInterface $request) use ($expectedReturn, $expectedResponse): Response {
$this->assertSame($expectedReturn, $request);
return $expectedResponse;
}
)
);

$this->assertSame($expectedResponse, $response);
Expand All @@ -176,10 +176,12 @@ public function testCallsNextWithOriginalRequestWhenNoStrategiesMatch(): void

$response = $middleware->process(
$serverRequest,
$this->mockHandler(function (ServerRequestInterface $request) use ($serverRequest, $expectedResponse) {
$this->assertSame($serverRequest, $request);
return $expectedResponse;
})
$this->mockHandler(
function (ServerRequestInterface $request) use ($serverRequest, $expectedResponse): Response {
$this->assertSame($serverRequest, $request);
return $expectedResponse;
}
)
);

$this->assertSame($expectedResponse, $response);
Expand Down Expand Up @@ -242,10 +244,8 @@ public function testParsesJsonBodyWhenExpected(string $method): void
);

$handlerTriggered = false;

$result = $this->bodyParams->process(
$serverRequest,
$this->mockHandler(function (ServerRequestInterface $request) use ($serverRequest, &$handlerTriggered) {
$callback =
function (ServerRequestInterface $request) use ($serverRequest, &$handlerTriggered): Response {
$handlerTriggered = true;

$this->assertNotSame(
Expand All @@ -267,7 +267,10 @@ public function testParsesJsonBodyWhenExpected(string $method): void
);

return new Response();
})
};
$result = $this->bodyParams->process(
$serverRequest,
$this->mockHandler($callback)
);

$this->assertInstanceOf(Response::class, $result);
Expand Down
3 changes: 1 addition & 2 deletions test/BodyParams/FormUrlEncodedStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@

class FormUrlEncodedStrategyTest extends TestCase
{
/** @var FormUrlEncodedStrategy */
private $strategy;
private FormUrlEncodedStrategy $strategy;

public function setUp(): void
{
Expand Down
3 changes: 1 addition & 2 deletions test/BodyParams/JsonStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@

class JsonStrategyTest extends TestCase
{
/** @var JsonStrategy */
private $strategy;
private JsonStrategy $strategy;

public function setUp(): void
{
Expand Down
3 changes: 1 addition & 2 deletions test/ConfigProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@

class ConfigProviderTest extends TestCase
{
/** @var ConfigProvider */
private $provider;
private ConfigProvider $provider;

protected function setUp(): void
{
Expand Down
6 changes: 2 additions & 4 deletions test/ContentLengthMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@

class ContentLengthMiddlewareTest extends TestCase
{
/** @var ContentLengthMiddleware */
private $middleware;
/** @var ServerRequest */
private $serverRequest;
private ContentLengthMiddleware $middleware;
private ServerRequest $serverRequest;

protected function setUp(): void
{
Expand Down
11 changes: 6 additions & 5 deletions test/ServerUrlMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ public function testMiddlewareInjectsHelperWithUri(): void
$invoked = false;

$handler = $this->prophesize(RequestHandlerInterface::class);
$handler->handle(Argument::type(RequestInterface::class))->will(function ($req) use (&$invoked) {
$invoked = true;

return new Response();
});
$handler->handle(Argument::type(RequestInterface::class))->will(
function ($req) use (&$invoked): Response {
$invoked = true;
return new Response();
}
);

$test = $middleware->process($request->reveal(), $handler->reveal());
//$this->assertSame($response->reveal(), $test, 'Unexpected return value from middleware');
Expand Down
8 changes: 3 additions & 5 deletions test/Template/RouteTemplateVariableMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ public function testMiddlewareInjectsVariableContainerWithNullRouteIfNoVariableC
{
$this->request
->getAttribute(TemplateVariableContainer::class, Argument::type(TemplateVariableContainer::class))
->will(function ($args) {
return $args[1];
})
->will(fn($args) => $args[1])
->shouldBeCalledTimes(1);

$this->request
Expand Down Expand Up @@ -80,7 +78,7 @@ public function testMiddlewareWillInjectNullValueForRouteIfNoRouteResultInReques
$this->request
->withAttribute(
TemplateVariableContainer::class,
Argument::that(function ($container) use ($originalContainer) {
Argument::that(static function ($container) use ($originalContainer) {
TestCase::assertNotSame($container, $originalContainer);
TestCase::assertTrue($container->has('route'));
TestCase::assertNull($container->get('route'));
Expand Down Expand Up @@ -118,7 +116,7 @@ public function testMiddlewareWillInjectRoutePulledFromRequestRouteResult(): voi
$this->request
->withAttribute(
TemplateVariableContainer::class,
Argument::that(function ($container) use ($originalContainer, $routeResult) {
Argument::that(static function ($container) use ($originalContainer, $routeResult) {
TestCase::assertNotSame($container, $originalContainer);
TestCase::assertTrue($container->has('route'));
TestCase::assertSame($container->get('route'), $routeResult->reveal());
Expand Down
3 changes: 1 addition & 2 deletions test/UrlHelperFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ class UrlHelperFactoryTest extends TestCase
/** @var ContainerInterface&MockObject */
private $container;

/** @var UrlHelperFactory */
private $factory;
private UrlHelperFactory $factory;

public function setUp(): void
{
Expand Down

0 comments on commit b460d1d

Please sign in to comment.