Skip to content

Add ShortOpenTag rule: detect PHP short open tags (fixes #771) - #1330

Open
aymericcucherousset wants to merge 1 commit into
phpmd:3.xfrom
aymericcucherousset:feature/short-open-tag-rule
Open

Add ShortOpenTag rule: detect PHP short open tags (fixes #771)#1330
aymericcucherousset wants to merge 1 commit into
phpmd:3.xfrom
aymericcucherousset:feature/short-open-tag-rule

Conversation

@aymericcucherousset

@aymericcucherousset aymericcucherousset commented Aug 17, 2026

Copy link
Copy Markdown

Type: feature
Issue: Resolves #771
Breaking change: no

Adds a new CleanCode rule, ShortOpenTag, that flags PHP files using
the short open tag <? instead of <?php.

Why this took until now

The issue was blocked on PDepend normalizing <? into <?php before
tokenizing, so PHPMD couldn't observe the distinction. That was fixed
upstream in pdepend/pdepend#490 (merged 2021); this repo's
pdepend/pdepend: ^2.16.1 requirement already includes it. In
practice the new rule doesn't consume PDepend's tokenizer at all, it
reads the file and tokenizes it directly with token_get_all(), so
it 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 of
short_open_tag and out of scope. Detecting the bare form correctly
turned out to be ini-sensitive: <? ... only tokenizes as
T_OPEN_TAG when short_open_tag is enabled (the increasingly
uncommon case), when it's disabled, the same text survives as literal
T_INLINE_HTML. The rule checks both token shapes so results don't
depend 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

  • Added to src/main/resources/rulesets/cleancode.xml
  • Implemented in src/main/php/PHPMD/Rule/CleanCode/ShortOpenTag.php
  • Documented in src/site/rst/rules/cleancode.rst
  • Covered in src/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.

@AJenbo

AJenbo commented Aug 17, 2026

Copy link
Copy Markdown
Member

Master (2.x) isn't taking on new features, please target 3.x instead.

@aymericcucherousset
aymericcucherousset changed the base branch from master to 3.x August 17, 2026 19:13
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
aymericcucherousset force-pushed the feature/short-open-tag-rule branch from 98c795c to 66e1084 Compare August 17, 2026 19:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

PHP short tag check

2 participants