diff --git a/.scrutinizer.yml b/.scrutinizer.yml index d754df69..a01d1bd5 100644 --- a/.scrutinizer.yml +++ b/.scrutinizer.yml @@ -6,7 +6,7 @@ build: rabbitmq: false mongodb: false php: - version: 7.3 + version: 7.4 cache: disabled: false diff --git a/.travis.yml b/.travis.yml index 29fa0279..efc25fb7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,8 +3,7 @@ sudo: false language: php php: - - 7.3 - - 7.4snapshot + - 7.4 - nightly cache: diff --git a/README.md b/README.md index 06f4706a..77b13be9 100644 --- a/README.md +++ b/README.md @@ -75,11 +75,9 @@ use Zend\Diactoros\StreamFactory; $middleware = ContentTypeMiddleware::fromRecommendedSettings( // First argument is the list of formats you want to support: [ - 'json' => [ - 'extension' => ['json'], - 'mime-type' => ['application/json', 'text/json', 'application/x-json'], - 'charset' => true, - ], + 'json', + // You may also specify the full configuration of the format. + // That's handy if you need to add extensions or mime-types: 'html' => [ 'extension' => ['html', 'htm', 'php'], 'mime-type' => ['text/html', 'application/xhtml+xml'], diff --git a/composer.json b/composer.json index ac991aff..b7ac8699 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,7 @@ } }, "require": { - "php": "^7.3", + "php": "^7.4 || ^8.0", "ext-json": "*", "fig/http-message-util": "^1.1", "psr/http-factory": "^1.0", @@ -28,24 +28,25 @@ "require-dev": { "infection/infection": "^0.15", "jms/serializer": "^3.5", - "lcobucci/coding-standard": "^3.1", + "laminas/laminas-diactoros": "^2.2", + "lcobucci/coding-standard": "^4.0", "league/plates": "^3.3", - "middlewares/negotiation": "^1.1", - "phpstan/phpstan": "^0.11", - "phpstan/phpstan-deprecation-rules": "^0.11", - "phpstan/phpstan-phpunit": "^0.11", - "phpstan/phpstan-strict-rules": "^0.11", + "middlewares/negotiation": "^2.0", + "phpstan/extension-installer": "^1.0", + "phpstan/phpstan": "^0.12", + "phpstan/phpstan-deprecation-rules": "^0.12", + "phpstan/phpstan-phpunit": "^0.12", + "phpstan/phpstan-strict-rules": "^0.12", "phpunit/phpunit": "^9.0", "squizlabs/php_codesniffer": "^3.5", - "twig/twig": "^2.12", - "zendframework/zend-diactoros": "^2.2" + "twig/twig": "^3.0" }, "suggest": { "jms/serializer": "For content formatting using a more flexible serializer", "league/plates": "For content formatting using Plates as template engine", "middlewares/negotiation": "For acceptable format identification", "twig/twig": "For content formatting using Twig as template engine", - "zendframework/zend-diactoros": "For concrete implementation of PSR-7" + "laminas/laminas-diactoros": "For concrete implementation of PSR-7" }, "autoload": { "psr-4": { diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 79a3306a..5995289c 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -1,9 +1,3 @@ -includes: - - vendor/phpstan/phpstan-phpunit/extension.neon - - vendor/phpstan/phpstan-phpunit/rules.neon - - vendor/phpstan/phpstan-deprecation-rules/rules.neon - - vendor/phpstan/phpstan-strict-rules/rules.neon - parameters: level: 7 paths: @@ -13,3 +7,5 @@ parameters: - message: '#Variable method call on#' path: tests/UnformattedResponseTest.php + + - '#static\(Lcobucci\\ContentNegotiation\\UnformattedResponse\) but returns Lcobucci\\ContentNegotiation\\UnformattedResponse#' diff --git a/src/ContentTypeMiddleware.php b/src/ContentTypeMiddleware.php index e45a97b6..e4da09d5 100644 --- a/src/ContentTypeMiddleware.php +++ b/src/ContentTypeMiddleware.php @@ -15,20 +15,13 @@ final class ContentTypeMiddleware implements MiddlewareInterface { - /** - * @var MiddlewareInterface - */ - private $negotiator; - - /** - * @var StreamFactoryInterface - */ - private $streamFactory; + private MiddlewareInterface $negotiator; + private StreamFactoryInterface $streamFactory; /** * @var Formatter[] */ - private $formatters; + private array $formatters; /** * @param Formatter[] $formatters diff --git a/src/Formatter/JmsSerializer.php b/src/Formatter/JmsSerializer.php index cded0b66..d414d16d 100644 --- a/src/Formatter/JmsSerializer.php +++ b/src/Formatter/JmsSerializer.php @@ -11,15 +11,8 @@ final class JmsSerializer implements Formatter { - /** - * @var SerializerInterface - */ - private $serializer; - - /** - * @var string - */ - private $format; + private SerializerInterface $serializer; + private string $format; public function __construct(SerializerInterface $serializer, string $format) { diff --git a/src/Formatter/Json.php b/src/Formatter/Json.php index 0cb78bc7..e8887bcc 100644 --- a/src/Formatter/Json.php +++ b/src/Formatter/Json.php @@ -6,25 +6,20 @@ use Lcobucci\ContentNegotiation\ContentCouldNotBeFormatted; use Lcobucci\ContentNegotiation\Formatter; use Throwable; +use function json_encode; +use function sprintf; use const JSON_HEX_AMP; use const JSON_HEX_APOS; use const JSON_HEX_QUOT; use const JSON_HEX_TAG; use const JSON_THROW_ON_ERROR; use const JSON_UNESCAPED_SLASHES; -use function assert; -use function is_string; -use function json_encode; -use function sprintf; final class Json implements Formatter { private const DEFAULT_FLAGS = JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_UNESCAPED_SLASHES; - /** - * @var int - */ - private $flags; + private int $flags; public function __construct(int $flags = self::DEFAULT_FLAGS) { @@ -37,10 +32,7 @@ public function __construct(int $flags = self::DEFAULT_FLAGS) public function format($content, array $attributes = []): string { try { - $content = json_encode($content, $this->flags | JSON_THROW_ON_ERROR); - assert(is_string($content)); - - return $content; + return json_encode($content, $this->flags | JSON_THROW_ON_ERROR); } catch (Throwable $exception) { throw new ContentCouldNotBeFormatted( sprintf('An exception was thrown during JSON formatting: %s', $exception->getMessage()), diff --git a/src/Formatter/Plates.php b/src/Formatter/Plates.php index b2ad8f12..80a14d76 100644 --- a/src/Formatter/Plates.php +++ b/src/Formatter/Plates.php @@ -12,15 +12,8 @@ final class Plates implements Formatter { private const DEFAULT_ATTRIBUTE = 'template'; - /** - * @var Engine - */ - private $engine; - - /** - * @var string - */ - private $attributeName; + private Engine $engine; + private string $attributeName; public function __construct(Engine $engine, string $attributeName = self::DEFAULT_ATTRIBUTE) { diff --git a/src/Formatter/Twig.php b/src/Formatter/Twig.php index fb0a21d2..4018b15c 100644 --- a/src/Formatter/Twig.php +++ b/src/Formatter/Twig.php @@ -6,24 +6,17 @@ use Lcobucci\ContentNegotiation\ContentCouldNotBeFormatted; use Lcobucci\ContentNegotiation\Formatter; use Throwable; -use Twig_Environment; +use Twig\Environment; final class Twig implements Formatter { private const DEFAULT_ATTRIBUTE = 'template'; - /** - * @var Twig_Environment - */ - private $environment; - - /** - * @var string - */ - private $attributeName; + private Environment $environment; + private string $attributeName; public function __construct( - Twig_Environment $environment, + Environment $environment, string $attributeName = self::DEFAULT_ATTRIBUTE ) { $this->environment = $environment; diff --git a/src/UnformattedResponse.php b/src/UnformattedResponse.php index 153da3d9..7b4b1b2b 100644 --- a/src/UnformattedResponse.php +++ b/src/UnformattedResponse.php @@ -8,10 +8,7 @@ final class UnformattedResponse implements ResponseInterface { - /** - * @var ResponseInterface - */ - private $decoratedResponse; + private ResponseInterface $decoratedResponse; /** * @var mixed @@ -19,13 +16,13 @@ final class UnformattedResponse implements ResponseInterface private $unformattedContent; /** - * @var mixed[] + * @var array */ - private $attributes; + private array $attributes; /** - * @param mixed $unformattedContent - * @param mixed[] $attributes + * @param mixed $unformattedContent + * @param array $attributes */ public function __construct( ResponseInterface $decoratedResponse, @@ -45,10 +42,7 @@ public function getUnformattedContent() return $this->unformattedContent; } - /** - * {@inheritdoc} - */ - public function getProtocolVersion() + public function getProtocolVersion(): string { return $this->decoratedResponse->getProtocolVersion(); } @@ -68,7 +62,7 @@ public function withProtocolVersion($version) /** * {@inheritdoc} */ - public function getHeaders() + public function getHeaders(): array { return $this->decoratedResponse->getHeaders(); } @@ -76,7 +70,7 @@ public function getHeaders() /** * {@inheritdoc} */ - public function hasHeader($name) + public function hasHeader($name): bool { return $this->decoratedResponse->hasHeader($name); } @@ -92,7 +86,7 @@ public function getHeader($name) /** * {@inheritdoc} */ - public function getHeaderLine($name) + public function getHeaderLine($name): string { return $this->decoratedResponse->getHeaderLine($name); } @@ -133,10 +127,7 @@ public function withoutHeader($name) ); } - /** - * {@inheritdoc} - */ - public function getBody() + public function getBody(): StreamInterface { return $this->decoratedResponse->getBody(); } @@ -153,10 +144,7 @@ public function withBody(StreamInterface $body) ); } - /** - * {@inheritdoc} - */ - public function getStatusCode() + public function getStatusCode(): int { return $this->decoratedResponse->getStatusCode(); } @@ -173,10 +161,7 @@ public function withStatus($code, $reasonPhrase = '') ); } - /** - * {@inheritdoc} - */ - public function getReasonPhrase() + public function getReasonPhrase(): string { return $this->decoratedResponse->getReasonPhrase(); } @@ -198,7 +183,7 @@ public function withAttribute(string $name, $value): self /** * Retrieve the configured attributes * - * @return mixed[] + * @return array */ public function getAttributes(): array { diff --git a/tests/ContentTypeMiddlewareTest.php b/tests/ContentTypeMiddlewareTest.php index 5e2dce78..bdf1c9ed 100644 --- a/tests/ContentTypeMiddlewareTest.php +++ b/tests/ContentTypeMiddlewareTest.php @@ -4,6 +4,10 @@ namespace Lcobucci\ContentNegotiation\Tests; use Fig\Http\Message\StatusCodeInterface; +use Laminas\Diactoros\Response; +use Laminas\Diactoros\Response\EmptyResponse; +use Laminas\Diactoros\ServerRequest; +use Laminas\Diactoros\StreamFactory; use Lcobucci\ContentNegotiation\ContentTypeMiddleware; use Lcobucci\ContentNegotiation\Formatter; use Lcobucci\ContentNegotiation\Tests\Formatter\NaiveTemplateEngine; @@ -12,10 +16,6 @@ use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\RequestHandlerInterface; -use Zend\Diactoros\Response; -use Zend\Diactoros\Response\EmptyResponse; -use Zend\Diactoros\ServerRequest; -use Zend\Diactoros\StreamFactory; use function array_map; /** @@ -184,19 +184,13 @@ private function createRequestHandler(ResponseInterface $response): RequestHandl { return new class($response) implements RequestHandlerInterface { - /** - * @var ResponseInterface - */ - private $response; + private ResponseInterface $response; public function __construct(ResponseInterface $response) { $this->response = $response; } - /** - * {@inheritdoc} - */ public function handle(ServerRequestInterface $request): ResponseInterface { return $this->response; diff --git a/tests/Formatter/JmsSerializerTest.php b/tests/Formatter/JmsSerializerTest.php index 6f0779e4..d3f269f1 100644 --- a/tests/Formatter/JmsSerializerTest.php +++ b/tests/Formatter/JmsSerializerTest.php @@ -18,7 +18,7 @@ final class JmsSerializerTest extends TestCase /** * @var SerializerInterface|MockObject */ - private $serializer; + private SerializerInterface $serializer; /** * @before diff --git a/tests/Formatter/JsonTest.php b/tests/Formatter/JsonTest.php index a7612564..ee845895 100644 --- a/tests/Formatter/JsonTest.php +++ b/tests/Formatter/JsonTest.php @@ -9,8 +9,8 @@ use Lcobucci\ContentNegotiation\Tests\PersonDto; use PHPUnit\Framework\TestCase; use RuntimeException; -use const JSON_UNESCAPED_SLASHES; use function acos; +use const JSON_UNESCAPED_SLASHES; /** * @coversDefaultClass \Lcobucci\ContentNegotiation\Formatter\Json diff --git a/tests/Formatter/PlatesTest.php b/tests/Formatter/PlatesTest.php index 93c436c3..ccbd0e04 100644 --- a/tests/Formatter/PlatesTest.php +++ b/tests/Formatter/PlatesTest.php @@ -15,10 +15,7 @@ */ final class PlatesTest extends TestCase { - /** - * @var Engine - */ - private $engine; + private Engine $engine; /** * @before diff --git a/tests/Formatter/TwigTest.php b/tests/Formatter/TwigTest.php index 38e7cd35..337dc3a8 100644 --- a/tests/Formatter/TwigTest.php +++ b/tests/Formatter/TwigTest.php @@ -7,8 +7,8 @@ use Lcobucci\ContentNegotiation\Formatter\Twig; use Lcobucci\ContentNegotiation\Tests\PersonDto; use PHPUnit\Framework\TestCase; -use Twig_Environment; -use Twig_Loader_Filesystem; +use Twig\Environment; +use Twig\Loader\FilesystemLoader; use function dirname; /** @@ -16,18 +16,15 @@ */ final class TwigTest extends TestCase { - /** - * @var Twig_Environment - */ - private $environment; + private Environment $environment; /** * @before */ public function configureEngine(): void { - $this->environment = new Twig_Environment( - new Twig_Loader_Filesystem('templates/twig', dirname(__DIR__, 2) . '/') + $this->environment = new Environment( + new FilesystemLoader('templates/twig', dirname(__DIR__, 2) . '/') ); } diff --git a/tests/PersonDto.php b/tests/PersonDto.php index f5524cec..9a3c2b96 100644 --- a/tests/PersonDto.php +++ b/tests/PersonDto.php @@ -5,15 +5,9 @@ final class PersonDto { - /** - * @var int - */ - public $id; + public int $id; - /** - * @var string - */ - public $name; + public string $name; public function __construct(int $id, string $name) { diff --git a/tests/UnformattedResponseTest.php b/tests/UnformattedResponseTest.php index ea24ca9f..7d196477 100644 --- a/tests/UnformattedResponseTest.php +++ b/tests/UnformattedResponseTest.php @@ -3,10 +3,10 @@ namespace Lcobucci\ContentNegotiation\Tests; +use Laminas\Diactoros\Response; +use Laminas\Diactoros\Stream; use Lcobucci\ContentNegotiation\UnformattedResponse; -use PHPStan\Testing\TestCase; -use Zend\Diactoros\Response; -use Zend\Diactoros\Stream; +use PHPUnit\Framework\TestCase; /** * @coversDefaultClass \Lcobucci\ContentNegotiation\UnformattedResponse