标签: phpcs

需要使用什么嗅探来强制执行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 规则

如何找到我需要的 PHP CodeSniffer 规则?

例如,我希望函数参数和数组不要像这样间隔

function asd( $var, $var2 = '' ) {
    $array = [ $arg, $arg2 ];
}
Run Code Online (Sandbox Code Playgroud)

相反,它应该是这样的:

function asd($var, $var2 = '') {
    $array = [$arg, $arg2];
}
Run Code Online (Sandbox Code Playgroud)

我希望 phpcbf 能够修复这些问题,但我不知道如何找到执行此操作的规则。

php phpcs

2
推荐指数
1
解决办法
3528
查看次数

自定义 phpcs 规则集不遵守排除模式声明

我有一个自定义的 phpcs 规则集文件,我在其中包含了几个规则集,并且还从某些目录(如测试或供应商)中排除了文件。

完整的 phpcs.xml 配置文件位于https://github.com/sudar/bulk-move/blob/master/phpcs.xml下面我提到了一些有趣的部分

<file>./</file>

<!-- Exclude test directories -->
<exclude-pattern>tests/*</exclude-pattern>

<!-- PHP Compatibility -->
<config name="testVersion" value="5.2-"/>
<rule ref="PHPCompatibility">
</rule>
Run Code Online (Sandbox Code Playgroud)

现在,当我phpcs从项目的根目录运行命令时,它从测试目录中排除了文件,并仅在其他文件上运行 phpcs。

在 PhpStorm 中,我在配置 phpcs 时选择了这个 phpcs.xml 文件作为自定义编码标准。但是当我从测试目录(被排除在外)编辑文件时,PhpStorm 仍然使用自定义编码标准调用 phpcs 并显示警告。

所以我的问题是如何告诉 PhpStorm 排除在 phpcs.xml 规则集文件中排除的文件?

php phpstorm phpcs

2
推荐指数
1
解决办法
1634
查看次数

错误:类“路径”不符合 PSR-4 配置

我正在尝试 PHP 编码标准包:https://github.com/inpsyde/php-coding-standards,但是当我开始运行 phpcs 时,我得到:

nickan@nickan-VirtualBox:~/src/wordpress/wp-content/plugins/my-plugin$ vendor/bin/phpcs ./src/model/Users.php 
E 1 / 1 (100%)



FILE: /home/src/wordpress/wp-content/plugins/my-plugin/src/model/Users.php
-------------------------------------------------------------------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
-------------------------------------------------------------------------------------------------------------------------------
 5 | ERROR | Class 'Plugin\Model\Users', located at
   |       | '/home/src/wordpress/wp-content/plugins/my-plugin/src/model/Users.php', is not
   |       | compliant with PSR-4 configuration. (Inpsyde.CodeQuality.Psr4.InvalidPSR4)
Run Code Online (Sandbox Code Playgroud)

这是路径中的文件my-plugin/src/model/Users.php

<?php declare(strict_types=1);

namespace Test\Model;

class Users
{

}
Run Code Online (Sandbox Code Playgroud)

我不明白出了什么问题,代码工作正常,但错误不断出现。我尝试使用不同的命名空间、更改文件夹等,但仍然无济于事,将不胜感激。谢谢。

psr-4 phpcs

2
推荐指数
1
解决办法
453
查看次数

类型提示的 PHPCS 规则

是否有规则来检查所有函数的类型提示?

/**
 * Set the part name.
 *
 * @param   string    $name   The part name.
 */
public function setName(string $name) : void
{
    $this->name = $name;
}
Run Code Online (Sandbox Code Playgroud)

例如,它必须在参数前面有一个类型,而函数必须有一个指定的返回类型。

phpcs php-7.1

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

PHP Code Sniffer和PhpStorm出错

17:18   PHP Code Sniffer
        No response from /home/my_user/.composer/vendor/squizlabs/php_codesniffer/bin/phpcs after 5000 ms
        Disable inspection
Run Code Online (Sandbox Code Playgroud)

任何的想法?我已经尝试了很多解决方案,比如禁用xdebug,增加响应时间.

它在PhpStorm中不起作用,但在终端中工作.

phpstorm phpcs

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

如何防止 PHP CS Fixer 修复特定函数名称?

当使用 PHP CS Fixer 解决文件保存时的语法错误并使用Symfony规则集时,任何非驼峰命名法的函数名称都将被纠正(如预期)。

然而,在编写基于 PHP 单元的测试时,通常的做法是将函数名称命名为Snake_case。这通常意味着 PHP CS Fixer 会自动解决问题,即使不需要。

是否可以在函数上方放置特定的注释来防止这种情况发生?

php annotations phpcs php-cs-fixer

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