matthewnance/laravel-php-cs-fixer

Laravel的PHP编码规范强制包装器。

v1.0.0 2018-11-16 12:12 UTC

This package is auto-updated.

Last update: 2024-09-29 05:34:22 UTC


README

PHP CS Fixer维护在GitHub上,地址为https://github.com/FriendsOfPHP/PHP-CS-Fixer,欢迎在那里提交错误报告和关于新功能的想法。

PHP编码标准修复器(PHP CS Fixer)工具可修复您的代码以遵循标准;无论是想遵循PSR-1、PSR-2等PHP编码标准,还是遵循Symfony等社区驱动的其他标准。您还可以通过配置定义您的(团队)风格。

此包通过提供您已经熟悉的工具来访问它,使使用PHP CS Fixer维护Laravel代码变得比以往任何时候都简单。一个用于修复代码的Artisan CLI命令,以及以您使用其他所有Laravel包相同的方式管理配置。

本包功能

  • 通过Laravel Artisan CLI运行PHP-CS-Fixer命令。
  • 默认使用Laravel代码风格配置。
  • 无需学习新工具。

版本和兼容性

注意:本文档是为Laravel 5.5编写的。

  • PHP版本:">=7.0 <7.3"
  • Laravel/Lumen: "5.4.x|5.5.x"
  • PHP-CS-Fixer: "2.13.*"

安装

composer require matthewnance/laravel-php-cs-fixer

注册提供者

对于Lumen服务,添加

$app->register(MatthewNance\Fixer\FixerServiceProvider::class);

bootstrap/app.php。对于Laravel应用程序,添加

MatthewNance\Fixer\FixerServiceProvider::class,

config/app.php中的providers数组。

配置

默认规则配置在fixer.php中,旨在匹配Laravel框架使用的规则。

return [
    'rules' => [
        'psr0' => false,
        '@PSR2' => true,
        'blank_line_after_namespace' => true,
        'braces' => true,
        'class_definition' => true,
        'elseif' => true,
        'function_declaration' => true,
        'indentation_type' => true,
        'line_ending' => true,
        'lowercase_constants' => true,
        'lowercase_keywords' => true,
        'method_argument_space' => [
            'ensure_fully_multiline' => true, ],
        'no_break_comment' => true,
        'no_closing_tag' => true,
        'no_spaces_after_function_name' => true,
        'no_spaces_inside_parenthesis' => true,
        'no_trailing_whitespace' => true,
        'no_trailing_whitespace_in_comment' => true,
        'single_blank_line_at_eof' => true,
        'single_class_element_per_statement' => [
            'elements' => ['property'],
        ],
        'single_import_per_statement' => true,
        'single_line_after_imports' => true,
        'switch_case_semicolon_to_colon' => true,
        'switch_case_space' => true,
        'visibility_required' => true,
        'encoding' => true,
        'full_opening_tag' => true,
        ],
];

如果您想自己修改,只需使用Artisan命令artisan vendor:publish --provider="MatthewNance\Fixer\FixerServiceProvider",它将默认配置放入'config/fixer.php'。有关有效规则,请参阅
PHP-CS-Fixer/README

注意:Finder中还有一些静态配置设置尚未移动到配置文件,您应该注意!我们计划很快将这些移动到配置文件。

$finder = Finder::create()
            ->notPath('bootstrap/cache')
            ->notPath('storage')
            ->notPath('vendor')
            ->in(base_path())
            ->name('*.php')
            ->notName('*.blade.php')
            ->ignoreDotFiles(true)
            ->ignoreVCS(true);

使用方法

修复您的代码

使用Laravel编码标准修复您的代码。

语法

$ php artisan fixer:fix [options]

示例

Usage:
  fixer:fix [options]

Options:
      --path[=PATH]                    The path. (multiple values allowed)
      --path-mode[=PATH-MODE]          Specify path mode (can be override or intersection). [default: "override"]
      --allow-risky[=ALLOW-RISKY]      Are risky fixers allowed (can be yes or no).
      --config[=CONFIG]                The path to a .php_cs file.
      --dry-run                        Only shows which files would have been modified.
      --rules[=RULES]                  The Rules
      --using-cache[=USING-CACHE]      Does cache should be used (can be yes or no). [default: "yes"]
      --cache-file[=CACHE-FILE]        The path to the cache file.
      --diff                           Also produce diff for each file.
      --diff-format[=DIFF-FORMAT]      Specify diff format.
      --format[=FORMAT]                To output results in other formats.
      --stop-on-violation              Stop execution on first violation.
      --show-progress[=SHOW-PROGRESS]  Type of progress indicator (none, run-in, estimating or estimating-max).
  -h, --help                           Display this help message
  -q, --quiet                          Do not output any message
  -V, --version                        Display this application version
      --ansi                           Force ANSI output
      --no-ansi                        Disable ANSI output
  -n, --no-interaction                 Do not ask any interactive question
      --env[=ENV]                      The environment the command should run under
  -v|vv|vvv, --verbose                 Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
  
// Only shows which files would have been modified
$ php artisan fixer:fix --dry-run

// Modify the files that need to be fixed
$ php artisan fixer:fix

// Check all the files in the `app` directory
$ php artisan fixer:fix --path app --dry-run