update code, add psalm

This commit is contained in:
lubiana 2023-01-25 23:06:41 +01:00
parent 427d474f7c
commit 224925e56b
No known key found for this signature in database
8 changed files with 1909 additions and 7 deletions

View file

@ -18,7 +18,8 @@
"php": ">=8.2", "php": ">=8.2",
"php-di/php-di": "^7.0.1", "php-di/php-di": "^7.0.1",
"nikic/fast-route": "^1.3", "nikic/fast-route": "^1.3",
"symfony/http-foundation": "^6.2.5" "symfony/http-foundation": "^6.2.5",
"vimeo/psalm": "^5.6"
}, },
"require-dev": { "require-dev": {
"phpstan/phpstan": "^1.9.14", "phpstan/phpstan": "^1.9.14",
@ -38,8 +39,14 @@
"Composer\\Config::disableProcessTimeout", "Composer\\Config::disableProcessTimeout",
"php -S 0.0.0.0:1235 -t public" "php -S 0.0.0.0:1235 -t public"
], ],
"phpstan": "./vendor/bin/phpstan analyze", "check": [
"baseline": "./vendor/bin/phpstan analyze --generate-baseline", "./vendor/bin/phpstan analyze",
"./vendor/bin/psalm"
],
"baseline": [
"./vendor/bin/phpstan analyze --generate-baseline",
"./vendor/bin/psalm --set-baseline=psalm-baseline.xml"
],
"style": "./vendor/bin/ecs", "style": "./vendor/bin/ecs",
"fix": [ "fix": [
"rector process" ,"ecs --fix", "ecs --fix" "rector process" ,"ecs --fix", "ecs --fix"

1849
composer.lock generated

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,6 @@
parameters:
ignoreErrors:
-
message: "#^Parameter \\#1 \\$data of function unserialize expects string, string\\|false given\\.$#"
count: 1
path: src/Infrastructure/WebApp/Route/CachedRouteCollector.php

8
psalm-baseline.xml Normal file
View file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="5.6.0@e784128902dfe01d489c4123d69918a9f3c1eac5">
<file src="src/Infrastructure/Finder.php">
<UnresolvableInclude>
<code>require_once (string) $file</code>
</UnresolvableInclude>
</file>
</files>

18
psalm.xml Normal file
View file

@ -0,0 +1,18 @@
<?xml version="1.0"?>
<psalm
errorLevel="2"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
findUnusedBaselineEntry="true"
errorBaseline="psalm-baseline.xml"
>
<projectFiles>
<directory name="src" />
<directory name="config" />
<ignoreFiles>
<directory name="vendor" />
</ignoreFiles>
</projectFiles>
</psalm>

View file

@ -7,6 +7,8 @@ use Lubian\AttributeMagic\Infrastructure\WebApp\Request\RequestEvent;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use function apcu_add; use function apcu_add;
use function apcu_fetch;
use function is_string;
use function md5; use function md5;
use function serialize; use function serialize;
use function unserialize; use function unserialize;
@ -16,8 +18,17 @@ final class CachedResponse
#[AsListener(RequestEvent::class, -99)] #[AsListener(RequestEvent::class, -99)]
public function cachedResponse(RequestEvent $event): void public function cachedResponse(RequestEvent $event): void
{ {
$response = new Response('lol'); if ($event->request->getMethod() !== 'GET') {
$serialized = serialize($response); return;
}
$path = md5($event->request->getPathInfo());
$serialized = apcu_fetch($path);
if (! is_string($serialized)) {
return;
}
/** @var Response|null $response */ /** @var Response|null $response */
$response = unserialize($serialized); $response = unserialize($serialized);
$event->response = $response; $event->response = $response;

View file

@ -3,6 +3,7 @@
namespace Lubian\AttributeMagic\Infrastructure\WebApp\Route; namespace Lubian\AttributeMagic\Infrastructure\WebApp\Route;
use Lubian\AttributeMagic\Infrastructure\Event\AsListener; use Lubian\AttributeMagic\Infrastructure\Event\AsListener;
use Lubian\AttributeMagic\Infrastructure\Route\Handler;
use function file_exists; use function file_exists;
use function file_get_contents; use function file_get_contents;
@ -25,7 +26,10 @@ final class CachedRouteCollector
return; return;
} }
$event->routes = unserialize(file_get_contents(self::ROUTES_DIR)); //@phpstan-ignore-line /** @var Handler[] $routes */
$routes = unserialize(file_get_contents(self::ROUTES_DIR));
$event->routes = $routes;
$event->stopped = true; $event->stopped = true;
} }

View file

@ -27,6 +27,7 @@ final readonly class HandlerCaller
} }
ob_start(); ob_start();
/** @var mixed $response */
$response = $this->invoker->call( $response = $this->invoker->call(
[$event->handler->handlerClass, $event->handler->handlerMethod], [$event->handler->handlerClass, $event->handler->handlerMethod],
$event->request->attributes->all() $event->request->attributes->all()