Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flag for allowing short echo tags #2045

Closed
justintadlock opened this issue Jun 6, 2018 · 4 comments
Closed

Flag for allowing short echo tags #2045

justintadlock opened this issue Jun 6, 2018 · 4 comments

Comments

@justintadlock
Copy link

This is a request from the WordPress themes codesniffer, which is an extension of this project. See original ticket: WPTT/WPThemeReview#142

Essentially, we want to allow short echo tags (<?=), which are always available since PHP 5.4.

Reference:

I'm not familiar enough with the code on the project to say exactly what needs to be done, so I'm just going to copy/paste the response from one of the devs on the project:

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.

@gsherwood
Copy link
Member

The easiest way to do this is to exclude that specific error message. The short echo and short open error messages have different codes, so you can do this in your ruleset:

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

That includes the sniff as normal, but then excludes the specific error message that complains when short echos are found. It will still report errors for short open tags as they use the Generic.PHP.DisallowShortOpenTag.Found error code. You can see error codes in reports by using the -s command line argument.

Does that do what you're after?

@jrfnl
Copy link
Contributor

jrfnl commented Jun 7, 2018

@gsherwood Thanks for the response. I'd completely overlooked the different error code.

@justintadlock
Copy link
Author

I'm going to close this issue then since it seems like we have what we need. Thanks for the response!

@gsherwood gsherwood removed this from Track in PHPCS v3 Development Jun 7, 2018
Alkarex added a commit to Alkarex/FreshRSS that referenced this issue Oct 23, 2019
Alkarex added a commit to FreshRSS/FreshRSS that referenced this issue Oct 23, 2019
* Take advantage of PHP 5.4+ short echo

https://php.net/migration54.new-features thanks to
#2495

Use `<?=  ?>` instead of `<?php echo; ?>`
10kB of code saved :-)

Done with regular expression:
```
<\?php echo (.+?);? *\?>
<?= \1 ?>
```

* Try Travis fix

squizlabs/PHP_CodeSniffer#2045 (comment)
javerous pushed a commit to javerous/FreshRSS that referenced this issue Jan 20, 2020
* Take advantage of PHP 5.4+ short echo

https://php.net/migration54.new-features thanks to
FreshRSS#2495

Use `<?=  ?>` instead of `<?php echo; ?>`
10kB of code saved :-)

Done with regular expression:
```
<\?php echo (.+?);? *\?>
<?= \1 ?>
```

* Try Travis fix

squizlabs/PHP_CodeSniffer#2045 (comment)
@puwnd
Copy link

puwnd commented Jun 23, 2023

In my case the above did not work. I think in the latest version, there is another syntax.
The following works for me:

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

My version is: PHP_CodeSniffer version 3.7.2 (stable) by Squiz

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants