Add ShortOpenTag rule: detect PHP short open tags (fixes #771) - #1330
Open
aymericcucherousset wants to merge 1 commit into
Open
Add ShortOpenTag rule: detect PHP short open tags (fixes #771)#1330aymericcucherousset wants to merge 1 commit into
aymericcucherousset wants to merge 1 commit into
Conversation
Member
|
Master (2.x) isn't taking on new features, please target 3.x instead. |
Detects bare '<?' short open tags (issue phpmd#771). Detection works independently of the local "short_open_tag" ini setting: bare '<?' only tokenizes as T_OPEN_TAG when that ini is enabled, so files are also scanned for it surviving as literal text inside T_INLINE_HTML when it's disabled. '<?=' is intentionally excluded (always available since PHP 5.4), as is a leading '<?xml ...?>' declaration (common, unfixable false positive; excluded on both detection paths so the result doesn't depend on the local ini state). Ported to the 3.x branch's restructured layout and PHP 8.1+ style.
aymericcucherousset
force-pushed
the
feature/short-open-tag-rule
branch
from
August 17, 2026 19:38
98c795c to
66e1084
Compare
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.
Type: feature
Issue: Resolves #771
Breaking change: no
Adds a new CleanCode rule,
ShortOpenTag, that flags PHP files usingthe short open tag
<?instead of<?php.Why this took until now
The issue was blocked on PDepend normalizing
<?into<?phpbeforetokenizing, so PHPMD couldn't observe the distinction. That was fixed
upstream in pdepend/pdepend#490 (merged 2021); this repo's
pdepend/pdepend: ^2.16.1requirement already includes it. Inpractice the new rule doesn't consume PDepend's tokenizer at all, it
reads the file and tokenizes it directly with
token_get_all(), soit isn't actually dependent on that PR, just aligned with the same
<?/<?php/<?=distinction it introduced.Detection approach
Only a bare
<?is flagged;<?=is always available regardless ofshort_open_tagand out of scope. Detecting the bare form correctlyturned out to be ini-sensitive:
<? ...only tokenizes asT_OPEN_TAGwhenshort_open_tagis enabled (the increasinglyuncommon case), when it's disabled, the same text survives as literal
T_INLINE_HTML. The rule checks both token shapes so results don'tdepend on the environment PHPMD happens to run in. A leading
<?xml ...?>declaration is excluded on both paths (common,deliberate pattern with no simple fix), matched case-insensitively.
Adding a New Rule checklist
src/main/resources/rulesets/cleancode.xmlsrc/main/php/PHPMD/Rule/CleanCode/ShortOpenTag.phpsrc/site/rst/rules/cleancode.rstsrc/test/php/PHPMD/Rule/CleanCode/ShortOpenTagTest.php:applies/does not apply cases, xml-prolog exclusion (both ini paths,
uppercase variant), short tag inside a string literal, dedup across
multiple declarations in one file
Happy to adjust to whatever version this actually ships in.