Fix: Allow PHP alternative syntax control structures in debug mode#40
Merged
Fix: Allow PHP alternative syntax control structures in debug mode#40
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes false “Invalid PHP block” compilation errors in debug: true when templates use PHP alternative control-structure syntax (if(): … endif;, foreach(): … endforeach;, etc.) by skipping per-segment validation for snippets that appear to be part of a multi-block alternative-syntax construct.
Changes:
- Add alternative-syntax detection to
PhpSyntaxValidatorusingPhpToken::tokenize()and skip isolated snippet validation when unbalanced. - Add unit tests covering alternative-syntax openers/closers and ensuring regular syntax errors are still reported.
- Add integration tests intended to verify alternative-syntax templates compile and render in
debug: true.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| src/Core/Compiler/PhpSyntaxValidator.php | Adds hasOpenAlternativeSyntax() and uses it to skip per-snippet validation for alternative-syntax fragments. |
| tests/Unit/Core/Compiler/PhpSyntaxValidatorTest.php | Adds unit coverage for alternative-syntax segment handling and regression coverage for real syntax errors. |
| tests/Integration/PhpNormalizationIntegrationTest.php | Adds integration scenarios for alternative syntax in debug mode (compile + render). |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Templates using PHP alternative control structure syntax (e.g.
if():/endif;,foreach():/endforeach;) would throw a falseInvalid PHP blockerror during compilation whendebug: truewas enabled.Root cause
PhpSyntaxValidator::rawPhp()validates each raw PHP block in isolation by wrapping it in astatic function(): void { ... }closure. When a block contains an alternative syntax opener likeif(true):, the parser sees the wrapper's closing}where it expectsT_ENDIF, producing a spurious syntax error.Fix
Added
hasOpenAlternativeSyntax()toPhpSyntaxValidator, which usesPhpToken::tokenize()to count alternative syntax openers (control keywords followed by:) against closers (T_ENDIF,T_ENDFOREACH,T_ENDFOR,T_ENDWHILE,T_ENDSWITCH). When the balance is non-zero the block is part of a multi-block construct and validation is skipped for that snippet — the full compiled output validation (also indebugmode) still catches any real errors in the final generated PHP.Changes
PhpSyntaxValidator: addhasOpenAlternativeSyntax(), call it inprepareRawPhpValidationCode(), usePhpToken::tokenize()(PHP 8)if/endif,foreach/endforeach,while/endwhile,else:, opener-with-body, and regression that real syntax errors are still caughtif/endif,foreach/endforeach, andif/else/endifround-trips compiled and rendered withdebug: true