承接 permafrost-dev/phpcsfixer-preset 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

permafrost-dev/phpcsfixer-preset

最新稳定版本:2.0.1

Composer 安装命令:

composer require permafrost-dev/phpcsfixer-preset

包简介

shared php-cs-fixer rules & finders preset

README 文档

README

Permafrost Dev

phpcsfixer-preset

version license downloads Run Tests Coverage Status


This package allows sharing identical php-cs-fixer formatting rules across all of your projects without copy-and-pasting configuration files. There is also a quick setup script to automatically generate a configuration file for the project structure and preferred formatting preset.

permafrost-dev/phpcsfixer-preset provides several opinionated php-cs-fixer configuration choices as well as pre-configured Finder classes for common project formats and use cases.

Supported PHP versions are 7.3, 7.4, 8.0, 8.1, and 8.2.

The original concept for this package came from this excellent article on sharing php-cs-fixer configurations across projects written by Tim Mcdonald.

Installation

composer require permafrost-dev/phpcsfixer-preset --dev

Example .php-cs-fixer.dist.php files

This example uses the Laravel project finder and the Default Ruleset:

<?php

require_once(__DIR__ . '/vendor/autoload.php');

use Permafrost\PhpCsFixerRules\Finders\LaravelProjectFinder;
use Permafrost\PhpCsFixerRules\Rulesets\DefaultRuleset;
use Permafrost\PhpCsFixerRules\SharedConfig;

$finder = LaravelProjectFinder::create(__DIR__);

return SharedConfig::create($finder, new DefaultRuleset());

Standard PhpCsFixer\Finder options can be chained onto the custom Finder class to customize it to your preferences:

    // ...
    $finder = LaravelProjectFinder::create(__DIR__)
        ->in([__DIR__ . '/custom-src-dir'])
        ->notName('*.ignored.php')
        ->notPath('another-custom-dir/cache/*');
    // ...

The standard PhpCsFixer\Finder class can be used along with any of the Rulesets:

<?php

require_once(__DIR__ . '/vendor/autoload.php');

use PhpCsFixer\Finder;
use Permafrost\PhpCsFixerRules\Rulesets\SpatieRuleset;
use Permafrost\PhpCsFixerRules\SharedConfig;

$finder = Finder::create()
    ->ignoreVCS(true)
    ->ignoreDotFiles(true)
    ->name('*.php')
    ->in([
        __DIR__ . '/src',
        __DIR__ . '/tests',
    ])
    ->exclude(__DIR__ . '/vendor');

return SharedConfig::create($finder, new SpatieRuleset());

Overriding Ruleset Rules

When creating a Ruleset class, you may pass an array of php-cs-fixer rules that add or override the Ruleset's default rules.

<?php

require_once(__DIR__.'/vendor/autoload.php');

use Permafrost\PhpCsFixerRules\Finders\LaravelProjectFinder;
use Permafrost\PhpCsFixerRules\Rulesets\DefaultRuleset;
use Permafrost\PhpCsFixerRules\SharedConfig;

$finder = LaravelProjectFinder::create(__DIR__);

return SharedConfig::create($finder, new DefaultRuleset([
    // existing rules can be overridden:
    'no_break_comment' => true,
    'no_closing_tag' => false,
    // new rules can be added:
    'a_new_option' => [
        'some_sub_option' => 12,
    ],
]));

Quick Setup

To generate a php-cs-fixer configuration file for your project, run:

vendor/bin/pf-create-cs-config <type> [-o|--outfile=filename] [-r|--ruleset=name] [-f|--force]

Parameter: <type>

Required: yes

Default: no default

Possible values:

  • custom
  • project
  • package
  • laravel (alias for laravel:project)
  • laravel:project
  • laravel:package

Flag: --outfile (or -o)

Required: no

Default: .php-cs-fixer.dist.php

Possible values: any valid filename

Flag: --ruleset (or -r)

Required: no

Default: default

Possible values:

  • default
  • laravel_shift
  • php_unit
  • spatie

Flag: --force (or -f)

Required: no

Default: false

Possible values: none

Effect: overwrites any existing configuration file

Examples:

vendor/bin/pf-create-cs-config laravel:package

vendor/bin/pf-create-cs-config package -f

vendor/bin/pf-create-cs-config laravel -o .php-cs-fixer.php -r spatie

vendor/bin/pf-create-cs-config project --ruleset=laravel_shift

vendor/bin/pf-create-cs-config custom --outfile=.my-config

Note on the custom type:

The custom type will prompt you to enter the directory names you would like php-cs-fixer to include and exclude. The generated configuration file implements the PhpCsFixer\Finder class instead of one of the preconfigured finder classes.

Automatic Formatting

To apply php-cs-fixer formatting using Github Actions automatically, see the automation with Github Actions documentation.

Finder Presets

BasicProjectFinder

  • ignores VCS files
  • ignores dotfiles
  • includes PHP files
  • excludes vendor/ directory

LaravelProjectFinder

  • inherits BasicProjectFinder presets
  • excludes *.blade.php files
  • excludes all files in bootstrap/, public/, resources/, storage/
  • includes PHP files in app/, config/, database/, routes/, tests/

LaravelPackageFinder

  • inherits BasicProjectFinder presets
  • excludes *.blade.php files
  • excludes all files in resources/
  • includes PHP files in src/, tests/, config/

ComposerPackageFinder

Rulesets

Default

  • Default opinionated Ruleset provided by this package.
  • View Rules

LaravelShift

PhpUnit

Spatie


Usage

Select a Finder preset or create an instance of \PhpCsFixer\Finder and return SharedConfig::create($finder) from the .php-cs-fixer.dist.php file.

Updating Default Rules

Update the rules() method in the Permafrost\PhpCsFixerRules\Rulesets\DefaultRuleset class.

Creating Rulesets

Create a class that implements the Permafrost\PhpCsFixerRules\Rulesets\Ruleset interface, returning your rules from the rules() method.

Sample Ruleset:

<?php

namespace Permafrost\PhpCsFixerRules\Rulesets;

class MyCustomRulesRuleset implements RuleSet
{
    public function allowRisky(): bool
    {
        return true; //this tells php-cs-fixer whether or not to permit "risky" rules.
    }

    public static function name(): string
    {
        return 'my_custom_rules'; //the name should omit 'ruleset' from the end.
    }

    /**
     * @return array
     */
    public function rules(): array
    {
        return array_merge([
            '@PSR2' => true,
            // additional php-cs-fixer rules
        ], $this->additional); //it's important that the additional rules property is merged
    }
}

If adding a new Ruleset to this package, the Ruleset must be registered in \Permafrost\PhpCsFixerRules\Commands\GenerateConfigCommand@rulesets() to allow the quick setup command to use it.

When creating a new Ruleset package, follow the above example but use a namespace unique to the package.

Code Formatting

To format all files specified in the configuration, run:

vendor/bin/php-cs-fixer fix

To list the files to be processed without making any changes:

vendor/bin/php-cs-fixer fix --dry-run

Testing

This package uses PHPUnit for unit tests. To run the test suite, run:

./vendor/bin/phpunit

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributions

Contributions of Rulesets, Finders, bug fixes, suggestions, or improvements are welcome. Please open an appropriately labeled issue or pull request for any of these.

License

The MIT License (MIT). Please see License File for more information.

permafrost-dev/phpcsfixer-preset 适用场景与选型建议

permafrost-dev/phpcsfixer-preset 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6.55k 次下载、GitHub Stars 达 22, 最近一次更新时间为 2020 年 11 月 09 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 permafrost-dev/phpcsfixer-preset 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 permafrost-dev/phpcsfixer-preset 我们能提供哪些服务?
定制开发 / 二次开发

基于 permafrost-dev/phpcsfixer-preset 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 6.55k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 22
  • 点击次数: 23
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 22
  • Watchers: 3
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-11-09