diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index ba38486..0b6a230 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -1,9 +1,24 @@ name: CI on: - push - - pull_request jobs: - build: + phpstan: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v2 + + - name: Setup PHP, with composer and extensions + uses: shivammathur/setup-php@v2 + with: + php-version: 7.3 + + - name: Install dependencies with Composer + uses: ramsey/composer-install@v1 + + - name: Run phpstan + run: vendor/bin/phpstan analyse --level=6 src/ + tests: strategy: matrix: dependencies: @@ -13,6 +28,7 @@ jobs: - 7.3 - 7.4 - 8.0 + - 8.1 runs-on: ubuntu-latest steps: - name: Checkout diff --git a/composer.json b/composer.json index 15db6bf..77bcca9 100644 --- a/composer.json +++ b/composer.json @@ -16,9 +16,8 @@ "maximebf/debugbar": "^1.4", "psr/http-server-handler": "^1.0", "psr/http-server-middleware": "^1.0", - "psr/container": "^1.0", - "psr/http-message": "^1.0.1", - "psr/http-factory": "^1.0", + "psr/container-implementation": "^1.0 || ^2.0", + "psr/http-message-implementation": "^1.0", "psr/http-factory-implementation": "^1.0" }, "require-dev": { @@ -28,7 +27,8 @@ "mezzio/mezzio": "^3.0", "mezzio/mezzio-fastroute": "^3.0.1", "laminas/laminas-servicemanager": "^3.3.2", - "laminas/laminas-diactoros": "^2.0" + "laminas/laminas-diactoros": "^2.0", + "phpstan/phpstan": "^1.4" }, "autoload": { "psr-4": { diff --git a/phpunit.xml b/phpunit.xml index b0c7e5f..fd2da66 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,15 +1,13 @@ - - - - - ./test - - - - - - ./src - - + + + + ./src + + + + + ./test + + diff --git a/src/ConfigProvider.php b/src/ConfigProvider.php index 6204d59..375bad1 100644 --- a/src/ConfigProvider.php +++ b/src/ConfigProvider.php @@ -5,11 +5,17 @@ final class ConfigProvider { + /** + * @return array + */ public static function getConfig(): array { return (new self())(); } + /** + * @return array + */ public function __invoke(): array { $config = include __DIR__ . '/../config/phpdebugbar.config.php'; diff --git a/src/JavascriptRendererFactory.php b/src/JavascriptRendererFactory.php index c47fef0..3063abc 100644 --- a/src/JavascriptRendererFactory.php +++ b/src/JavascriptRendererFactory.php @@ -11,11 +11,11 @@ final class JavascriptRendererFactory { public function __invoke(ContainerInterface $container): JavascriptRenderer { - $debugbar = $container->get(DebugBar::class); + $debugBar = $container->get(DebugBar::class); $config = $container->get(ConfigProvider::class); $rendererOptions = $config['phpmiddleware']['phpdebugbar']['javascript_renderer']; - $renderer = new JavascriptRenderer($debugbar); + $renderer = new JavascriptRenderer($debugBar); $renderer->setOptions($rendererOptions); return $renderer; diff --git a/src/PhpDebugBarMiddleware.php b/src/PhpDebugBarMiddleware.php index 6bdc2f6..8d4a892 100644 --- a/src/PhpDebugBarMiddleware.php +++ b/src/PhpDebugBarMiddleware.php @@ -21,16 +21,27 @@ final class PhpDebugBarMiddleware implements MiddlewareInterface { public const FORCE_KEY = 'X-Enable-Debug-Bar'; + /** + * @var DebugBarRenderer + */ private $debugBarRenderer; + + /** + * @var ResponseFactoryInterface + */ private $responseFactory; + + /** + * @var StreamFactoryInterface + */ private $streamFactory; public function __construct( - DebugBarRenderer $debugbarRenderer, + DebugBarRenderer $debugBarRenderer, ResponseFactoryInterface $responseFactory, StreamFactoryInterface $streamFactory ) { - $this->debugBarRenderer = $debugbarRenderer; + $this->debugBarRenderer = $debugBarRenderer; $this->responseFactory = $responseFactory; $this->streamFactory = $streamFactory; } @@ -60,7 +71,14 @@ public function process(ServerRequest $request, RequestHandler $handler): Respon public function __invoke(ServerRequest $request, Response $response, callable $next): Response { $handler = new class($next, $response) implements RequestHandler { + /** + * @var callable + */ private $next; + + /** + * @var Response + */ private $response; public function __construct(callable $next, Response $response) @@ -81,9 +99,9 @@ private function shouldReturnResponse(ServerRequest $request, Response $response { $forceHeaderValue = $request->getHeaderLine(self::FORCE_KEY); $forceCookieValue = $request->getCookieParams()[self::FORCE_KEY] ?? ''; - $forceAttibuteValue = $request->getAttribute(self::FORCE_KEY, ''); - $isForceEnable = in_array('true', [$forceHeaderValue, $forceCookieValue, $forceAttibuteValue], true); - $isForceDisable = in_array('false', [$forceHeaderValue, $forceCookieValue, $forceAttibuteValue], true); + $forceAttributeValue = $request->getAttribute(self::FORCE_KEY, ''); + $isForceEnable = in_array('true', [$forceHeaderValue, $forceCookieValue, $forceAttributeValue], true); + $isForceDisable = in_array('false', [$forceHeaderValue, $forceCookieValue, $forceAttributeValue], true); return $isForceDisable || (!$isForceEnable && ($this->isRedirect($response) || !$this->isHtmlAccepted($request))); } @@ -169,22 +187,22 @@ private function getContentTypeByFileName(string $filename): string 'woff2' => 'application/font-woff2', ]; - return isset($map[$ext]) ? $map[$ext] : 'text/plain'; + return $map[$ext] ?? 'text/plain'; } private function isHtmlResponse(Response $response): bool { - return $this->hasHeaderContains($response, 'Content-Type', 'text/html'); + return $this->isHtml($response, 'Content-Type'); } private function isHtmlAccepted(ServerRequest $request): bool { - return $this->hasHeaderContains($request, 'Accept', 'text/html'); + return $this->isHtml($request, 'Accept'); } - private function hasHeaderContains(MessageInterface $message, string $headerName, string $value): bool + private function isHtml(MessageInterface $message, string $headerName): bool { - return strpos($message->getHeaderLine($headerName), $value) !== false; + return strpos($message->getHeaderLine($headerName), 'text/html') !== false; } private function isRedirect(Response $response): bool @@ -216,6 +234,9 @@ private function serializeResponse(Response $response) : string ); } + /** + * @param array> $headers + */ private function serializeHeaders(array $headers) : string { $lines = [];