mollie/php-coding-standards

包含PHP编码标准,例如PHP-CS-Fixer规则,用于标准化。

v2.3.1 2024-07-18 15:20 UTC

README

包含PHP编码标准,例如PHP-CS-Fixer规则,用于标准化。

安装

composer require --dev  mollie/php-coding-standards

使用方法

此包利用了PHP-CS-Fixer。

已经熟悉PHP-CS-Fixer

此包为PHP-CS-Fixer提供默认规则。

您可以在 Mollie\PhpCodingStandards\PhpCsFixer\Rules 中找到它们,该规则具有针对特定PHP版本的特定方法,您可以直接在配置中的 ->setRules() 部分使用它们。例如,假设PHP版本为8.3

use Mollie\PhpCodingStandards\PhpCsFixer\Rules;

$config->setRules(Rules::getForPhp83());

新手使用PHP-CS-Fixer

在项目根目录中放置一个名为 .php-cs-fixer.dist.php 的文件,其中包含以下内容。

<?php 

use Mollie\PhpCodingStandards\PhpCsFixer\Rules;
use PhpCsFixer\Config;
use PhpCsFixer\Finder;

$finder = Finder::create()
    ->in(__DIR__);

return (new Config())
    ->setFinder($finder)
    ->setRiskyAllowed(true)
    // use specific rules for your php version e.g.: getForPhp74, getForPhp82, getForPhp83
    ->setRules(Rules::getForPhp74());

手动触发

在项目目录中运行以下命令,这将为每个 .php 文件运行修复器。

vendor/bin/php-cs-fixer fix

通过PhpStorm文件监视器使用

请参阅官方PhpStorm文档

通过pre-commit git钩子使用

将以下bash脚本的內容放入 .git/hooks 目录中名为pre-commit的文件,并使其可执行。有关git钩子的更多详细信息,请参阅官方git手册

#!/usr/bin/env bash

EXCLUDE_DIRECTORIES_REGEX='^(directory_one/(sub_directory_one|sub_directory_two)|directory_two)/.*'
git diff --diff-filter=ACMRTUXB --name-only --staged | grep -E '\.php(_cs\.dist)?$' | grep -vE $EXCLUDE_DIRECTORIES_REGEX |  while read FILE; do
    STATUS="$(git status --porcelain ${FILE} | cut -c 1-2)"
    vendor/bin/php-cs-fixer fix --using-cache=no --quiet --dry-run ${FILE}

    # Not 0 means php-cs-fixer either errored or wanted to make changes
    if [ $? != 0 ]
    then
        # MM = staged & non-staged modification in same file
        if [ ${STATUS} = "MM" ]
        then
            echo -e "\033[31m┌────────────────────────────────────────────────────────────────────────────────┐"
            echo -e "│ Failure:\033[39m Codestyle violation(s) in file with both staged and unstaged changes. \033[31m│"
            echo -e "├────────────────────────────────────────────────────────────────────────────────┘"
            echo -e "└\033[33m File:\033[39m    $FILE"
            exit 1
        fi

        vendor/bin/php-cs-fixer fix --using-cache=no --quiet ${FILE}
        git add ${FILE}
    fi
done

在Mollie工作

Mollie始终在寻找新的才华加入我们的团队。我们寻找好奇心强、有良好想法和坚定意见的人,最重要的是,知道如何推出优秀的产品。想要加入支付的未来?查看我们的职位空缺

许可证

BSD (伯克利软件发行版) 许可证。版权(c)2023,Mollie B.V.