wongjn / vscode-php-sniffer

Visual Studio Code extension for PHP_Codesniffer validation and formatting.
https://marketplace.visualstudio.com/items?itemName=wongjn.php-sniffer
MIT License
46 stars 7 forks source link

False positives when using PHP 7.3 flexible HEREDOC syntax #41

Closed n18l closed 3 years ago

n18l commented 4 years ago

Describe the bug

I'm experiencing what I believe to be false-positive error highlighting when attempting to use PHP 7.3's flexible HEREDOC syntax. From what I can tell, it's failing to consider the HEREDOC terminator as the actual end of a string unless it's in the first column of the editor.

PHPCS itself doesn't flag any errors with the way I'm using HEREDOC in this case, nor does PHP throw errors/warnings; only the extension seems to think something is out of place. My code's actual output works as intended.

To Reproduce

The following contrived example highlights an error:

function outputDiv($content)
{
    if (!$content) { // Line indented incorrectly; expected 0 spaces, found 4 (Generic.WhiteSpace.ScopeIndent.IncorrectExact)
        $content = 'No content';
    }

    // Valid HEREDOC syntax as of PHP 7.3
    $output = <<<HTML
        <div>{$content}</div>
        HTML;

    echo $output;
}

You can resolve this error by moving the HEREDOC terminator to the first column (a pre-7.3 requirement):

function outputDiv($content)
{
    if (!$content) {
        $content = 'No content';
    }

    // Valid HEREDOC syntax as of PHP 7.3, with terminator in column 0
    $output = <<<HTML
        <div>{$content}</div>
HTML;

    echo $output;
}

You can also resolve it by not using HEREDOC at all, naturally:

function outputDiv($content)
{
    if (!$content) {
        $content = 'No content';
    }

    $output = "<div>{$content}</div>";

    echo $output;
}

But the previous example will give an identical error to the first if you forget to close your double-quote, like so:

function outputDiv($content)
{
    if (!$content) { // Line indented incorrectly; expected 0 spaces, found 4 (Generic.WhiteSpace.ScopeIndent.IncorrectExact)
        $content = 'No content';
    }

    // Missing the closing quote, causing the previous error
    $output = "<div>{$content}</div>;

    echo $output;
}

This is what leads me to believe that the HEREDOC string is being treated as "unclosed" unless its terminator begins at column 0.

Expected behavior

No highlighted errors in the first example.

Environment

Extension settings

{
    "phpSniffer.run": "onSave",
    "phpSniffer.onTypeDelay": 250,
    "phpSniffer.executablesFolder": "",
    "phpSniffer.autoDetect": true,
    "phpSniffer.standard": "",
    "phpSniffer.snippetExcludeSniffs" : [],
}

Additional context

I'm not 100% convinced vscode-php-sniffer is to blame for what I'm seeing, but so far it's the only link in the chain I don't have any additional control over. The test cases above were checked in VS Code with all other extensions disabled.

Thanks for any insight you can provide!

wongjn commented 4 years ago

Could you supply your phpcs configuration please (standards used, .xml file)?

n18l commented 4 years ago

Sorry for the slow response on this! Here's the project's current phpcs.xml file:

<?xml version="1.0"?>
<ruleset name="ddm-standard">
    <description>PSR2 based coding standard.</description>

    <file>web/app/themes</file>

    <exclude-pattern>*/twenty*/*</exclude-pattern>

    <exclude-pattern>*/tests/*</exclude-pattern>
    <exclude-pattern>*/cache/*</exclude-pattern>
    <exclude-pattern>*/*.js</exclude-pattern>
    <exclude-pattern>*/*.css</exclude-pattern>
    <exclude-pattern>*/*.xml</exclude-pattern>
    <exclude-pattern>*/autoload.php</exclude-pattern>
    <exclude-pattern>*/vendor/*</exclude-pattern>
    <exclude-pattern>*/node_modules/*</exclude-pattern>
    <exclude-pattern>dist</exclude-pattern>

    <rule ref="PSR1">
        <exclude name="PSR1.Methods.CamelCapsMethodName.NotCamelCaps" />
    </rule>

    <rule ref="PSR2">
        <exclude name="Generic.Files.LineLength.TooLong" />
    </rule>

    <!--PHPCBF doesn't fix this violation automatically so mark it as a warning to prevent CI failures-->
    <rule ref="PSR1.Methods.CamelCapsMethodName.NotCamelCaps">
        <type>warning</type>
        <severity>8</severity>
    </rule>

    <!--PHPCBF doesn't fix this violation automatically so mark it as a warning to prevent CI failures-->
    <rule ref="Generic.Files.LineLength">
        <properties>
            <property name="lineLimit" value="120"/>
            <property name="absoluteLineLimit" value="120"/>
        </properties>
        <type>warning</type>
        <severity>8</severity>
    </rule>
</ruleset>
michaelbeltran commented 4 years ago

I fix this changing a variable in the settings.json

"php.validate.executablePath": "C:/php/php.exe"

this line allows vs to use a more updated version of php lint

wongjn commented 3 years ago

Could not reproduce, does not seem an issue with the extension anyway — would probably be a bug in PHP_CodeSniffer.