footeam/php-style

FooTeam为PHP-CS-Fixer提供的共享PHP风格规则

dev-main 2021-06-01 22:24 UTC

This package is not auto-updated.

Last update: 2024-09-26 23:42:27 UTC


README

规则预设

以及自定义规则(请参阅 /src/rules)。

安装

将此仓库添加到您的composer.json文件中

"repositories": [
  {
    "type": "vcs",
    "url": "https://gitlab.com/FooTeam/php-style"
  }
]

然后执行以下命令

composer require footeam/php-style:dev-main --dev

echo "<?php

require __DIR__.'/vendor/autoload.php';

return FooTeam\styles(PhpCsFixer\Finder::create()
    ->in([__DIR__.'/app', __DIR__.'/config', __DIR__.'/database', __DIR__.'/routes', __DIR__.'/tests']));
" >> .php-cs-fixer.dist.php

别忘了将以下行添加到.gitignore

.php-cs-fixer.cache

用法

在终端中

./vendor/bin/php-cs-fixer fix

在git pre-commit钩子(.git/hooks/pre-commit)中

#!/usr/bin/env bash

source_root=$(git rev-parse --show-toplevel)
staged_files=$(git diff --cached --name-only)
exit_code=0

for staged in ${staged_files}; do
    if [[ -f ${staged} && ${staged} == *.php ]]; then
		${source_root}/vendor/bin/php-cs-fixer fix ${staged} --dry-run -vv --using-cache=no

		if [[ $? -ne 0 ]]; then
		    exit_code=1
		fi
    fi
done

if [[ ${exit_code} -eq 1 ]]; then
    echo "lint failed"
fi

exit ${exit_code}

风险规则

这将应用以下风险规则

  • no_php4_constructor
  • no_alias_functions
  • implode_call
  • combine_nested_dirname
  • pow_to_exponentiation
  • void_return
  • is_null
  • no_unreachable_default_argument_value
  • self_accessor
  • no_unneeded_final_method
  • use_arrow_functions
  • non_printable_character
  • random_api_migration
  • psr4
  • psr_autoloading

在此检查它们。