标签: phpcodesniffer

在 PHP CodeSniffer 中出错

当我尝试在 PHPStorm(10.0.3) 中打开文件时收到以下消息。

phpcs: PHP Fatal error: Call to undefined method PHP_CodeSniffer_File::recordMetric() in /usr/share/php/PHP/CodeSniffer/Standards/PSR2/Sniffs/Files/ClosingTagSniff.php on line 91
Run Code Online (Sandbox Code Playgroud)

有人知道这是什么吗?

php phpstorm phpcodesniffer

5
推荐指数
1
解决办法
762
查看次数

PHP CodeSniffer 在方法中忽略/排除下划线规则

运行 phpcs 时,出现Protected member variable "myMethod" must contain a leading underscore. 我如何排除/忽略这个错误ruleset.xml

<?xml version="1.0"?>
<ruleset name="PSR2">
 <description>The PSR-2 coding standard.</description>
 <arg name="tab-width" value="4"/>
 <!-- 2. General -->
 <!-- 2.1 Basic Coding Standard -->
 <!-- Include the whole PSR-1 standard -->
 <rule ref="PSR1"/>
 <!-- 2.2 Files -->
 <!-- All PHP files MUST use the Unix LF (linefeed) line ending. -->
 <rule ref="Generic.Files.LineEndings">
  <properties>
   <property name="eolChar" value="\r\n"/>
  </properties>
 </rule>
 <!-- All PHP files MUST end with a single …
Run Code Online (Sandbox Code Playgroud)

php zend-framework codesniffer phpcodesniffer phpcs

5
推荐指数
1
解决办法
3524
查看次数

php代码嗅探器什么都不返回……我的代码是标准的吗?

我有一个 Laravel 应用程序(用 composer 创建),我试图确保它符合 PHP 编码标准(级别 PSR-1)。我跑:

$ phpcs --standard=PSR1 my_app/
Run Code Online (Sandbox Code Playgroud)

并在几秒钟内返回一个新的、空的、随时可用的命令行:

$
Run Code Online (Sandbox Code Playgroud)

这是否意味着我的代码符合 PSR-1 中的所有要求和标准?它只做同样的事情:

$ phpcs my_app/
$ phpcs --standard=PEAR my_app/
$ phpcs --standard=PSR1 --report=summary lauras_app/
Run Code Online (Sandbox Code Playgroud)

我只想确保如果命令什么都不返回,那意味着我的代码是标准的。谢谢!

php pear code-standards phpcodesniffer psr-1

5
推荐指数
1
解决办法
1401
查看次数

PHPcs xml 配置禁用一些规则

我了解如何使用 xml 配置 phpcs,但找不到如何禁用某些嗅探。这是我当前的配置(甚至不知道这是否正确):

\n\n
<?xml version="1.0" encoding="UTF-8"?>\n<ruleset name="custom" \n  xmlns="http://pmd.sf.net/ruleset/1.0.0" \n  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0 http://pmd.sf.net/ruleset_xml_schema.xsd" xsi:noNamespaceSchemaLocation="http://pmd.sf.net/ruleset_xml_schema.xsd">\n  <rule ref="rulesets/codesize.xml"/>\n  <rule ref="rulesets/controversial.xml/Superglobals"/>\n  <rule ref="rulesets/controversial.xml/CamelCaseParameterName"/>\n  <rule ref="rulesets/controversial.xml/CamelCaseVariableName"/>\n  <rule ref="rulesets/design.xml"/>\n  <rule ref="rulesets/naming.xml/ShortMethodName"/>\n  <rule ref="rulesets/naming.xml/ConstructorWithNameAsEnclosingClass"/>\n  <rule ref="rulesets/naming.xml/ConstantNamingConventions"/>\n  <rule ref="rulesets/naming.xml/BooleanGetMethodName">\n    <properties>\n      <property name="checkParameterizedMethods" value="true"/>\n    </properties>\n  </rule>\n  <rule ref="rulesets/unusedcode.xml"/>\n\n  <arg name="tab-width" value="2"/>\n  <rule ref="Generic.WhiteSpace.ScopeIndent">\n    <properties>\n      <property name="indent" value="2"/>\n    </properties>\n  </rule>\n\n  <rule ref="Generic.Files.LineLength">\n    <properties>\n      <property name="lineLimit" value="140"/>\n      <property name="absoluteLineLimit" value="0"/>\n    </properties>\n  </rule>\n</ruleset>\n
Run Code Online (Sandbox Code Playgroud)\n\n

我想禁用这些:

\n\n
    \n
  • [phpcs] @copyright tag must contain a year and the name of the …

php phpcodesniffer phpcs

5
推荐指数
3
解决办法
1万
查看次数

如何设置 PHP CodeSniffer 来扩展 WordPress 编码标准 + VSCode 中的自动修复错误?

如何使用扩展 WordPress 编码标准的个人规则为项目设置 PHP CodeSniffer + 保存时在 VSCode 中自动修复错误?

我已经全局安装了 CodeSniffer

composer global require "squizlabs/php_codesniffer=*"
Run Code Online (Sandbox Code Playgroud)

WordPress 编码标准安装在主题文件夹内(因此它们位于项目内/wp-content/themes/bideja/wpcs

git clone -b master https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards.git wpcs
Run Code Online (Sandbox Code Playgroud)

我创建的主题文件夹内部phpcs.xml应继承 WordPress 标准,以便我可以进一步自定义它

<?xml version="1.0"?>
<ruleset name="Bideja">
    <description>Sniffs</description>

    <config name="installed_paths" value="wpcs" />

    <arg value="s"/>

    <exclude-pattern>assets/*</exclude-pattern>
    <exclude-pattern>node_modules/*</exclude-pattern>
    <exclude-pattern>vendor/*</exclude-pattern>

    <rule ref="WordPress-VIP">
        <exclude name="Generic.Files.EndFileNewline.NotFound" />
        <exclude name="WordPress.PHP.YodaConditions.NotYoda" />
    </rule>

</ruleset>
Run Code Online (Sandbox Code Playgroud)

在主题内的 Bash 终端中,我可以扫描页面中的错误并修复它们

phpcs page.php
phpbcf page.php 
Run Code Online (Sandbox Code Playgroud)

但是 VSCode 如何在保存时修复它们呢?我收到弹出错误:

phpcs: Referenced sniff "WordPress-VIP" does not exist
Run Code Online (Sandbox Code Playgroud)

我应该在 User settings.json 中放置什么?

"phpcs.enable": true,
"phpcs.standard": …
Run Code Online (Sandbox Code Playgroud)

php wordpress phpcodesniffer visual-studio-code

5
推荐指数
1
解决办法
4005
查看次数

PHP CodeSniffer:错误:指定的嗅探代码“Generic.Files.LineEndings.InvalidEOLChar”无效

我试图在我的 Windows 机器上排除对 EOL 字符的检查总是导致此错误消息:

>vendor\bin\phpcs.bat --standard=PSR2 --exclude=Generic.Files.LineEndings.InvalidEOLChar src\version.php
ERROR: The specified sniff code "Generic.Files.LineEndings.InvalidEOLChar" is invalid

Run "phpcs --help" for usage information
Run Code Online (Sandbox Code Playgroud)

无法弄清楚我做错了什么。我已经通过 composer 安装了 PHP CodeSniffer 并且正在运行 3.4.0 版。

codesniffer phpcodesniffer phpcs

4
推荐指数
1
解决办法
597
查看次数

PhpStorm - 如何检测PHP错误"不应静态调用非静态方法"?

使用PHP,静态方法既可以用于静态方法,也可以用于非静态方法,而非静态方法只能用于非静态方法.这就是调用动态方法静态生成E_STRICT错误的原因.

例如:

<?php

class Example
{
    public function foo() {
        return "Foo";
    }

    public static function bar() {
        return "Bar";
    }
}

$ex = new Example();

// Non-static call
echo $ex->bar();

// Static call on a non-static method
// PHP Error "Strict standards: Non-static method should not be called statically"
// ERROR NOT DETECTED BY PHPSTORM!
echo Example::foo();
Run Code Online (Sandbox Code Playgroud)

最后一行将生成此PHP错误(这是逻辑): 在此输入图像描述

我目前正在开发一个大型PHP应用程序,在某些PHP文件中静态调用非静态方法.这是一个非常老的PHP版本的问题,但我们决定迁移到最新的PHP版本.

手动检查所有项目文件,以确定这个错误的语法将太长(+ 1000文件)!

PhpStorm的内置代码检查功能在分析的源代码中未检测到此类错误.为什么?我应该配置什么?怎么样?

下面,我在PhpStorm中的PHP代码检查配置:

在此输入图像描述

谢谢!

php code-inspection phpstorm phpmd phpcodesniffer

3
推荐指数
1
解决办法
1522
查看次数

需要使用什么嗅探来强制执行camelCase变量命名约定?

我正在研究一个专有的遗留代码库,一些变量是驼峰式的,而另一些则是蛇形的.我想清理并强制执行只有驼峰的变量名称,但我似乎无法找到它的嗅觉.以下是我的自定义规则集的内容.

The ruleset.xml standard contains 68 sniffs

Generic (22 sniffs)
-------------------
  Generic.Classes.DuplicateClassName
  Generic.CodeAnalysis.ForLoopShouldBeWhileLoop
  Generic.CodeAnalysis.UnconditionalIfStatement
  Generic.CodeAnalysis.UnnecessaryFinalModifier
  Generic.CodeAnalysis.UnusedFunctionParameter
  Generic.CodeAnalysis.UselessOverridingMethod
  Generic.Commenting.Fixme
  Generic.Commenting.Todo
  Generic.ControlStructures.InlineControlStructure
  Generic.Files.ByteOrderMark
  Generic.Files.LineEndings
  Generic.Files.LineLength
  Generic.Formatting.DisallowMultipleStatements
  Generic.Formatting.NoSpaceAfterCast
  Generic.Functions.FunctionCallArgumentSpacing
  Generic.NamingConventions.CamelCapsFunctionName
  Generic.NamingConventions.UpperCaseConstantName
  Generic.PHP.DisallowShortOpenTag
  Generic.PHP.LowerCaseConstant
  Generic.PHP.LowerCaseKeyword
  Generic.WhiteSpace.DisallowTabIndent
  Generic.WhiteSpace.ScopeIndent

PEAR (5 sniffs)
---------------
  PEAR.Commenting.InlineComment
  PEAR.Formatting.MultiLineAssignment
  PEAR.Functions.ValidDefaultValue
  PEAR.WhiteSpace.ScopeClosingBrace
  PEAR.WhiteSpace.ScopeIndent

PSR1 (3 sniffs)
---------------
  PSR1.Classes.ClassDeclaration
  PSR1.Files.SideEffects
  PSR1.Methods.CamelCapsMethodName

PSR2 (12 sniffs)
----------------
  PSR2.Classes.ClassDeclaration
  PSR2.Classes.PropertyDeclaration
  PSR2.ControlStructures.ControlStructureSpacing
  PSR2.ControlStructures.ElseIfDeclaration
  PSR2.ControlStructures.SwitchDeclaration
  PSR2.Files.ClosingTag
  PSR2.Files.EndFileNewline
  PSR2.Methods.FunctionCallSignature
  PSR2.Methods.FunctionClosingBrace
  PSR2.Methods.MethodDeclaration
  PSR2.Namespaces.NamespaceDeclaration
  PSR2.Namespaces.UseDeclaration

Squiz (26 sniffs)
-----------------
  Squiz.Classes.ValidClassName
  Squiz.ControlStructures.ControlSignature
  Squiz.ControlStructures.ForEachLoopDeclaration
  Squiz.ControlStructures.ForLoopDeclaration
  Squiz.ControlStructures.LowercaseDeclaration
  Squiz.Functions.FunctionDeclarationArgumentSpacing
  Squiz.Functions.FunctionDeclaration
  Squiz.Functions.LowercaseFunctionKeywords
  Squiz.Functions.MultiLineFunctionDeclaration
  Squiz.PHP.CommentedOutCode
  Squiz.PHP.Eval …
Run Code Online (Sandbox Code Playgroud)

php codesniffer phpcodesniffer phpcs

3
推荐指数
1
解决办法
1679
查看次数

PHP CodeSniffer WordPress 通过 Composer

有没有办法通过Composer每个项目的PHP_CodeSniffer安装PHPCodeSnifferWordPress 编码标准?我已经将两者都安装为开发依赖项,并在 CodeSniffer.conf 中设置了 WordPress 编码标准的安装路径。不幸的是,当我在终端中运行命令时,phpcs 无法解析到配置文件,因为它在“vendor”文件夹中查找“vendor/squizlabs/php_codesniffer/CodeSniffer.conf”

这是我的项目设置:

作曲家.json

{
    "require-dev": {
        "squizlabs/php_codesniffer": "^3.2",
        "wp-coding-standards/wpcs": "^0.14.0"
    }
}
Run Code Online (Sandbox Code Playgroud)

代码嗅探器配置文件

<?php
    $phpCodeSnifferConfig = array (
        'installed_paths' => 'vendor/wp-coding-standards/wpcs',
    )
?>
Run Code Online (Sandbox Code Playgroud)

终端

vendor/bin/phpcs -p THEME_NAME --standard=WordPress
Run Code Online (Sandbox Code Playgroud)

“错误:未安装“WordPress”编码标准。已安装的编码标准是 PEAR、Zend、PSR2、MySource、Squiz 和 PSR1”

php wordpress composer-php phpcodesniffer

3
推荐指数
1
解决办法
1778
查看次数

是否可以将所有 PHP 混乱检测器检查替换为 PHP 代码嗅探器检查?

我有一个项目同时使用 PHPMD(PHP 混乱检测器)和 PHPCS(PHP 代码嗅探器),这让我想知道是否所有 PHPMD 检查都可以替换为 PHPCS 检查,还是最好同时使用两者?

PHPMD 检查列表似乎在这里:https ://phpmd.org/rules/index.html但我没有找到任何关于 PHPMD 提供的好处的信息。

php phpmd phpcodesniffer phpcs

3
推荐指数
1
解决办法
113
查看次数