From 4d80d76676ad8ef64542188646e00b8c17f6821c Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 3 Aug 2026 17:26:33 +0700 Subject: [PATCH] [refactor] simplify RemoveMissingPsr4PathVisitor --- .../RemoveMissingPsr4PathVisitor.php | 81 ++++++++----------- 1 file changed, 34 insertions(+), 47 deletions(-) diff --git a/src/Rule/Fixer/JsonRecast/ObjectItemNode/RemoveMissingPsr4PathVisitor.php b/src/Rule/Fixer/JsonRecast/ObjectItemNode/RemoveMissingPsr4PathVisitor.php index caeb244b..363b3910 100644 --- a/src/Rule/Fixer/JsonRecast/ObjectItemNode/RemoveMissingPsr4PathVisitor.php +++ b/src/Rule/Fixer/JsonRecast/ObjectItemNode/RemoveMissingPsr4PathVisitor.php @@ -36,75 +36,62 @@ public function __construct( public function enterNode(NodeJson $nodeJson, NodeJsonPath $nodeJsonPath): ?int { - if ($nodeJson instanceof ObjectItemNode && $this->isPsr4Mapping($nodeJsonPath)) { - if ( - $nodeJson->value instanceof StringNode - && ! $this->directoryExists($nodeJson->value->value) - ) { - return NodeJsonVisitor::REMOVE_NODE; - } - + if (! $this->isMissingPsr4Path($nodeJson, $nodeJsonPath)) { return null; } - if (! $nodeJson instanceof ArrayItemNode || ! $this->isPsr4PathListItem($nodeJsonPath)) { - return null; - } + return NodeJsonVisitor::REMOVE_NODE; + } - if (! $nodeJson->value instanceof StringNode || $this->directoryExists($nodeJson->value->value)) { + public function leaveNode(NodeJson $nodeJson, NodeJsonPath $nodeJsonPath): null|int + { + if ( + ! $nodeJson instanceof ObjectItemNode + || ! $this->becameEmptyPsr4Container($nodeJson, $nodeJsonPath) + ) { return null; } return NodeJsonVisitor::REMOVE_NODE; } - public function leaveNode(NodeJson $nodeJson, NodeJsonPath $nodeJsonPath): null|int + private function isMissingPsr4Path(NodeJson $nodeJson, NodeJsonPath $nodeJsonPath): bool { - if (! $nodeJson instanceof ObjectItemNode) { - return null; + if ($nodeJson instanceof ObjectItemNode) { + return $this->isPsr4Mapping($nodeJsonPath) + && $this->isMissingDirectory($nodeJson->value); } - if ($this->isPsr4Section($nodeJson, $nodeJsonPath)) { - if ( - ! $nodeJson->value instanceof ObjectNode - || ! $this->becameEmpty($nodeJson->value) - ) { - return null; - } + return $nodeJson instanceof ArrayItemNode + && $this->isPsr4PathListItem($nodeJsonPath) + && $this->isMissingDirectory($nodeJson->value); + } - return NodeJsonVisitor::REMOVE_NODE; - } + private function isMissingDirectory(NodeJson $nodeJson): bool + { + return $nodeJson instanceof StringNode + && ! $this->directoryExists($nodeJson->value); + } - if ($this->isEmptyComposerAutoloadItem($nodeJson, $nodeJsonPath)) { - return NodeJsonVisitor::REMOVE_NODE; - } + private function becameEmptyPsr4Container(ObjectItemNode $objectItemNode, NodeJsonPath $nodeJsonPath): bool + { + $value = $objectItemNode->value; - if (! $this->isPsr4Mapping($nodeJsonPath)) { - return null; + if ($value instanceof ArrayNode) { + return $this->isPsr4Mapping($nodeJsonPath) + && $this->becameEmpty($value); } - if ( - ! $nodeJson->value instanceof ArrayNode - || ! $this->becameEmpty($nodeJson->value) - ) { - return null; + if (! $value instanceof ObjectNode || ! $this->becameEmpty($value)) { + return false; } - return NodeJsonVisitor::REMOVE_NODE; - } - - private function isPsr4Section(ObjectItemNode $objectItemNode, NodeJsonPath $nodeJsonPath): bool - { - return $objectItemNode->key->value === 'psr-4' - && $this->isComposerAutoloadPath($nodeJsonPath); - } + if ($objectItemNode->key->value === 'psr-4') { + return $this->isComposerAutoloadPath($nodeJsonPath); + } - private function isEmptyComposerAutoloadItem(ObjectItemNode $objectItemNode, NodeJsonPath $nodeJsonPath): bool - { return $nodeJsonPath->isRoot() - && $this->isComposerAutoloadKey($objectItemNode->key->value) - && $objectItemNode->value instanceof ObjectNode - && $this->becameEmpty($objectItemNode->value); + && $this->isComposerAutoloadKey($objectItemNode->key->value); } private function isPsr4Mapping(NodeJsonPath $nodeJsonPath): bool