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

Open PHP tag in XML, impacts performance of PHP Tokenizer #3821

Closed victor-v-rad closed 1 year ago

victor-v-rad commented 1 year ago

Describe the bug Open PHP tag in XML, impacts performance of PHP Tokenizer

Custom ruleset

<ruleset name="TestXml">
    <arg name="extensions" value="xml"/>
    <rule ref="Generic.Files.InlineHTML">
        <properties>
            <property name="lineLimit" value="420"/>
            <property name="absoluteLineLimit" value="0"/>
        </properties>
    </rule>
</ruleset>

To reproduce Steps to reproduce the behavior:

  1. clone https://github.com/venustheme/magento2-image-slider
  2. Run phpcs --standard=path/to/custom_ruleset.xml path/to/magento2-image-slider

Actual Results phpcs never finishes

Expected behavior phpcs finishes almost instantly

Versions (please complete the following information):

Additional context The issue is in this file https://github.com/venustheme/magento2-image-slider/blob/9bba8c275c81997e066cd8e3c87d2498f477692e/etc/widget.xml#L3

image

Opened PHP tag in xml file makes PHP Tokenizer parse XML as is it is PHP code

jrfnl commented 1 year ago

@victor-v-rad The problem is that you are telling PHPCS to parse XML files as if they were PHP by having this <arg name="extensions" value="xml"/> in your ruleset.

PHPCS is not suitable for analysing XML files and would never attempt to parse them if it wasn't for that extensions setting in your ruleset, so the real question is: what are you trying to do with that ? Why is that setting there ?

gsherwood commented 1 year ago

I'm not sure why there is an open PHP tag in your XML file, but it's causing PHPCS to attempt to parse the XML as PHP.

I'd suggest closing the PHP tag so the file is valid again, or just not telling PHPCS to process XML files as PHP files, as @jrfnl has mentioned already.

victor-v-rad commented 1 year ago

Thank you @gsherwood and @jrfnl for your advice and sorry for the late reply. As a workaround, I created a dummy tokenizer for XML to be able to sniff and validate XML content.