当我尝试在 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)
有人知道这是什么吗?
运行 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) 我有一个 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)
我只想确保如果命令什么都不返回,那意味着我的代码是标准的。谢谢!
我了解如何使用 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>\nRun Code Online (Sandbox Code Playgroud)\n\n我想禁用这些:
\n\n[phpcs] @copyright tag must contain a year and the name of the …如何使用扩展 WordPress 编码标准的个人规则为项目设置 PHP CodeSniffer + 保存时在 VSCode 中自动修复错误?
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) 我试图在我的 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 版。
使用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版本.
手动检查所有项目文件,以确定这个错误的语法将太长(+ 1000文件)!
PhpStorm的内置代码检查功能在分析的源代码中未检测到此类错误.为什么?我应该配置什么?怎么样?
下面,我在PhpStorm中的PHP代码检查配置:
谢谢!
我正在研究一个专有的遗留代码库,一些变量是驼峰式的,而另一些则是蛇形的.我想清理并强制执行只有驼峰的变量名称,但我似乎无法找到它的嗅觉.以下是我的自定义规则集的内容.
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) 有没有办法通过Composer为每个项目的PHP_CodeSniffer安装PHPCodeSniffer和WordPress 编码标准?我已经将两者都安装为开发依赖项,并在 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”
我有一个项目同时使用 PHPMD(PHP 混乱检测器)和 PHPCS(PHP 代码嗅探器),这让我想知道是否所有 PHPMD 检查都可以替换为 PHPCS 检查,还是最好同时使用两者?
PHPMD 检查列表似乎在这里:https ://phpmd.org/rules/index.html但我没有找到任何关于 PHPMD 提供的好处的信息。
phpcodesniffer ×10
php ×9
phpcs ×5
codesniffer ×3
phpmd ×2
phpstorm ×2
wordpress ×2
composer-php ×1
pear ×1
psr-1 ×1