From bc007d109e558e75edf2e4ccda43d37bf419025d Mon Sep 17 00:00:00 2001 From: Witold Wasiczko Date: Tue, 22 Feb 2022 14:18:21 +0100 Subject: [PATCH 1/6] Improve deps --- .github/workflows/tests.yml | 1 + composer.json | 7 ++++--- phpunit.xml | 24 +++++++++++------------- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index ba38486..99b8ee9 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -13,6 +13,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..970c451 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", + "psr/http-message-implementation": "^1.0", "psr/http-factory-implementation": "^1.0" }, "require-dev": { @@ -41,3 +40,5 @@ } } } + + 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 + + From 9641c4734d85a3bcaed07fdfdb1d3340669d6ac2 Mon Sep 17 00:00:00 2001 From: Witold Wasiczko Date: Tue, 22 Feb 2022 14:20:05 +0100 Subject: [PATCH 2/6] Composer update --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 970c451..d5a2b7e 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,7 @@ "maximebf/debugbar": "^1.4", "psr/http-server-handler": "^1.0", "psr/http-server-middleware": "^1.0", - "psr/container-implementation": "^1.0", + "psr/container-implementation": "^1.0 || ^2.0", "psr/http-message-implementation": "^1.0", "psr/http-factory-implementation": "^1.0" }, From 61015a92269b17c6a377d2eae070e6f84455a255 Mon Sep 17 00:00:00 2001 From: Witold Wasiczko Date: Tue, 22 Feb 2022 14:26:54 +0100 Subject: [PATCH 3/6] phpstan --- .github/workflows/tests.yml | 18 +++++++++++++++++- composer.json | 5 ++--- src/ConfigProvider.php | 6 ++++++ src/PhpDebugBarMiddleware.php | 3 +++ 4 files changed, 28 insertions(+), 4 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 99b8ee9..b8a1074 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -3,7 +3,23 @@ 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: diff --git a/composer.json b/composer.json index d5a2b7e..77bcca9 100644 --- a/composer.json +++ b/composer.json @@ -27,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": { @@ -40,5 +41,3 @@ } } } - - 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/PhpDebugBarMiddleware.php b/src/PhpDebugBarMiddleware.php index 6bdc2f6..1137e58 100644 --- a/src/PhpDebugBarMiddleware.php +++ b/src/PhpDebugBarMiddleware.php @@ -216,6 +216,9 @@ private function serializeResponse(Response $response) : string ); } + /** + * @param array> $headers + */ private function serializeHeaders(array $headers) : string { $lines = []; From e85f36b7e4372ce9ea2feaf836eff5e590998dc0 Mon Sep 17 00:00:00 2001 From: Witold Wasiczko Date: Tue, 22 Feb 2022 14:28:05 +0100 Subject: [PATCH 4/6] Remove on pull request --- .github/workflows/tests.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index b8a1074..0b6a230 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -1,7 +1,6 @@ name: CI on: - push - - pull_request jobs: phpstan: runs-on: ubuntu-latest From d05d060ba3d00526b7535717b5aafc954bf97cdc Mon Sep 17 00:00:00 2001 From: Witold Wasiczko Date: Tue, 22 Feb 2022 14:29:58 +0100 Subject: [PATCH 5/6] Fixes --- src/PhpDebugBarMiddleware.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/PhpDebugBarMiddleware.php b/src/PhpDebugBarMiddleware.php index 1137e58..0a1e80f 100644 --- a/src/PhpDebugBarMiddleware.php +++ b/src/PhpDebugBarMiddleware.php @@ -21,8 +21,19 @@ 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( @@ -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) From f0e0b1a427b2b8edd4f4049bc04a23f090b74469 Mon Sep 17 00:00:00 2001 From: Witold Wasiczko Date: Tue, 22 Feb 2022 14:33:42 +0100 Subject: [PATCH 6/6] Fix typos --- src/JavascriptRendererFactory.php | 4 ++-- src/PhpDebugBarMiddleware.php | 20 ++++++++++---------- 2 files changed, 12 insertions(+), 12 deletions(-) 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 0a1e80f..8d4a892 100644 --- a/src/PhpDebugBarMiddleware.php +++ b/src/PhpDebugBarMiddleware.php @@ -37,11 +37,11 @@ final class PhpDebugBarMiddleware implements MiddlewareInterface 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; } @@ -99,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))); } @@ -187,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