Skip to content

Commit

Permalink
task: Fix PHPStan errors
Browse files Browse the repository at this point in the history
  • Loading branch information
andreaswolf committed Mar 8, 2024
1 parent 6310bc9 commit 9506621
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 14 deletions.
9 changes: 8 additions & 1 deletion fractor-xml/src/DomDocumentIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ public function traverseDocument(\DOMDocument $document): void
$visitor->beforeTraversal($document);
}

$this->traverseNode($document->firstChild);
if ($document->firstChild instanceof \DOMNode) {
$this->traverseNode($document->firstChild);
}

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

if ($node->parentNode === null) {
// TODO convert into a custom ShouldNotHappenException
throw new \RuntimeException('Node has no parent node');
}

if ($result === DomDocumentIterator::REMOVE_NODE) {
$node->parentNode->removeChild($node);
$nodeRemoved = true;
Expand Down
5 changes: 4 additions & 1 deletion fractor-xml/src/XmlFractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

namespace a9f\FractorXml;

interface XmlFractor
interface XmlFractor extends DomNodeVisitor
{
public function canHandle(\DOMNode $node): bool;

/**
* @return \DOMNode|DomDocumentIterator::*|null
*/
public function refactor(\DOMNode $node): \DOMNode|int|null;
}
15 changes: 11 additions & 4 deletions fractor-xml/tests/DomDocumentIteratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public function enterNode(\DOMNode $node): \DOMNode|int
'afterTraversal:#document',
], $nodeRemovingVisitor->calls);

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

#[Test]
Expand All @@ -179,6 +179,10 @@ public function enterNode(\DOMNode $node): \DOMNode|int
{
parent::enterNode($node);
if ($node->nodeName === 'Child') {
if ($node->ownerDocument === null) {
throw new \RuntimeException('Node does not have an ownerDocument, cannot create element');
}

return $node->ownerDocument->createElement('NewChild');
}
return $node;
Expand All @@ -201,14 +205,17 @@ public function enterNode(\DOMNode $node): \DOMNode|int
'afterTraversal:#document',
], $nodeRemovingVisitor->calls);

self::assertXmlStringEqualsXmlString('<Root><NewChild></NewChild></Root>', $document->saveXML());
self::assertXmlStringEqualsXmlString('<Root><NewChild></NewChild></Root>', $document->saveXML() ?: '');
}

private function getCollectingDomNodeVisitor(): DomNodeVisitor
private function getCollectingDomNodeVisitor(): CollectingDomNodeVisitor
{
return new CollectingDomNodeVisitor();
}

/**
* @param list<string> $recorder
*/
private function getCallRecordingDomNodeVisitor(string $visitorName, array &$recorder): DomNodeVisitor
{
return new class($visitorName, $recorder) implements DomNodeVisitor {
Expand All @@ -217,7 +224,7 @@ private function getCallRecordingDomNodeVisitor(string $visitorName, array &$rec
*/
public function __construct(
private readonly string $visitorName,
private array &$calls
public array &$calls // only public to please PHPStan
) {
}

Expand Down
6 changes: 3 additions & 3 deletions fractor/src/DependencyInjection/ContainerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function createDependencyInjectionContainer(?string $fractorConfigFile):
$config->addCompilerPass(new CommandsCompilerPass());
$config->addCompilerPass(new FileProcessorCompilerPass());

if (is_file($fractorConfigFile)) {
if ($fractorConfigFile !== null && is_file($fractorConfigFile)) {
$config->import($fractorConfigFile);
}

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

private function registerConfiguredRules(FractorConfig $config)
private function registerConfiguredRules(FractorConfig $config): void
{
foreach ($config->getRules() as $rule) {
$config->registerForAutoconfiguration($rule)
->addTag('fractor.rule');
}
}

private function registerConfiguredFileProcessors(FractorConfig $config)
private function registerConfiguredFileProcessors(FractorConfig $config): void
{
foreach ($config->getFileProcessors() as $processor) {
$config->registerForAutoconfiguration($processor)
Expand Down
2 changes: 2 additions & 0 deletions fractor/src/FileSystem/FileFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
final class FileFinder
{
/**
* @param list<non-empty-string> $directories
* @param list<non-empty-string> $fileExtensions
* @return list<\SplFileInfo>
*/
public function findFiles(array $directories, array $fileExtensions): array
Expand Down
1 change: 1 addition & 0 deletions fractor/tests/Configuration/FractorConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public function importFileThrowsExceptionIfNoClosureIsReturned(): void
private function placeConfigFileInTemporaryFolderAndImport(FractorConfig $config, string $closure): void
{
$tempFile = tempnam(sys_get_temp_dir(), 'fractor-test');
self::assertIsString($tempFile);
try {
file_put_contents($tempFile, $closure);

Expand Down
3 changes: 2 additions & 1 deletion typo3-fractor/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"php": "^8.2",
"a9f/fractor": "@dev",
"a9f/fractor-extension-installer": "@dev",
"a9f/fractor-xml": "@dev"
"a9f/fractor-xml": "@dev",
"thecodingmachine/safe": "^2.5"
},
"require-dev": {
"ergebnis/composer-normalize": "^2.42",
Expand Down
9 changes: 8 additions & 1 deletion typo3-fractor/src/AbstractFlexformFractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ abstract class AbstractFlexformFractor extends AbstractXmlFractor
{
public function canHandle(\DOMNode $node): bool
{
return $node->ownerDocument->firstChild->nodeName === 'T3DataStructure';
$rootNode = $node->ownerDocument?->firstChild;

if ($rootNode === null) {
// TODO convert into a custom ShouldNotHappenException
throw new \RuntimeException('Node\'s document does not have a root node');
}

return $rootNode->nodeName === 'T3DataStructure';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,14 @@ public function refactor(\DOMNode $node): \DOMNode|int|null
}

if ($isSingleSelect) {
$renderType = $childNode->ownerDocument->createElement('renderType');
$ownerDocument = $node->ownerDocument;

if ($ownerDocument === null) {
// TODO convert into a custom ShouldNotHappenException
throw new \RuntimeException('Node does not have an ownerDocument');
}

$renderType = $ownerDocument->createElement('renderType');
$renderType->nodeValue = 'selectSingle';
$node->appendChild($renderType);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

use function Safe\file_get_contents;

class AddRenderTypeToFlexFormFractorTest extends TestCase
{
private const FIXTURE_SEPARATOR = '-----';

/**
* @return array<string, string>
* @return array<string, array{string}>
*/
public static function fixtureFilesProvider(): array
{
Expand Down Expand Up @@ -41,7 +43,7 @@ public function test(string $filePath): void
$iterator = new DomDocumentIterator([new AddRenderTypeToFlexFormFractor()]);
$iterator->traverseDocument($document);

$result = $document->saveXML();
$result = $document->saveXML() ?: '';

self::assertXmlStringEqualsXmlString($expectedResultXml, $result);
}
Expand Down

0 comments on commit 9506621

Please sign in to comment.