Skip to content

Commit fbada2b

Browse files
authored
[BUGFIX] Handle newly returned TypoScript Statements (#356)
Resolves: #322
1 parent fa710bd commit fbada2b

File tree

5 files changed

+48
-14
lines changed

5 files changed

+48
-14
lines changed

packages/fractor-typoscript/src/AbstractTypoScriptFractor.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ final public function enterNode(Statement $node): Statement|array|int
3838
return $result;
3939
}
4040

41-
final public function leaveNode(Statement $node): void
41+
final public function leaveNode(Statement $node): ?Statement
4242
{
43+
return null;
4344
}
4445

4546
/**

packages/fractor-typoscript/src/Contract/TypoScriptNodeVisitor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function beforeTraversal(File $file, array $statements): void;
2222
*/
2323
public function enterNode(Statement $node): Statement|array|int;
2424

25-
public function leaveNode(Statement $node): void;
25+
public function leaveNode(Statement $node): ?Statement;
2626

2727
/**
2828
* @param Statement[] $statements

packages/fractor-typoscript/src/TypoScriptStatementsIterator.php

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,29 +77,53 @@ private function processStatementList(array $statements): array
7777
private function traverseNode(Statement $node): int|Statement|array
7878
{
7979
$lastCalledVisitor = null;
80-
$result = $node;
80+
$command = null;
81+
8182
foreach ($this->visitors as $visitor) {
82-
$result = $visitor->enterNode($node);
83+
$return = $visitor->enterNode($node);
84+
85+
if ($return instanceof Statement) {
86+
$node = $return;
87+
continue;
88+
}
8389

84-
if (is_int($result)) {
90+
if (is_array($return)) {
91+
return $return;
92+
}
93+
94+
if ($return === self::REMOVE_NODE) {
95+
$command = self::REMOVE_NODE;
8596
$lastCalledVisitor = $visitor;
8697
break;
8798
}
8899
}
89100

90-
if ($node instanceof ConditionalStatement) {
91-
$node->ifStatements = $this->processStatementList($node->ifStatements);
92-
$node->elseStatements = $this->processStatementList($node->elseStatements);
93-
} elseif ($node instanceof NestedAssignment) {
94-
$node->statements = $this->processStatementList($node->statements);
101+
if ($command !== self::REMOVE_NODE) {
102+
if ($node instanceof ConditionalStatement) {
103+
$node->ifStatements = $this->processStatementList($node->ifStatements);
104+
$node->elseStatements = $this->processStatementList($node->elseStatements);
105+
} elseif ($node instanceof NestedAssignment) {
106+
$node->statements = $this->processStatementList($node->statements);
107+
}
95108
}
96109

97110
foreach ($this->visitors as $visitor) {
98111
if ($lastCalledVisitor === $visitor) {
99112
break;
100113
}
101-
$visitor->leaveNode($node);
114+
115+
$return = $visitor->leaveNode($node);
116+
if ($return === null) {
117+
continue;
118+
}
119+
120+
$node = $return;
102121
}
103-
return $result;
122+
123+
if ($command === self::REMOVE_NODE) {
124+
return self::REMOVE_NODE;
125+
}
126+
127+
return $node;
104128
}
105129
}

packages/fractor-typoscript/tests/TypoScriptStatementsIterator/Fixture/StatementCollectingVisitor.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,10 @@ public function enterNode(Statement $node): Statement
3333
return $node;
3434
}
3535

36-
public function leaveNode(Statement $node): void
36+
public function leaveNode(Statement $node): Statement
3737
{
3838
$this->calls[] = sprintf('%s:leaveNode:%s:l-%d', $this->visitorName, $node::class, $node->sourceLine);
39+
return $node;
3940
}
4041

4142
/**

packages/typo3-fractor/rules/TYPO3v13/TypoScript/MigrateIncludeTypoScriptSyntaxFractor.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,14 @@ public function refactor(Statement $statement): null|Statement
133133

134134
private function shouldSkip(Statement $statement): bool
135135
{
136-
return ! $statement instanceof FileIncludeStatement && ! $statement instanceof DirectoryIncludeStatement;
136+
if (! $statement instanceof FileIncludeStatement && ! $statement instanceof DirectoryIncludeStatement) {
137+
return true;
138+
}
139+
140+
if ($statement instanceof FileIncludeStatement && $statement->newSyntax === true) {
141+
return true;
142+
}
143+
144+
return false;
137145
}
138146
}

0 commit comments

Comments
 (0)