Skip to content

Conversation

ghunti
Copy link
Contributor

@ghunti ghunti commented Apr 25, 2017

I was using phpcbf to fix some legacy files that contained short opening tags without an empty space between them and the content: <?echo (this is valid to the PHP engine)
The problem is that phpcbf was replacing the short opening tag with a huge string like <?phpphpphpphpphpphpphpphpphpphpphpphpphpphpphpphpphpphpphpphpphpphpphpphpphpphpecho

I've added a test that shows the failure + the fix that I found for the problem.
Keep up the good work.
Cheers

Copy link
Contributor

@jrfnl jrfnl left a comment

Choose a reason for hiding this comment

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

I suppose I shouldn't be surprised that <?echo is actually interpreted correctly by PHP 😖

Good change.

if ($fix === true) {
$phpcsFile->fixer->replaceToken($stackPtr, '<?php');
$correctOpening = '<?php';
if (isset($tokens[$stackPtr + 1]) && $tokens[$stackPtr + 1]['code'] !== T_WHITESPACE) {
Copy link
Contributor

Choose a reason for hiding this comment

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

You'll need to make the isset($tokens[$stackPtr + 1]) explicitely test against true for the build to pass:
if (isset($tokens[$stackPtr + 1]) === true && ...)

@ghunti
Copy link
Contributor Author

ghunti commented Apr 25, 2017

@jrfnl The requested fix is pushed. Travis is failing with my commit, was it caused by some problem in my code?
As a side note, <?if also works :-p So basically you don't need a whitespace after the short opening tag!

@jrfnl
Copy link
Contributor

jrfnl commented Apr 25, 2017

Travis is failing with my commit, was it caused by some problem in my code?

@ghunti Still a few more small code style issues to fix, nothing major - see the travis build details for pointers.

@ghunti
Copy link
Contributor Author

ghunti commented Apr 25, 2017

@jrfnl Haven't realize that phpcs uses its own standard ^_^
It should be fine now (fingers crossed)

@gsherwood gsherwood changed the title Avoid creating invalid open tags when replacing short open tags PHPCBF can't fix short open tags when they are not followed by a space Apr 26, 2017
@gsherwood gsherwood merged commit 8f656d2 into squizlabs:master Apr 26, 2017
gsherwood added a commit that referenced this pull request Apr 26, 2017
@gsherwood
Copy link
Member

Thanks a lot for finding and fixing this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

3 participants