Skip to content

Commit

Permalink
Improve deps (#38)
Browse files Browse the repository at this point in the history
* Improve deps
* phpstan
* Remove on pull request
* Fix typos
  • Loading branch information
snapshotpl authored Feb 22, 2022
1 parent 133763e commit 7a55035
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 31 deletions.
20 changes: 18 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -13,6 +28,7 @@ jobs:
- 7.3
- 7.4
- 8.0
- 8.1
runs-on: ubuntu-latest
steps:
- name: Checkout
Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -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": {
Expand Down
24 changes: 11 additions & 13 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit bootstrap="./vendor/autoload.php" colors="true">
<testsuites>
<testsuite name="unit">
<directory>./test</directory>
</testsuite>
</testsuites>

<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="./vendor/autoload.php" colors="true" xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd">
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./src</directory>
</include>
</coverage>
<testsuites>
<testsuite name="unit">
<directory>./test</directory>
</testsuite>
</testsuites>
</phpunit>
6 changes: 6 additions & 0 deletions src/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@

final class ConfigProvider
{
/**
* @return array<string, mixed>
*/
public static function getConfig(): array
{
return (new self())();
}

/**
* @return array<string, mixed>
*/
public function __invoke(): array
{
$config = include __DIR__ . '/../config/phpdebugbar.config.php';
Expand Down
4 changes: 2 additions & 2 deletions src/JavascriptRendererFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
41 changes: 31 additions & 10 deletions src/PhpDebugBarMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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)
Expand All @@ -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)));
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -216,6 +234,9 @@ private function serializeResponse(Response $response) : string
);
}

/**
* @param array<string, array<string>> $headers
*/
private function serializeHeaders(array $headers) : string
{
$lines = [];
Expand Down

0 comments on commit 7a55035

Please sign in to comment.