Page MenuHomePhabricator

Jenkins: Create phpcs sniff to detect text before the first <?php tag
Closed, ResolvedPublic

Description

It would be helpful to comment or downvote if there was whitespace (or possibly any character) before the <?php/<? tag. This causes unexpected output, which can lead to annoying issues.

It could initially be done as a non-voting job.

PHP CodeSniffer might be one way to implement this check.


Version: unspecified
Severity: enhancement

Event Timeline

bzimport raised the priority of this task from to Lowest.Nov 22 2014, 1:19 AM
bzimport set Reference to bz44875.
bzimport added a subscriber: Unknown Object (MLST).

Maybe something as lame as:

grep --perl-regexp '\s+\<\?php' -R mediawiki/core

But that in turns return a toooon of false positive since we have templates using <?php :-D

(In reply to comment #1)

But that in turns return a toooon of false positive since we have templates
using <?php :-D

Yeah, I was going to say, you'd want to check the first line only or we'd probably get a lot of false positives.

Yeah, I meant the first line, just forgot to say it.

I found a good way to do it. The relevant CodeSniffer rule is Generic.PHP.CharacterBeforePHPOpeningTag.

I tested it with:

phpcs --standard=Generic --sniffs=Generic.PHP.CharacterBeforePHPOpeningTag somefile.php

Ah thanks for looking in codesniffer rules!

The Generic.PHP.CharacterBeforePHPOpeningTag rule looks almost like something we could use. Unfortunately it complains whenever the T_OPEN_TAG token is not the very first token, for example when we have several open tags in a file.

A few examples from core:

includes/Feed.php
includes/SkinTemplate.php
includes/api/ApiFormatBase.php
includes/templates/Userlogin.php

Not ideal yet :(

You're right. The error message is not consistent with the actual behavior.

The CharacterBeforePHPOpeningTag sniff is good, but we can't use it as-is because it also warns when you use multiple <?php tag in the file (e.g. Skin templates).

We should create our own version based on CharacterBeforePHPOpeningTag that only warns for the first one.

Krinkle: Over a year since last activity, removing Krinkle as assignee. He know how to re-add if he wants :)

Change 192201 had a related patch set uploaded (by Sumit):
Sniff to detect text before first opening php tag

https://gerrit.wikimedia.org/r/192201

Patch-For-Review

Change 192201 merged by jenkins-bot:
Sniff to detect text before first opening php tag

https://gerrit.wikimedia.org/r/192201

Legoktm assigned this task to Sumit.
Legoktm set Security to None.