Skip to content

Fix: Allow PHP alternative syntax control structures in debug mode#40

Merged
josbeir merged 2 commits intomainfrom
fix/php-shorthand-syntax
Mar 11, 2026
Merged

Fix: Allow PHP alternative syntax control structures in debug mode#40
josbeir merged 2 commits intomainfrom
fix/php-shorthand-syntax

Conversation

@josbeir
Copy link
Copy Markdown
Owner

@josbeir josbeir commented Mar 11, 2026

Templates using PHP alternative control structure syntax (e.g. if(): / endif;, foreach(): / endforeach;) would throw a false Invalid PHP block error during compilation when debug: true was enabled.

Root cause

PhpSyntaxValidator::rawPhp() validates each raw PHP block in isolation by wrapping it in a static function(): void { ... } closure. When a block contains an alternative syntax opener like if(true):, the parser sees the wrapper's closing } where it expects T_ENDIF, producing a spurious syntax error.

Fix

Added hasOpenAlternativeSyntax() to PhpSyntaxValidator, which uses PhpToken::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 in debug mode) still catches any real errors in the final generated PHP.

Changes

  • PhpSyntaxValidator: add hasOpenAlternativeSyntax(), call it in prepareRawPhpValidationCode(), use PhpToken::tokenize() (PHP 8)
  • Unit tests: 6 new cases covering if/endif, foreach/endforeach, while/endwhile, else:, opener-with-body, and regression that real syntax errors are still caught
  • Integration tests: if/endif, foreach/endforeach, and if/else/endif round-trips compiled and rendered with debug: true

Copilot AI review requested due to automatic review settings March 11, 2026 07:20
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 PhpSyntaxValidator using PhpToken::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.

@josbeir josbeir merged commit 0fc5a0f into main Mar 11, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants