Skip to content

Commit f3a885e

Browse files
committed
TASK: PhpStan Level 8
1 parent 4dda134 commit f3a885e

File tree

8 files changed

+98
-34
lines changed

8 files changed

+98
-34
lines changed

.github/workflows/build.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ jobs:
3636
restore-keys: |
3737
${{ runner.os }}-php-
3838
39+
- name: Setup PHP
40+
uses: shivammathur/setup-php@v2
41+
with:
42+
php-version: ${{ matrix.php-versions }}
43+
extensions: mbstring, xml, json, zlib, iconv, intl, pdo_sqlite
44+
ini-values: date.timezone="Africa/Tunis", opcache.fast_shutdown=0, apc.enable_cli=on
45+
3946
- name: Set Neos Version
4047
run: composer require neos/neos ^${{ matrix.neos-versions }} --no-progress --no-interaction
4148

Classes/Aspects/ContentCacheAspect.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*/
1414
class ContentCacheAspect
1515
{
16-
private $hadUncachedSegments = false;
16+
private bool $hadUncachedSegments = false;
1717

1818
/**
1919
* @Flow\Inject
@@ -24,7 +24,7 @@ class ContentCacheAspect
2424
/**
2525
* @Flow\Before("method(Neos\Fusion\Core\Cache\ContentCache->(createUncachedSegment)())")
2626
*/
27-
public function grabUncachedSegment(JoinPointInterface $joinPoint)
27+
public function grabUncachedSegment(JoinPointInterface $joinPoint): void
2828
{
2929
$this->hadUncachedSegments = true;
3030
}
@@ -35,7 +35,7 @@ public function grabUncachedSegment(JoinPointInterface $joinPoint)
3535
*
3636
* @throws \Neos\Utility\Exception\PropertyNotAccessibleException
3737
*/
38-
public function interceptNodeCacheFlush(JoinPointInterface $joinPoint)
38+
public function interceptNodeCacheFlush(JoinPointInterface $joinPoint): void
3939
{
4040
$object = $joinPoint->getProxy();
4141

Classes/Cache/MetadataAwareStringFrontend.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class MetadataAwareStringFrontend extends StringFrontend
1919
/**
2020
* Store metadata of all loaded cache entries indexed by identifier
2121
*
22-
* @var array
22+
* @var array<string, array{identifier:string, tags: string[], lifetime: int|null}>
2323
*/
2424
protected $metadata = [];
2525

@@ -39,6 +39,10 @@ class MetadataAwareStringFrontend extends StringFrontend
3939
* Set a cache entry and store additional metadata (tags and lifetime)
4040
*
4141
* {@inheritdoc}
42+
*
43+
* @param string $content
44+
* @param string[] $tags
45+
* @return void
4246
*/
4347
public function set(string $entryIdentifier, $content, array $tags = [], int $lifetime = null)
4448
{
@@ -48,6 +52,8 @@ public function set(string $entryIdentifier, $content, array $tags = [], int $li
4852

4953
/**
5054
* {@inheritdoc}
55+
*
56+
* @return string|false
5157
*/
5258
public function get(string $entryIdentifier)
5359
{
@@ -61,6 +67,7 @@ public function get(string $entryIdentifier)
6167

6268
/**
6369
* {@inheritdoc}
70+
* @return array<string,string>
6471
*/
6572
public function getByTag(string $tag): array
6673
{
@@ -77,16 +84,12 @@ public function getByTag(string $tag): array
7784
*
7885
* @param string $content
7986
* @param string $entryIdentifier The identifier metadata
80-
* @param array $tags The tags metadata
87+
* @param string[] $tags The tags metadata
8188
* @param integer $lifetime The lifetime metadata
8289
* @return string The content including the serialized metadata
83-
* @throws InvalidDataTypeException
8490
*/
85-
protected function insertMetadata($content, $entryIdentifier, array $tags, $lifetime)
91+
protected function insertMetadata(string $content, string $entryIdentifier, array $tags, ?int $lifetime)
8692
{
87-
if (!is_string($content)) {
88-
throw new InvalidDataTypeException('Given data is of type "' . gettype($content) . '", but a string is expected for string cache.', 1433155737);
89-
}
9093
$metadata = [
9194
'identifier' => $entryIdentifier,
9295
'tags' => $tags,
@@ -106,7 +109,7 @@ protected function insertMetadata($content, $entryIdentifier, array $tags, $life
106109
* @return string The content without metadata
107110
* @throws InvalidDataTypeException
108111
*/
109-
protected function extractMetadata($entryIdentifier, $content)
112+
protected function extractMetadata($entryIdentifier, $content): string
110113
{
111114
$separatorIndex = strpos($content, self::SEPARATOR);
112115
if ($separatorIndex === false) {
@@ -116,6 +119,7 @@ protected function extractMetadata($entryIdentifier, $content)
116119
} else {
117120
throw $exception;
118121
}
122+
return $content;
119123
}
120124

121125
$metadataJson = substr($content, 0, $separatorIndex);
@@ -135,9 +139,9 @@ protected function extractMetadata($entryIdentifier, $content)
135139
}
136140

137141
/**
138-
* @return array Metadata of all loaded entries (indexed by identifier)
142+
* @return array<string, array{identifier:string, tags?: string[], lifetime?: int|null}> Metadata of all loaded entries (indexed by identifier)
139143
*/
140-
public function getAllMetadata()
144+
public function getAllMetadata(): array
141145
{
142146
return $this->metadata;
143147
}

Classes/Domain/Dto/CacheEntry.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Flowpack\FullPageCache\Domain\Dto;
6+
7+
use GuzzleHttp\Psr7\Message;
8+
use Psr\Http\Message\ResponseInterface;
9+
10+
readonly class CacheEntry
11+
{
12+
public function __construct(
13+
public int $timestamp,
14+
public string $responseAsString,
15+
) {
16+
}
17+
18+
public static function createFromResponse(ResponseInterface $response): CacheEntry
19+
{
20+
$responseAsString = Message::toString($response);
21+
$response->getBody()->rewind();
22+
23+
return new self(
24+
time(),
25+
$responseAsString
26+
);
27+
}
28+
29+
public function getResponse(): ResponseInterface
30+
{
31+
return Message::parseResponse($this->responseAsString)
32+
->withHeader('Age', (string)(time() - $this->timestamp));
33+
}
34+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Flowpack\FullPageCache\Domain\Dto;
6+
7+
readonly class FusionCacheInformation
8+
{
9+
/**
10+
* @param bool $hasUncachedSegments
11+
* @param string[] $tags
12+
* @param int|null $lifetime
13+
*/
14+
public function __construct(
15+
public bool $hasUncachedSegments,
16+
public array $tags,
17+
public ?int $lifetime
18+
) {
19+
}
20+
}

Classes/Middleware/FusionAutoconfigurationMiddleware.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Flowpack\FullPageCache\Middleware;
66

7+
use Flowpack\FullPageCache\Domain\Dto\FusionCacheInformation;
78
use Neos\Flow\Annotations as Flow;
89
use Psr\Http\Message\ResponseInterface;
910
use Psr\Http\Message\ServerRequestInterface;
@@ -48,34 +49,33 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
4849
$response = $response->withoutHeader(self::HEADER_ENABLED);
4950
}
5051

51-
list($hasUncachedSegments, $tags, $lifetime) = $this->getFusionCacheInformations();
52+
$cacheMetadata = $this->getFusionCacheInformations();
5253

53-
if ($response->hasHeader('Set-Cookie') || $hasUncachedSegments) {
54+
if ($response->hasHeader('Set-Cookie') || $cacheMetadata->hasUncachedSegments) {
5455
return $response;
5556
}
5657

5758
$response = $response
5859
->withHeader(RequestCacheMiddleware::HEADER_ENABLED, "");
5960

60-
if ($tags) {
61+
if ($cacheMetadata->tags) {
6162
$response = $response
62-
->withHeader(RequestCacheMiddleware::HEADER_TAGS, $tags);
63+
->withHeader(RequestCacheMiddleware::HEADER_TAGS, $cacheMetadata->tags);
6364
}
6465

65-
if ($lifetime) {
66+
67+
if ($cacheMetadata->lifetime) {
6668
$response = $response
67-
->withHeader(RequestCacheMiddleware::HEADER_LIFETIME, $lifetime);
69+
->withHeader(RequestCacheMiddleware::HEADER_LIFETIME, (string)$cacheMetadata->lifetime);
6870
}
6971

7072
return $response;
7173
}
7274

7375
/**
7476
* Get cache tags and lifetime from the cache metadata that was extracted by the special cache frontend for content cache
75-
*
76-
* @return array with first "hasUncachedSegments", "tags" and "lifetime"
7777
*/
78-
public function getFusionCacheInformations(): array
78+
public function getFusionCacheInformations(): FusionCacheInformation
7979
{
8080
$lifetime = null;
8181
$tags = [];
@@ -94,6 +94,6 @@ public function getFusionCacheInformations(): array
9494
}
9595
$hasUncachedSegments = $this->contentCacheAspect->hasUncachedSegments();
9696

97-
return [$hasUncachedSegments, $tags, $lifetime];
97+
return new FusionCacheInformation($hasUncachedSegments, $tags, $lifetime);
9898
}
9999
}

Classes/Middleware/RequestCacheMiddleware.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Flowpack\FullPageCache\Middleware;
66

7+
use Flowpack\FullPageCache\Domain\Dto\CacheEntry;
78
use Neos\Flow\Annotations as Flow;
89
use Neos\Cache\Frontend\VariableFrontend;
910
use Psr\Http\Message\ResponseInterface;
@@ -36,19 +37,19 @@ class RequestCacheMiddleware implements MiddlewareInterface
3637
protected $cacheFrontend;
3738

3839
/**
39-
* @var array
40+
* @var string[]
4041
* @Flow\InjectConfiguration(path="request.queryParams.allow")
4142
*/
4243
protected $allowedQueryParams;
4344

4445
/**
45-
* @var array
46+
* @var string[]
4647
* @Flow\InjectConfiguration(path="request.queryParams.ignore")
4748
*/
4849
protected $ignoredQueryParams;
4950

5051
/**
51-
* @var array
52+
* @var string[]
5253
* @Flow\InjectConfiguration(path="request.cookieParams.ignore")
5354
*/
5455
protected $ignoredCookieParams;
@@ -72,11 +73,10 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
7273
}
7374

7475
if ($cacheEntry = $this->cacheFrontend->get($entryIdentifier)) {
75-
$age = time() - $cacheEntry['timestamp'];
76-
$response = Message::parseResponse($cacheEntry['response']);
77-
return $response
78-
->withHeader('Age', (string)$age)
79-
->withHeader(self::HEADER_INFO, 'HIT: ' . $entryIdentifier);
76+
if ($cacheEntry instanceof CacheEntry) {
77+
return $cacheEntry->getResponse()
78+
->withHeader(self::HEADER_INFO, 'HIT: ' . $entryIdentifier);
79+
}
8080
}
8181

8282
$response = $next->handle($request->withHeader(self::HEADER_ENABLED, ''));
@@ -106,8 +106,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
106106
->withHeader('Cache-Control', 'public, max-age=' . $publicLifetime);
107107
}
108108

109-
$this->cacheFrontend->set($entryIdentifier, [ 'timestamp' => time(), 'response' => Message::toString($response) ], $tags, $lifetime);
110-
$response->getBody()->rewind();
109+
$this->cacheFrontend->set($entryIdentifier, CacheEntry::createFromResponse($response), $tags, $lifetime);
111110
return $response->withHeader(self::HEADER_INFO, 'MISS: ' . $entryIdentifier);
112111
}
113112

phpstan.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
parameters:
2-
level: 5
2+
level: 8
33
paths:
44
- Classes
55
reportUnmatchedIgnoredErrors: false

0 commit comments

Comments
 (0)