squizlabs / PHP_CodeSniffer

PHP_CodeSniffer tokenizes PHP files and detects violations of a defined set of coding standards.
BSD 3-Clause "New" or "Revised" License
10.66k stars 1.48k forks source link

Issue with inline PHP and double quote usage #901

Closed u01jmg3 closed 8 years ago

u01jmg3 commented 8 years ago

Is there any way to prevent the DoubleQuoteUsage rule from firing suggesting to use single quotes rather than double quotes when inline PHP is quoted?

<rule ref="Squiz.Strings.DoubleQuoteUsage.ContainsVar">
    <severity>0</severity>
</rule>

<script type="text/php">
if(isset($pdf)){
    $pdf->page_text(30, $h-30, strtoupper('<?php echo implode(" ", array($forename1{0} . "." . (strlen($surname) > 13 ? substr($surname, 0, 6) . "..." . substr($surname, -5, 5) : $surname), "(" . $personal_id . "-" . $application_id . ")")); ?>'), $font, 10, array(0, 0, 0));
}
</script>
gsherwood commented 8 years ago

Your sample code doesn't produce any DoubleQuoteUsage errors in my testing. What are you seeing?

u01jmg3 commented 8 years ago

Apologies - the sample code wasn't complete. This is an extract from a custom footer for use with dompdf and in order that the inline PHP can be parsed by dompdf, you must use double quotes unescaped.

 2 | ERROR | [x] String " " does not require double quotes; use
   |       |     single quotes instead
 2 | ERROR | [x] String "." does not require double quotes; use
   |       |     single quotes instead
 2 | ERROR | [ ] Inline shorthand IF statement requires brackets
   |       |     around comparison
 2 | ERROR | [x] String "..." does not require double quotes; use
   |       |     single quotes instead
 2 | ERROR | [x] String "(" does not require double quotes; use
   |       |     single quotes instead
 2 | ERROR | [x] String "-" does not require double quotes; use
   |       |     single quotes instead
 2 | ERROR | [x] String ")" does not require double quotes; use
   |       |     single quotes instead
gsherwood commented 8 years ago

The problem is that PHPCS (and the PHP tokenizer) is not seeing any PHP code at all until you hit the <?php token inside the string. From that point, you're going to potentially have problems because the PHP outside these tags is going to be ignored.

There isn't anything the sniff can do to detect that you are writing PHP outside of PHP tags, so you're just going to need to ignore these errors for this file.

Here is an extract from the annotated ruleset to help with that:

 <!--
    You can also hard-code ignore patterns for specific sniffs,
    a feature not available on the command line. Please note that
    all sniff-specific ignore patterns are checked using absolute paths.

    The code here will hide all messages from the Squiz DoubleQuoteUsage
    sniff for files that match either of the two exclude patterns.
 -->
 <rule ref="Squiz.Strings.DoubleQuoteUsage">
    <exclude-pattern>*/tests/*</exclude-pattern>
    <exclude-pattern>*/data/*</exclude-pattern>
 </rule>

Is this a possible solution for you?

u01jmg3 commented 8 years ago

Thanks @gsherwood - I thought there would have to be some sort of exclusion to get around this. This will work

gsherwood commented 8 years ago

Good news. Thanks for getting back to me.