WPThemeReview icon indicating copy to clipboard operation
WPThemeReview copied to clipboard

Allow short PHP echo tags

Open justintadlock opened this issue 6 years ago • 6 comments

Short PHP <? tags should be disallowed (see: https://github.com/WPTRT/WordPress-Coding-Standards/issues/2). However, as of PHP 5.4, the short echo syntax <?= is always available. The sniffer should make a distinction between the two.

For those of us no longer coding for PHP versions that have reached EOL, it'd be nice to be able to use the short echo syntax, especially within templates.

Here's an example:

<?= esc_html( $var ) ?>

// vs.

<?php echo esc_html( $var ) ?>

Reference:

  • http://php.net/manual/en/ini.core.php#ini.short-open-tag
  • http://php.net/manual/en/function.echo.php

justintadlock avatar Mar 10 '18 13:03 justintadlock

@justintadlock I've just looked into this. The current sniff we use - Generic.PHP.DisallowShortOpenTag - does not allow for an exception easily, however as it is a good sniff, it would be kind of silly to duplicate it.

As the sniff comes from PHPCS itself, I think the best course of action would be to propose a custom property to be added to the sniff which can toggle whether "short open echo" is allowed or not.

If this would be acceptable to PHPCS, I'd happily create the PR for it.

Would you be willing to write up & open the issue yourself in the PHPCS repo please ?

jrfnl avatar May 31 '18 16:05 jrfnl

If you can point me to the correct place to post the issue, I'd be happy to create a ticket.

justintadlock avatar Jun 01 '18 17:06 justintadlock

@justintadlock https://github.com/squizlabs/PHP_CodeSniffer/issues/ ;-)

jrfnl avatar Jun 01 '18 18:06 jrfnl

@justintadlock Just saw the upstream issue & Greg's response. I had looked at the sniff, but must have completely overlooked the different error codes. Oh well... at least that means this can be fixed really easily.

jrfnl avatar Jun 07 '18 00:06 jrfnl

Cool. I closed that ticket out.

justintadlock avatar Jun 07 '18 11:06 justintadlock

Should this be added in the ruleset? The

<exclude name="Generic.PHP.DisallowShortOpenTag.EchoFound"/>

Or should we leave this out? @justintadlock

dingo-d avatar May 18 '19 12:05 dingo-d