简体   繁体   English

是否有使用 PHP 代码嗅探器强制执行严格比较 (===) 的规则?

[英]Is there a rule for enforcing strict comparison (===) using PHP Code Sniffer?

As title says.正如标题所说。 I am looking for a rule similar to eslint's eqeqeq , but for PHP.我正在寻找类似于 eslint 的eqeqeq的规则,但适用于 PHP。 I would want to enforce the use of === / !== instead of == / != .我想强制使用=== / !==而不是== / !=

I have tried googling and searched through their repo as well, but unfortunately to no avail.我也尝试过谷歌搜索并搜索了他们的回购,但不幸的是无济于事。 Just wanna ask here if someone knows of a rule that I have missed before I open an issue on Github :)只是想在这里问一下是否有人知道我在 Github 上打开问题之前错过的规则:)

Try including the Squiz.Operators.ComparisonOperatorUsage sniff.尝试包括Squiz.Operators.ComparisonOperatorUsage嗅探。 It enforces strict type comparisons, including banning the use of !$foo in favour of === false .它强制执行严格的类型比较,包括禁止使用!$foo以支持=== false

It also bans implicit true comparisons, so you can't do something like if ($foo) , but you can disable that by excluding the Squiz.Operators.ComparisonOperatorUsage.ImplicitTrue error code if that it too strict for your standard.它还禁止隐式真比较,因此您不能执行if ($foo) ,但是您可以通过排除Squiz.Operators.ComparisonOperatorUsage.ImplicitTrue错误代码来禁用它,如果它对您的标准来说太严格了。

The Slevomat Coding Standard contains a rule specifically for disallowing loose comparison operators. Slevomat 编码标准包含专门用于禁止松散比较运算符的规则。

Install安装

Install the coding standard globally through Composer or locally to your project directory.通过 Composer 将编码标准全局安装到您的项目目录中。

composer global require slevomat\\coding-standard

composer require slevomat\\coding-standard --dev

Reference参考

phpcs is often configured through a phpcs.xml file. phpcs 通常通过phpcs.xml文件进行配置。 Add a reference to the slevomat coding standard inside this file using the config tag.使用 config 标签在此文件中添加对 slevomat 编码标准的引用。

<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="PHP_CodeSniffer" xsi:noNamespaceSchemaLocation="phpcs.xsd">
    <config name="installed_paths" value="{path}"/>
...
</ruleset>

The {path} value may be an absolute path if installed globally or a relative path if installed in your project. {path} 值可能是一个绝对路径(如果全局安装)或相对路径(如果安装在您的项目中)。

C:\\Users\\{username}\\AppData\\Roaming\\Composer\\vendor\\slevomat\\coding-standard

vendor\\slevomat\\coding-standard

Configure配置

This is an example of a configured phpcs.xml file.这是已配置的 phpcs.xml 文件的示例。 I've told phpcs to use the PSR2 rules and also use the DisallowEqualOperators rule from the Slevomat Coding Standard.我已经告诉 phpcs 使用 PSR2 规则并使用 Slevomat 编码标准中的 DisallowEqualOperators 规则。

<ruleset ...>
    <config name="installed_paths" value="vendor\slevomat\coding-standard"/>

    <!-- Add your rules below this line -->

    <!-- Use the PSR2 rules that are included with phpcs. -->
    <rule ref="PSR2">

    <!--
        https://github.com/slevomat/coding-standard#slevomatcodingstandardoperatorsdisallowequaloperators-
        Disallows using loose == and != comparison operators. Use === and !== instead, they are much more secure and predictable.
    -->
    <rule ref="SlevomatCodingStandard.Operators.DisallowEqualOperators"/>
</ruleset>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM