roslov / psr12ext
PSR-12 扩展编码标准
11.0.0
2024-04-12 13:49 UTC
Requires
- php: ^7.2|^8.0
- slevomat/coding-standard: ^8.15
README
此存储库扩展了 PSR-12 编码风格。
除了 PSR-12,此编码风格还包含来自 Slevomat Coding Standard 和 PHP_CodeSniffer 的某些规则。
使用此编码风格
安装 CodeSniffer 后,您可以使用以下语法使用自定义编码风格启动它
composer require --dev roslov/psr12ext ./vendor/bin/phpcs --extensions=php --standard=PSR12Ext /app
如果您计划将代码质量检查与您的 IDE 或 CI 集成,请在项目的根目录中创建 ruleset.xml
文件。
此文件可以包含您想禁用的规则列表以及您的自定义设置。例如
<?xml version="1.0" encoding="UTF-8"?> <ruleset name="project-code-styling"> <description>Local Project Coding Standard</description> <!-- Excludes vendor and temporary folders --> <exclude-pattern>vendor/*</exclude-pattern> <exclude-pattern>var/*</exclude-pattern> <!-- Base standards (if not detected) --> <config name="installed_paths" value="vendor/roslov/psr12ext" /> <!-- Base rules --> <rule ref="PSR12Ext"> <!-- Ignores line endings --> <exclude name="Generic.Files.LineEndings" /> <!-- Disables strict type requirement --> <exclude name="SlevomatCodingStandard.TypeHints.DeclareStrictTypes" /> <!-- Ignores missing parentheses in class instantiation --> <exclude name="PSR12.Classes.ClassInstantiation.MissingParentheses" /> </rule> <!-- Rules for tests --> <rule ref="PSR1.Classes.ClassDeclaration.MissingNamespace"> <exclude-pattern>tests/*</exclude-pattern> </rule> <rule ref="PSR2.Methods.MethodDeclaration.Underscore"> <exclude-pattern>tests/*</exclude-pattern> </rule> </ruleset>
此编码标准中使用的规则列表
所有使用的规则及其选项均描述在 PSR12Ext/ruleset.xml 中。
以下仅列出规则的名称
- PSR12 的所有规则:
- PSR1 的所有规则:
- Generic.Files.ByteOrderMark
- Generic.NamingConventions.UpperCaseConstantName
- Generic.PHP.DisallowAlternativePHPTags
- Generic.PHP.DisallowShortOpenTag
- Generic.PHP.DisallowShortOpenTag.EchoFound
- Squiz.Classes.ValidClassName
- Generic.ControlStructures.InlineControlStructure
- Generic.Files.LineEndings
- Generic.Files.LineLength
- Generic.Formatting.DisallowMultipleStatements
- Generic.Functions.FunctionCallArgumentSpacing
- Generic.PHP.LowerCaseConstant
- Generic.PHP.LowerCaseKeyword
- Generic.PHP.LowerCaseType
- Generic.WhiteSpace.DisallowTabIndent
- Generic.WhiteSpace.IncrementDecrementSpacing
- Generic.WhiteSpace.ScopeIndent
- PEAR.Functions.ValidDefaultValue
- PSR2.Classes.ClassDeclaration
- PSR2.Classes.PropertyDeclaration
- PSR2.ControlStructures.ElseIfDeclaration
- PSR2.ControlStructures.SwitchDeclaration
- PSR2.Files.ClosingTag
- PSR2.Files.EndFileNewline
- PSR2.Methods.FunctionCallSignature
- PSR2.Methods.FunctionCallSignature.OpeningIndent
- PSR2.Methods.FunctionCallSignature.SpaceAfterCloseBracket
- PSR2.Methods.FunctionClosingBrace
- PSR2.Methods.MethodDeclaration
- PSR2.Methods.MethodDeclaration.Underscore
- Squiz.ControlStructures.ControlSignature
- Squiz.ControlStructures.ForEachLoopDeclaration
- Squiz.ControlStructures.ForEachLoopDeclaration.AsNotLower
- Squiz.ControlStructures.ForEachLoopDeclaration.SpaceAfterOpen
- Squiz.ControlStructures.ForEachLoopDeclaration.SpaceBeforeClose
- Squiz.ControlStructures.ForLoopDeclaration
- Squiz.ControlStructures.ForLoopDeclaration.SpacingAfterOpen
- Squiz.ControlStructures.ForLoopDeclaration.SpacingBeforeClose
- Squiz.ControlStructures.LowercaseDeclaration
- Squiz.Functions.FunctionDeclaration
- Squiz.Functions.FunctionDeclarationArgumentSpacing
- Squiz.Functions.LowercaseFunctionKeywords
- Squiz.Functions.MultiLineFunctionDeclaration
- Squiz.Scope.MethodScope
- Squiz.WhiteSpace.CastSpacing
- Squiz.WhiteSpace.ControlStructureSpacing.SpacingAfterOpen
- Squiz.WhiteSpace.ControlStructureSpacing.SpacingBeforeClose
- Squiz.WhiteSpace.ScopeClosingBrace
- Squiz.WhiteSpace.ScopeKeywordSpacing
- Squiz.WhiteSpace.SuperfluousWhitespace
- Squiz.WhiteSpace.SuperfluousWhitespace.EmptyLines
- Squiz.WhiteSpace.SuperfluousWhitespace.EndFile
- Squiz.WhiteSpace.SuperfluousWhitespace.StartFile
- PSR1 的所有规则:
- 自定义规则
- Generic.Arrays.DisallowLongArraySyntax
- Generic.Formatting.SpaceAfterCast
- PSR1.Files.SideEffects.FoundWithSymbols
- SlevomatCodingStandard.Arrays.DisallowImplicitArrayCreation
- SlevomatCodingStandard.Arrays.SingleLineArrayWhitespace
- SlevomatCodingStandard.Arrays.TrailingArrayComma
- SlevomatCodingStandard.Attributes.AttributeAndTargetSpacing
- SlevomatCodingStandard.Attributes.AttributesOrder
- SlevomatCodingStandard.Attributes.DisallowMultipleAttributesPerLine
- SlevomatCodingStandard.Attributes.DisallowAttributesJoining
- SlevomatCodingStandard.Attributes.RequireAttributeAfterDocComment
- SlevomatCodingStandard.Classes.BackedEnumTypeSpacing
- SlevomatCodingStandard.Classes.ClassConstantVisibility
- 代码规范库\.类\.类长度
- 代码规范库\.类\.类成员间距
- 代码规范库\.类\.类结构
- 代码规范库\.类\.常量间距
- 代码规范库\.类\.不允许常量晚绑定
- 代码规范库\.类\.不允许多常量定义
- 代码规范库\.类\.不允许多属性定义
- 代码规范库\.类\.方法间距
- 代码规范库\.类\.现代类名引用
- 代码规范库\.类\.属性声明
- 代码规范库\.类\.属性间距
- 代码规范库\.类\.要求抽象或最终
- 代码规范库\.类\.特质使用间距
- 代码规范库\.类\.无用的晚静态绑定
- 代码规范库\.注释\.废弃的注释声明
- 代码规范库\.注释\.不允许代码后注释
- 代码规范库\.注释\.文档注释间距
- 代码规范库\.注释\.空注释
- 代码规范库\.注释\.内联文档注释声明
- 代码规范库\.注释\.无用的函数文档注释
- 代码规范库\.复杂性\.认知复杂度
- 代码规范库\.控制结构\.不允许在switch中继续使用非整数操作数
- 代码规范库\.控制结构\.带括号的语言构造
- 代码规范库\.控制结构\.要求使用null合并等于操作符
- 代码规范库\.控制结构\.要求使用null合并操作符
- 代码规范库\.控制结构\.无用的if条件带返回
- 代码规范库\.控制结构\.无用的三元操作符
- 代码规范库\.异常\.死捕获
- 代码规范库\.异常\.仅引用可抛出异常
- 代码规范库\.异常\.要求非捕获捕获
- 代码规范库\.文件\.文件长度
- 代码规范库\.函数\.箭头函数声明
- 代码规范库\.函数\.不允许空函数
- 代码规范库\.函数\.静态闭包
- 代码规范库\.函数\.未使用的继承变量传递到闭包
- 代码规范库\.函数\.无用的参数默认值
- 代码规范库\.命名空间\.按字母顺序排序的使用
- 代码规范库\.命名空间\.不允许分组使用
- 代码规范库\.命名空间\.每行多个使用
- 代码规范库\.命名空间\.命名空间声明
- 代码规范库\.命名空间\.命名空间间距
- 代码规范库\.命名空间\.仅引用使用过的命名空间
- 代码规范库\.命名空间\.要求文件中只有一个命名空间
- 代码规范库\.命名空间\.未使用的使用
- 代码规范库\.命名空间\.使用不以反斜杠开头
- 代码规范库\.命名空间\.无用的别名
- 代码规范库\.命名空间\.使用间距
- 代码规范库\.运算符\.否定运算符间距
- 代码规范库\.运算符\.要求组合赋值运算符
- 代码规范库\.运算符\.展开运算符间距
- 代码规范库\.PHP\.禁止的类
- 代码规范库\.PHP\.优化函数无解包
- 代码规范库\.PHP\.要求显式断言
- 代码规范库\.PHP\.短列表
- 代码规范库\.PHP\.无用的括号
- 代码规范库\.PHP\.无用的分号
- 代码规范库\.类型提示\.声明严格类型
- 代码规范库\.类型提示\.长类型提示
- 代码规范库\.类型提示\.null默认值的可空类型
- 代码规范库\.类型提示\.最后一个位置上的null类型提示
- 代码规范库\.类型提示\.参数类型提示
- 代码规范库\.类型提示\.参数类型提示间距
- 代码规范库\.类型提示\.属性类型提示
- 代码规范库\.类型提示\.返回类型提示
- 代码规范库\.类型提示\.返回类型提示间距
- 代码规范库\.类型提示\.联合类型提示格式
- 代码规范库\.类型提示\.无用的常量类型提示
- 代码规范库\.变量\.不允许超全局变量
- 代码规范库\.变量\.变量重复赋值
- 代码规范库\.变量\.未使用的变量
- 代码规范库\.变量\.无用的变量
- 代码规范库\.空白\.重复空格
- PHP标准库\.字符串\.连接符间距
- PHP标准库\.字符串\.双引号使用\.不必要
- PHP标准库\.空白\.运算符间距
版本控制
PSR-12 扩展编码规范使用 MAJOR.MINOR.PATCH
版本号格式。
MAJOR
版本在以下情况下递增:
- Slevomat 编码标准的主版本发生变化时,或
- PHP_CodeSniffer 的主版本发生变化时,或
- 对
ruleset.xml
格式进行向后不兼容的更改时,或 - 在此标准中启用新的 sniff(添加到列表中),或
- 从此标准中删除现有的 sniff(从列表中删除)。
MINOR
版本在以下情况下递增:
- Slevomat 编码标准的次版本发生变化时,或
- PHP_CodeSniffer 的次版本发生变化时,或
- 对
ruleset.xml
格式进行向后兼容的更改。
PATCH
版本在以下情况下递增:
- 进行向后兼容的错误修复。