zebra-north/phpcs-short-types

在注释中要求使用短标量类型名称(bool, int),而不是长名称(boolean, integer)。

v1.0.1 2021-12-20 20:01 UTC

This package is auto-updated.

Last update: 2024-09-21 01:59:59 UTC


README

PHP Codesniffer 要求使用长形式书写标量类型,例如 booleaninteger

从 PHP 7+ 开始,标量必须使用短形式书写,例如 boolint

此补丁允许你在文档块注释中使用短形式。

之前

/**
 * Test function.
 *
 * @param integer $test A numeric argument.
 *
 * @return boolean Returns true or false.
 */
function test(int $test): bool
{
    return false;
}

之后

/**
 * Test function.
 *
 * @param int $test A numeric argument.
 *
 * @return bool Returns true or false.
 */
function test(int $test): bool
{
    return false;
}

错误修复

  • Squiz.Commenting.FunctionComment.IncorrectParamVarName
    • 期望 "boolean" 但找到 "bool" 作为参数类型
    • 期望 "integer" 但找到 "int" 作为参数类型
  • Squiz.Commenting.VariableComment.IncorrectVarType
    • 期望 "boolean" 但找到 "bool" 作为成员变量注释中的 @var 标签
    • 期望 "integer" 但找到 "int" 作为成员变量注释中的 @var 标签
  • Squiz.Commenting.FunctionComment.InvalidReturn
    • 期望 "boolean" 但找到 "bool" 作为函数返回类型
    • 期望 "integer" 但找到 "int" 作为函数返回类型

安装

  1. 使用以下命令安装到 composer 中: composer require --dev zebra-north/phpcs-short-types
  2. 编辑你的项目中的 phpcs.xml 以添加引导文件
<?xml version="1.0"?>
<ruleset>

    <arg name="bootstrap" value="vendor/zebra-north/phpcs-short-types/short-types.php"/>

    ...

</ruleset>

已知问题

不会 建议使用 bool/int 如果你已经使用了 boolean/integer - 这需要修复 PHP Codesniffer。 已创建补丁,但尚未合并

squizlabs/PHP_CodeSniffer#3139