Skip to content

Commit c83dd21

Browse files
committed
TASK: Resolve phpstan issues
1 parent 4a18c38 commit c83dd21

File tree

7 files changed

+80
-43
lines changed

7 files changed

+80
-43
lines changed

Classes/Command/StylesCommandController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function exportCommand(string $siteNodeName, string $basePrototypeName =
5050
}
5151

5252
/**
53-
* Create a simple controller context which can be used to instantiate a Fusion runtime etc.
53+
* Create a simple controller context which can be used to instantiate a Fusion runtime, etc.
5454
*/
5555
protected function createSimpleControllerRequest(): ActionRequest
5656
{

Classes/Fusion/FusionView.php

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
*/
99

1010
use Neos\Flow\Annotations as Flow;
11+
use Neos\Flow\Mvc\Exception;
1112
use Neos\Flow\Security\Exception as SecurityException;
13+
use Neos\Fusion\Core\FusionConfiguration;
1214
use Neos\Fusion\Core\FusionGlobals;
1315
use Neos\Fusion\View\FusionView as BaseFusionView;
1416
use Neos\Fusion\Core\Runtime as FusionRuntime;
@@ -26,16 +28,13 @@ class FusionView extends BaseFusionView
2628

2729
/**
2830
* @inheritDoc
31+
* @throws Exception
2932
*/
3033
protected function loadFusion(): void
3134
{
32-
$fusionAst = [];
33-
try {
34-
$fusionAst = $this->fusionService->getFusionConfigurationForSitePackage(
35-
$this->getOption('packageKey')
36-
);
37-
} catch (\Exception) {
38-
}
35+
$fusionAst = $this->fusionService->getFusionConfigurationForSitePackage(
36+
$this->getOption('packageKey')
37+
);
3938
$this->parsedFusion = $fusionAst;
4039
}
4140

@@ -47,19 +46,28 @@ protected function loadFusion(): void
4746
*/
4847
public function renderStyles(string $stylePrototypeName): string
4948
{
49+
/** @noinspection PhpConditionAlreadyCheckedInspection */
50+
/** @phpstan-ignore booleanNot.alwaysFalse */
5051
if (!$this->parsedFusion) {
51-
$this->loadFusion();
52+
try {
53+
$this->loadFusion();
54+
} catch (Exception) {
55+
return '';
56+
}
57+
}
58+
$fusionAst = $this->parsedFusion->toArray();
59+
$prototypes = $fusionAst['__prototypes'] ?? [];
60+
61+
if (!$prototypes) {
62+
return '';
5263
}
53-
$fusionAst = $this->parsedFusion;
54-
$prototypes = $fusionAst['__prototypes'];
5564

5665
$arrayIterator = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($fusionAst));
5766
$outputArray = [];
5867
/** @noinspection PhpUnusedLocalVariableInspection */
5968
foreach ($arrayIterator as $sub) {
60-
$subArray = $arrayIterator->getSubIterator();
61-
/** @noinspection PhpParamsInspection */
62-
if (!$subArray || !array_key_exists('__objectType', $subArray)) {
69+
$subArray = iterator_to_array($arrayIterator->getSubIterator());
70+
if (!array_key_exists('__objectType', $subArray)) {
6371
continue;
6472
}
6573
$prototypeName = $subArray['__objectType'];
@@ -82,7 +90,7 @@ public function renderStyles(string $stylePrototypeName): string
8290
$fusionGlobals = FusionGlobals::fromArray(array_filter([
8391
'request' => $this->assignedActionRequest,
8492
]));
85-
$fusionRuntime = new FusionRuntime($fusionAst, $fusionGlobals);
93+
$fusionRuntime = new FusionRuntime(FusionConfiguration::fromArray($fusionAst), $fusionGlobals);
8694
$fusionRuntime->pushContextArray($this->variables);
8795
$output .= $fusionRuntime->render($this->styleRenderPath);
8896
$fusionRuntime->popContext();

Classes/FusionObjects/LoadStylesImplementation.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
declare(strict_types=1);
34

45
namespace Shel\CriticalCSS\FusionObjects;
@@ -52,6 +53,6 @@ public function loadResourceContent(?string $path): string
5253
throw new InvalidVariableException('You have to define a path.', 1573317165);
5354
}
5455

55-
return file_get_contents($path);
56+
return file_get_contents($path) ?: '';
5657
}
5758
}

Classes/FusionObjects/StyleCollectorImplementation.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
/**
1313
* Collects all style tags defined with the `Shel.CriticalCSS:Styles` object
14-
* and puts them in the html head or at the start of the object the collector is applied to.
14+
* and puts them in the HTML head or at the start of the object the collector is applied to.
1515
*/
1616
class StyleCollectorImplementation extends AbstractFusionObject
1717
{
@@ -22,21 +22,30 @@ public function evaluate()
2222
{
2323
$content = $this->fusionValue('content');
2424

25-
preg_match_all('/<style data-inline>(.*?)<\/style>/', $content, $matches);
26-
27-
if (!$matches) {
25+
$occurrences = preg_match_all('/<style data-inline>(.*?)<\/style>/', $content, $matches);
26+
if (!$occurrences) {
2827
return $content;
2928
}
3029

3130
$styles = array_unique($matches[1]);
32-
$content = preg_replace('/<style data-inline>.*?<\/style>/s', '', $content);
31+
/** @var string $modifiedContent */
32+
$modifiedContent = preg_replace('/<style data-inline>.*?<\/style>/s', '', $content);
33+
34+
if (!$modifiedContent) {
35+
return $content;
36+
}
3337

3438
$styleTag = '<style>' . implode('', $styles) . '</style>';
3539

36-
if (str_contains($content, '</head>')) {
37-
return str_replace('</head>', $styleTag . '</head>', $content);
40+
if (str_contains($modifiedContent, '</head>')) {
41+
/** @var string $modifiedContent */
42+
$modifiedContent = str_replace('</head>', $styleTag . '</head>', $modifiedContent);
43+
if ($modifiedContent) {
44+
return $modifiedContent;
45+
}
46+
return $content;
3847
}
3948

40-
return $styleTag . $content;
49+
return $styleTag . $modifiedContent;
4150
}
4251
}

Classes/FusionObjects/StylesImplementation.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use Shel\CriticalCSS\Service\StylesService;
1515

1616
/**
17-
* Adds all attributes as css styles into a style tag with a css class and adds the class to the tag
17+
* Adds all attributes as CSS styles into a style tag with a CSS class and adds the class to the tag
1818
*/
1919
class StylesImplementation extends DataStructureImplementation
2020
{
@@ -28,12 +28,12 @@ class StylesImplementation extends DataStructureImplementation
2828
/**
2929
* Properties that are ignored
3030
*
31-
* @var array
31+
* @var string[]
3232
*/
3333
protected $ignoreProperties = ['__meta'];
3434

3535
/**
36-
* The content that will be applied the generated css class or
36+
* The content that will be applied the generated CSS class or
3737
* if it's multiple elements they will be wrapped with a new
3838
* tag and the generated class. See `fallbackTagName`.
3939
*/
@@ -53,7 +53,7 @@ protected function getFallbackTagName(): string
5353
}
5454

5555
/**
56-
* When this is set a selector is used instead of the generated class.
56+
* When this is set, a selector is used instead of the generated class.
5757
*/
5858
protected function getSelector(): bool|string
5959
{
@@ -75,6 +75,7 @@ protected function getClassPrefix(): string
7575

7676
/**
7777
* @throws FusionException
78+
* @phpstan-ignore method.childReturnType
7879
*/
7980
public function evaluate(): string
8081
{
@@ -87,7 +88,7 @@ public function evaluate(): string
8788
foreach ($sortedChildFusionKeys as $key) {
8889
$value = $this->fusionValue($key);
8990

90-
// When using simple nesting with `{` instead of using Neos.Fusion:DataStructure
91+
// When using simple nesting with `{` instead of using Neos.Fusion:DataStructure,
9192
// we have to retrieve the value from the properties as the value is null.
9293
if ($value === null && array_key_exists($key, $this->properties)) {
9394
$value = $this->properties[$key];
@@ -99,7 +100,7 @@ public function evaluate(): string
99100
$styleProperties[$key] = $value;
100101
}
101102

102-
$path = [$selector !== false ? $selector : '.' . $classPrefix . '#{$hash}'];
103+
$path = [$selector !== false ? (string)$selector : '.' . $classPrefix . '#{$hash}'];
103104
$stylesHash = $this->stylesService->getHashForStyles($styleProperties, $path);
104105
$styles = $this->stylesService->renderStyles($styleProperties, $path);
105106
$styles = str_replace('#{$hash}', $stylesHash, $styles);
@@ -126,7 +127,7 @@ public function evaluate(): string
126127

127128
/**
128129
* @param string $path
129-
* @param array $props
130+
* @param array<string, mixed> $props
130131
*/
131132
protected function evaluateNestedProps(string $path, array &$props): void
132133
{

Classes/Http/StyleMiddleware.php

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@
1616
use Psr\Http\Server\RequestHandlerInterface;
1717

1818
/**
19-
* The HTTP component to collect all inline styles and merge them into the html head
19+
* The HTTP component to collect all inline styles and merge them into the HTML head
2020
*/
2121
class StyleMiddleware implements MiddlewareInterface
2222
{
2323

2424
#[Flow\InjectConfiguration('mergeStyles.enabled')]
25-
protected bool $enabled;
25+
protected bool $enabled = false;
2626

2727
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
2828
{
@@ -36,13 +36,12 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
3636
return $response;
3737
}
3838

39-
$content = $response->getBody()->getContents();
39+
$renderedContent = $response->getBody()->getContents();
4040
$response->getBody()->rewind();
4141

4242
// Retrieve all inline style tags
43-
preg_match_all('/<style data-inline>(.*?)<\/style>/', $content, $matches);
44-
45-
if (!$matches) {
43+
$foundOccurrences = preg_match_all('/<style data-inline>(.*?)<\/style>/', $renderedContent, $matches);
44+
if (!$foundOccurrences) {
4645
return $response;
4746
}
4847

@@ -54,11 +53,15 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
5453
}
5554

5655
// Remove inline style tags from content
57-
$content = preg_replace('/<style data-inline>.*?<\/style>/s', '', $content);
58-
59-
// Add merged styles into one new style tag to head
60-
$styleTag = '<style data-merged>' . implode('', $styles) . '</style>';
61-
$content = str_replace('</head>', $styleTag . '</head>', $content);
56+
$content = preg_replace('/<style data-inline>.*?<\/style>/s', '', $renderedContent);
57+
58+
if ($content) {
59+
// Add merged styles into one new style tag to head
60+
$styleTag = '<style data-merged>' . implode('', $styles) . '</style>';
61+
$content = str_replace('</head>', $styleTag . '</head>', $content);
62+
} else {
63+
$content = $renderedContent;
64+
}
6265

6366
return $response->withBody(ContentStream::fromContents($content));
6467
}

Classes/Service/StylesService.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,24 @@
1515
#[Flow\Scope('singleton')]
1616
class StylesService
1717
{
18+
/**
19+
* @var array<string, string>
20+
*/
1821
protected array $stylesCache = [];
1922

23+
/**
24+
* @param array<string, string> $styleProperties
25+
* @param string[] $path
26+
*/
2027
public function getHashForStyles(array $styleProperties, array $path = []): string
2128
{
2229
return substr(md5(serialize($styleProperties) . serialize($path)), 0, 10);
2330
}
2431

32+
/**
33+
* @param array<string, string> $styleProperties
34+
* @param string[] $path
35+
*/
2536
public function renderStyles(array $styleProperties, array $path = []): string
2637
{
2738
$hash = $this->getHashForStyles($styleProperties, $path);
@@ -35,6 +46,10 @@ public function renderStyles(array $styleProperties, array $path = []): string
3546
return $styles;
3647
}
3748

49+
/**
50+
* @param array<string, mixed> $properties
51+
* @param string[] $path
52+
*/
3853
protected function renderProperties(array $properties, array $path = []): string
3954
{
4055
// Construct full CSS selector
@@ -52,7 +67,7 @@ protected function renderProperties(array $properties, array $path = []): string
5267
if (str_starts_with($styleName, '@')) {
5368
usort($childPath, self::compareSelectorParts(...));
5469
}
55-
$subSelectors[] = $this->renderProperties($styleValue, $childPath);
70+
$subSelectors[] = $this->renderProperties((array)$styleValue, $childPath);
5671
} elseif ($styleValue !== null && $styleValue !== '') {
5772
$styleProps[]= $styleName . ':' . $styleValue;
5873
}

0 commit comments

Comments
 (0)