wieni/wmcodestyle

一套Wieni最佳实践、编码规范及其执行工具。

安装次数: 5,609

依赖者: 41

建议者: 0

安全性: 0

星标: 3

关注者: 7

分支: 2

开放性问题: 2

类型:应用程序

1.10.1 2023-07-31 13:57 UTC

This package is auto-updated.

Last update: 2024-08-30 01:09:14 UTC


README

Latest Stable Version Total Downloads License

一套Wieni最佳实践、编码规范及其执行工具。

为什么?

  • 一个集中位置,用于跟踪我们编写的编码规范和最佳实践。
  • 使配置工具强制执行我们的编码规范在我们的所有存储库中可用,无需手动复制和同步。

目录

  1. 编码规范
  2. 工具
  3. 变更日志
  4. 安全性
  5. 许可证
  6. 致谢

编码规范

工具

安装

本包需要PHP 7.1或更高版本,并可以使用Composer进行安装。

 composer require --dev wieni/wmcodestyle

与您的项目同步配置文件

本包提供了一个命令,用于将此存储库中的任何文件同步到您的项目中。建议将其添加到composer.json的scripts部分,以便在适当的时间自动执行。

 {
   "name": "foo/bar",
   "require": {
     "php": "^8.0",
   },
   "require-dev": {
     "wieni/wmcodestyle": "^1.0"
+  },
+  "scripts": {
+    "post-update-cmd": [
+      "wmcodestyle sync .editorconfig --quiet"
+    ]
   }
 }

PHP-CS-Fixer

本包为friendsofphp/php-cs-fixer提供了配置工厂和多个规则集。

配置

选择一个规则集

在项目的根目录中创建一个配置文件 .php_cs.php

<?php

use Wieni\wmcodestyle\PhpCsFixer\Config\Factory;
use Wieni\wmcodestyle\PhpCsFixer\Config\RuleSet\Php73;

$config = Factory::fromRuleSet(new Php73);

$config->getFinder()
    ->ignoreVCSIgnored(true)
    ->in(__DIR__)
    ->name('/\.(php|module|inc|install|test|profile|theme)$/')
    ->notPath('/(public|web)\/(index|update)\.php/');

$config->setCacheFile(__DIR__ . '/.cache/php_cs.cache');

return $config;

默认情况下,不使用风险规则。要使用它们,请将--allow-risky=yes传递给php-cs-fixer或将环境变量WMCODESTYLE_RISKY=1设置为。

Git

所有配置示例都使用了缓存功能,如果您也想使用它,请将缓存文件夹添加到.gitignore

# Ignore files generated by wieni/wmcodestyle
.cache

带覆盖规则的配置

💡 可选地通过传递要合并的规则数组来覆盖规则集中的规则

<?php

use Wieni\wmcodestyle\PhpCsFixer\Config\Factory;
use Wieni\wmcodestyle\PhpCsFixer\Config\RuleSet\Php73;

-$config = Factory::fromRuleSet(new Php73);
+$config = Factory::fromRuleSet(new Php73, [
+    'mb_str_functions' => false,
+    'strict_comparison' => false,
+]);

$config->getFinder()
    ->ignoreVCSIgnored(true)
    ->in(__DIR__)
    ->name('/\.(php|module|inc|install|test|profile|theme)$/')
    ->notPath('/(public|web)\/(index|update)\.php/');

$config->setCacheFile(__DIR__ . '/.php_cs.cache');

return $config;

Rector

即时升级和重构PHP代码

本包为rector/rector提供了多个规则集。这些规则集在很大程度上与默认规则集相同,只是有一些我们不喜欢的/不需要的个别规则。为了更容易地将此包包含在您的Composer项目中,我们将rector/rector添加为依赖项。

规则集

选择一个规则集

示例配置

<?php

declare(strict_types=1);

use Rector\Core\Configuration\Option;
use Rector\Set\ValueObject\SetList;
use Rector\Symfony\Set\TwigSetList;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Wieni\wmcodestyle\Rector\SetList as WieniSetList;

return static function (ContainerConfigurator $containerConfigurator): void {
    $parameters = $containerConfigurator->parameters();

    $parameters->set(Option::AUTO_IMPORT_NAMES, true);
    $parameters->set(Option::IMPORT_SHORT_CLASSES, false);

    $parameters->set(Option::PATHS, [
        __DIR__ . '/public/modules/custom',
    ]);

    $parameters->set(Option::AUTOLOAD_PATHS, [
        __DIR__ . '/public/core',
        __DIR__ . '/public/core/modules',
        __DIR__ . '/public/modules',
        __DIR__ . '/public/profiles',
        __DIR__ . '/public/sites',
        __DIR__ . '/public/themes',
    ]);

    $parameters->set(Option::SETS, [
        SetList::DEAD_CODE,
        SetList::PHP_73,
        SetList::PHP_74,
        TwigSetList::TWIG_UNDERSCORE_TO_NAMESPACE,
    ]);

    $containerConfigurator->import(WieniSetList::CODE_QUALITY);
    $containerConfigurator->import(WieniSetList::CODING_STYLE);
    $containerConfigurator->import(WieniSetList::DEPENDENCY_INJECTION);
    $containerConfigurator->import(WieniSetList::EARLY_RETURN);
    $containerConfigurator->import(WieniSetList::TYPE_DECLARATION);
};

添加Symfony容器XML

要使用一些Symfony规则,您现在需要链接您的容器XML文件

<?php

declare(strict_types=1);

use Rector\Core\Configuration\Option;
use Rector\Set\ValueObject\SetList;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Wieni\wmcodestyle\Rector\SetList as WieniSetList;

return static function (ContainerConfigurator $containerConfigurator): void {
    $parameters = $containerConfigurator->parameters();
    $parameters->set(Option::SYMFONY_CONTAINER_XML_PATH_PARAMETER, __DIR__ . '/.cache/drupal_container.xml');
};
// phpstan-for-rector.neon
parameters:
    symfony:
        container_xml_path: %currentWorkingDirectory%/.cache/drupal_container.xml

includes:
	- vendor/wieni/wmcodestyle/phpstan/for-rector.neon

在Drupal中使用时,无法直接导出容器,但我们的Container Dumper模块使这成为可能。

PHPStan

PHPStan专注于在运行代码之前发现代码中的错误。它甚至在您为代码编写测试之前就捕捉到整个类别的bug。在这一点上,PHP更接近于编译语言,因为可以在实际运行每一行代码之前检查每一行代码的正确性。

Drupal

为了更好地与Drupal集成,请考虑将mglaman/phpstan-drupalphpstan/phpstan-symfony添加到您的项目中。当使用drupal-moduledrupal-site配置时,这些扩展是必需的。[链接](https://github.com/mglaman/phpstan-drupal "mglaman/phpstan-drupal")

您可以使用Container Dumper模块将Symfony容器导出到一个XML文件。然后,Symfony扩展可以使用此文件更好地了解您的项目。将以下内容添加到您的PHPStan配置中

parameters:
    symfony:
        container_xml_path: %currentWorkingDirectory%/.cache/drupal_container.xml

Symfony

为了更好地与Symfony集成,请考虑将phpstan/phpstan-symfonyphpstan/phpstan-doctrine添加到您的项目中。[链接](https://github.com/phpstan/phpstan-symfony "phpstan/phpstan-symfony")

composer-normalize

提供用于规范化composer.json的Composer插件。

我们强烈推荐此Composer插件以确保您的composer.json格式一致。

运行代码风格修复器

Makefile

如果您喜欢Makefile,请创建一个带有coding-standards目标的Makefile

+.PHONY: coding-standards
+coding-standards: vendor
+    mkdir -p .build/php-cs-fixer
+    vendor/bin/php-cs-fixer fix --config=.php_cs --diff --verbose
+    composer normalize"
+    vendor/bin/rector process"
+    vendor/bin/php-cs-fixer fix --config=.php_cs.php
+    vendor/bin/phpstan analyse

 vendor: composer.json composer.lock
     composer validate
     composer install

运行

$ make coding-standards

以自动修复编码规范违规。

Composer脚本

如果您喜欢composer脚本,请将coding-standards脚本添加到composer.json

 {
   "name": "foo/bar",
   "require": {
     "php": "^8.0",
   },
   "require-dev": {
     "wieni/wmcodestyle": "^1.0"
+  },
+  "scripts": {
+    "coding-standards": [
+      "@composer normalize",
+      "rector process",
+      "php-cs-fixer fix --config=.php_cs.php",
+      "phpstan analyse"
+    ]
   }
 }

运行

$ composer coding-standards

变更日志

此项目的所有重大更改都将记录在CHANGELOG文件中。

安全性

如果您发现任何安全相关的问题,请通过security@wieni.be发送电子邮件,而不是使用问题跟踪器。

许可证

在MIT许可下分发。有关更多信息,请参阅LICENSE文件。

致谢