Skip to content

Commit 9506621

Browse files
committed
task: Fix PHPStan errors
1 parent 6310bc9 commit 9506621

File tree

10 files changed

+51
-14
lines changed

10 files changed

+51
-14
lines changed

fractor-xml/src/DomDocumentIterator.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ public function traverseDocument(\DOMDocument $document): void
2020
$visitor->beforeTraversal($document);
2121
}
2222

23-
$this->traverseNode($document->firstChild);
23+
if ($document->firstChild instanceof \DOMNode) {
24+
$this->traverseNode($document->firstChild);
25+
}
2426

2527
foreach ($this->visitors as $visitor) {
2628
$visitor->afterTraversal($document);
@@ -33,6 +35,11 @@ private function traverseNode(\DOMNode $node): void
3335
foreach ($this->visitors as $visitor) {
3436
$result = $visitor->enterNode($node);
3537

38+
if ($node->parentNode === null) {
39+
// TODO convert into a custom ShouldNotHappenException
40+
throw new \RuntimeException('Node has no parent node');
41+
}
42+
3643
if ($result === DomDocumentIterator::REMOVE_NODE) {
3744
$node->parentNode->removeChild($node);
3845
$nodeRemoved = true;

fractor-xml/src/XmlFractor.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
namespace a9f\FractorXml;
44

5-
interface XmlFractor
5+
interface XmlFractor extends DomNodeVisitor
66
{
77
public function canHandle(\DOMNode $node): bool;
88

9+
/**
10+
* @return \DOMNode|DomDocumentIterator::*|null
11+
*/
912
public function refactor(\DOMNode $node): \DOMNode|int|null;
1013
}

fractor-xml/tests/DomDocumentIteratorTest.php

+11-4
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ public function enterNode(\DOMNode $node): \DOMNode|int
168168
'afterTraversal:#document',
169169
], $nodeRemovingVisitor->calls);
170170

171-
self::assertXmlStringEqualsXmlString('<Root></Root>', $document->saveXML());
171+
self::assertXmlStringEqualsXmlString('<Root></Root>', $document->saveXML() ?: '');
172172
}
173173

174174
#[Test]
@@ -179,6 +179,10 @@ public function enterNode(\DOMNode $node): \DOMNode|int
179179
{
180180
parent::enterNode($node);
181181
if ($node->nodeName === 'Child') {
182+
if ($node->ownerDocument === null) {
183+
throw new \RuntimeException('Node does not have an ownerDocument, cannot create element');
184+
}
185+
182186
return $node->ownerDocument->createElement('NewChild');
183187
}
184188
return $node;
@@ -201,14 +205,17 @@ public function enterNode(\DOMNode $node): \DOMNode|int
201205
'afterTraversal:#document',
202206
], $nodeRemovingVisitor->calls);
203207

204-
self::assertXmlStringEqualsXmlString('<Root><NewChild></NewChild></Root>', $document->saveXML());
208+
self::assertXmlStringEqualsXmlString('<Root><NewChild></NewChild></Root>', $document->saveXML() ?: '');
205209
}
206210

207-
private function getCollectingDomNodeVisitor(): DomNodeVisitor
211+
private function getCollectingDomNodeVisitor(): CollectingDomNodeVisitor
208212
{
209213
return new CollectingDomNodeVisitor();
210214
}
211215

216+
/**
217+
* @param list<string> $recorder
218+
*/
212219
private function getCallRecordingDomNodeVisitor(string $visitorName, array &$recorder): DomNodeVisitor
213220
{
214221
return new class($visitorName, $recorder) implements DomNodeVisitor {
@@ -217,7 +224,7 @@ private function getCallRecordingDomNodeVisitor(string $visitorName, array &$rec
217224
*/
218225
public function __construct(
219226
private readonly string $visitorName,
220-
private array &$calls
227+
public array &$calls // only public to please PHPStan
221228
) {
222229
}
223230

fractor/src/DependencyInjection/ContainerBuilder.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function createDependencyInjectionContainer(?string $fractorConfigFile):
3030
$config->addCompilerPass(new CommandsCompilerPass());
3131
$config->addCompilerPass(new FileProcessorCompilerPass());
3232

33-
if (is_file($fractorConfigFile)) {
33+
if ($fractorConfigFile !== null && is_file($fractorConfigFile)) {
3434
$config->import($fractorConfigFile);
3535
}
3636

@@ -42,15 +42,15 @@ public function createDependencyInjectionContainer(?string $fractorConfigFile):
4242
return $config;
4343
}
4444

45-
private function registerConfiguredRules(FractorConfig $config)
45+
private function registerConfiguredRules(FractorConfig $config): void
4646
{
4747
foreach ($config->getRules() as $rule) {
4848
$config->registerForAutoconfiguration($rule)
4949
->addTag('fractor.rule');
5050
}
5151
}
5252

53-
private function registerConfiguredFileProcessors(FractorConfig $config)
53+
private function registerConfiguredFileProcessors(FractorConfig $config): void
5454
{
5555
foreach ($config->getFileProcessors() as $processor) {
5656
$config->registerForAutoconfiguration($processor)

fractor/src/FileSystem/FileFinder.php

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
final class FileFinder
88
{
99
/**
10+
* @param list<non-empty-string> $directories
11+
* @param list<non-empty-string> $fileExtensions
1012
* @return list<\SplFileInfo>
1113
*/
1214
public function findFiles(array $directories, array $fileExtensions): array

fractor/tests/Configuration/FractorConfigTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public function importFileThrowsExceptionIfNoClosureIsReturned(): void
4848
private function placeConfigFileInTemporaryFolderAndImport(FractorConfig $config, string $closure): void
4949
{
5050
$tempFile = tempnam(sys_get_temp_dir(), 'fractor-test');
51+
self::assertIsString($tempFile);
5152
try {
5253
file_put_contents($tempFile, $closure);
5354

typo3-fractor/composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"php": "^8.2",
1515
"a9f/fractor": "@dev",
1616
"a9f/fractor-extension-installer": "@dev",
17-
"a9f/fractor-xml": "@dev"
17+
"a9f/fractor-xml": "@dev",
18+
"thecodingmachine/safe": "^2.5"
1819
},
1920
"require-dev": {
2021
"ergebnis/composer-normalize": "^2.42",

typo3-fractor/src/AbstractFlexformFractor.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ abstract class AbstractFlexformFractor extends AbstractXmlFractor
88
{
99
public function canHandle(\DOMNode $node): bool
1010
{
11-
return $node->ownerDocument->firstChild->nodeName === 'T3DataStructure';
11+
$rootNode = $node->ownerDocument?->firstChild;
12+
13+
if ($rootNode === null) {
14+
// TODO convert into a custom ShouldNotHappenException
15+
throw new \RuntimeException('Node\'s document does not have a root node');
16+
}
17+
18+
return $rootNode->nodeName === 'T3DataStructure';
1219
}
1320
}

typo3-fractor/src/Rules/FlexForm/AddRenderTypeToFlexFormFractor.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,14 @@ public function refactor(\DOMNode $node): \DOMNode|int|null
3737
}
3838

3939
if ($isSingleSelect) {
40-
$renderType = $childNode->ownerDocument->createElement('renderType');
40+
$ownerDocument = $node->ownerDocument;
41+
42+
if ($ownerDocument === null) {
43+
// TODO convert into a custom ShouldNotHappenException
44+
throw new \RuntimeException('Node does not have an ownerDocument');
45+
}
46+
47+
$renderType = $ownerDocument->createElement('renderType');
4148
$renderType->nodeValue = 'selectSingle';
4249
$node->appendChild($renderType);
4350
}

typo3-fractor/tests/Rules/FlexForm/AddRenderTypeToFlexFormFractorTest.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
use PHPUnit\Framework\Attributes\DataProvider;
88
use PHPUnit\Framework\TestCase;
99

10+
use function Safe\file_get_contents;
11+
1012
class AddRenderTypeToFlexFormFractorTest extends TestCase
1113
{
1214
private const FIXTURE_SEPARATOR = '-----';
1315

1416
/**
15-
* @return array<string, string>
17+
* @return array<string, array{string}>
1618
*/
1719
public static function fixtureFilesProvider(): array
1820
{
@@ -41,7 +43,7 @@ public function test(string $filePath): void
4143
$iterator = new DomDocumentIterator([new AddRenderTypeToFlexFormFractor()]);
4244
$iterator->traverseDocument($document);
4345

44-
$result = $document->saveXML();
46+
$result = $document->saveXML() ?: '';
4547

4648
self::assertXmlStringEqualsXmlString($expectedResultXml, $result);
4749
}

0 commit comments

Comments
 (0)