narrowspark / coding-standard
Narrowspark 编码标准是一套应用于所有 Narrowspark 项目的 phpstan、psalm、infection、rector 和 php-cs-fixer 规则。
Requires
- php: ^8.0
- infection/infection: ^0.21.3
- narrowspark/php-cs-fixer-config: ~6.3.2
- phpstan/phpstan: ^0.12.80
- phpstan/phpstan-deprecation-rules: ^0.12.6
- phpstan/phpstan-mockery: ^0.12.12
- phpstan/phpstan-phpunit: ^0.12.17
- phpstan/phpstan-strict-rules: ^0.12.9
- psalm/plugin-mockery: ~0.7.0
- psalm/plugin-phpunit: ^0.15.1
- rector/rector: ^0.10.0
- rector/rector-phpunit: ^0.10.0
- slam/phpstan-extensions: ^5.1.0
- symplify/phpstan-rules: ^9.2
- thecodingmachine/phpstan-strict-rules: ^0.12.1
- vimeo/psalm: ^4.6.2
Requires (Dev)
- phpunit/phpunit: ^9.5.2
- symfony/var-exporter: ^5.2.3
Suggests
- phpstan/phpstan-doctrine: Doctrine extensions for PHPStan (*)
- phpstan/phpstan-php-parser: PHP-Parser extensions for PHPStan (*)
- dev-main / 5.3.x-dev
- v5.3.2
- v5.3.1
- v5.3.0
- v5.2.3
- v5.2.2
- v5.2.1
- v5.2.0
- 5.1.0
- 5.0.0
- 4.1.0
- 4.0.0
- 3.4.0
- 3.3.0
- 3.2.0
- v3.1.0
- 3.0.0
- 2.1.1
- 2.1.0
- 2.0.0
- 1.4.0
- 1.3.0
- 1.2.1
- v1.2.0
- v1.1.2
- v1.1.1
- v1.1.0
- v1.0.0
- dev-renovate/stefanzweifel-git-auto-commit-action-4.x
- dev-renovate/node-14.x
- dev-renovate/major-6-devdependencies-(major)
- dev-renovate/actions-stale-5.x
- dev-renovate/actions-stale-4.x
- dev-renovate/actions-setup-node-3.x
- dev-renovate/actions-cache-3.x
- dev-renovate/actions-checkout-3.x
- dev-renovate/dependencies-(non-major)
- dev-renovate/anolilab-textlint-config-2.x
This package is auto-updated.
Last update: 2023-03-20 08:58:51 UTC
README
Narrowspark 所有项目的编码标准。
安装
composer require narrowspark/coding-standard
使用
Settings.yml
在 .github
中创建一个名为 settings.yml
的配置文件。
# https://github.com/probot/settings branches: - name: main protection: enforce_admins: false required_pull_request_reviews: dismiss_stale_reviews: true require_code_owner_reviews: true required_approving_review_count: 1 restrictions: # https://developer.github.com/v3/repos/branches/#parameters-1 # Note: User, app, and team restrictions are only available for organization-owned repositories. # Set to null to disable when using this configuration for a repository on a personal account. apps: [] teams: [] labels: - name: Added description: "Changelog Added" color: 90db3f - name: Changed description: "Changelog Changed" color: fbca04 - name: dependency description: "Pull requests that update a dependency file" color: e1f788 - name: Deprecated description: "Changelog Deprecated" color: 1d76db - name: Duplicate color: 000000 - name: Enhancement color: d7e102 - name: Fixed description: "Changelog Fixed" color: 9ef42e - name: Removed description: "Changelog Removed" color: e99695 - name: Security description: "Changelog Security" color: ed3e3b - name: "Status: Good first issue" color: d7e102 - name: "Status: Help wanted" color: 85d84e - name: "Status: Needs Work" color: fad8c7 - name: "Status: Waiting for feedback" color: fef2c0 - name: "Type: BC Break" color: b60205 - name: "Type: Bug" color: b60205 - name: "Type: Critical" color: ff8c00 - name: "Type: RFC" color: fbca04 - name: "Type: Unconfirmed" color: 444444 - name: "Type: Wontfix" color: 000000 repository: allow_merge_commit: true allow_rebase_merge: false allow_squash_merge: false archived: false default_branch: main delete_branch_on_merge: true # Needs to be added description: "" has_downloads: true has_issues: true has_pages: false has_projects: false has_wiki: false # Needs to be added name: "" private: false # https://developer.github.com/v3/repos/branches/#remove-branch-protection # Needs to be added topics: ""
PHPstan
创建一个名为 phpstan.neon
和 phpstan-baseline.neon
的配置文件。
includes: - vendor/narrowspark/coding-standard/base_rules.neon - vendor/phpstan/phpstan/conf/bleedingEdge.neon - %currentWorkingDirectory%/phpstan-baseline.neon parameters: level: max inferPrivatePropertyTypeFromConstructor: true paths: - %currentWorkingDirectory%/src - %currentWorkingDirectory%/tests tmpDir: %currentWorkingDirectory%/.build/phpstan excludes_analyse: - vendor
点击链接查看如何配置规则
Narrowspark PHPstan 规则提供了对 symplify/phpstan-rules
的配置。
默认配置
parameters: symplify: no_chain_method_call: allowed_chain_types: - Mockery - Mockery\MockInterface - Mockery\ExpectationInterface - Mockery\Expectation - Mockery\HigherOrderMessage prevent_duplicate_class_method: minimum_line_count: 4 no_short_name: min_name_length: 2 forbidden_complex_func_call: forbidden_complex_functions: - 'array_filter' maximum_stmt_count: 2 forbidden_node: forbidden_nodes: - PhpParser\Node\Expr\Empty_ - PhpParser\Node\Stmt\Switch_ - PhpParser\Node\Expr\ErrorSuppress forbidden_func_call: forbidden_functions: - 'd' - 'dd' - 'dump' - 'var_dump' - 'extract' - 'curl_*' - 'compact' - 'method_exists' - 'property_exists' - 'spl_autoload_register' - 'spl_autoload_unregister' - array_walk see_annotation_to_test: required_see_types: - PHPStan\Rules\Rule class_name_respects_parent_suffix: parent_classes: - Symfony\Component\Console\Command\Command too_long_variable: max_variable_length: 10
PHP-CS-Fixer
在项目根目录创建一个包含以下内容的 .php_cs
配置文件
<?php declare(strict_types=1); use Ergebnis\License; use Narrowspark\CS\Config\Config; $license = License\Type\MIT::markdown( __DIR__ . '/LICENSE.md', License\Range::since( License\Year::fromString('2021'), new \DateTimeZone('UTC') ), License\Holder::fromString('Daniel Bannert'), License\Url::fromString('https://github.com/{name}/{repo}') ); $license->save(); $config = new Config($license->header()); $config->getFinder() ->files() ->in(__DIR__) ->exclude([ '.build', '.docker', '.github', 'vendor' ]) // php_unit_namespaced rule thinks than the const are some namespaces ->notPath('rector.php') ->name('*.php') ->ignoreDotFiles(true) ->ignoreVCS(true); $config->setCacheFile(__DIR__ . '/.build/php-cs-fixer/.php_cs.cache'); return $config;
信息
使用的 php-cs-fixer 规则。
更多信息,请参考 php-cs-fixer-config。
Psalm
使用以下命令添加您的配置。
./vendor/bin/psalm --init
或使用我们的配置
<?xml version="1.0"?> <psalm xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="https://getpsalm.org/schema/config" xsi:schemaLocation="https://getpsalm.org/schema/config/vendor/vimeo/psalm/config.xsd" cacheDirectory="./.build/psalm" errorBaseline="psalm-baseline.xml" resolveFromConfigFile="true" allowStringToStandInForClass='true' findUnusedCode='true' findUnusedVariablesAndParams='true' strictBinaryOperands='true' totallyTyped='true' usePhpDocMethodsWithoutMagicCall='true' phpVersion='8.0' errorLevel='8' > <issueHandlers> <LessSpecificReturnType errorLevel="info" /> </issueHandlers> <plugins> <pluginClass class="Psalm\PhpUnitPlugin\Plugin" /> </plugins> <projectFiles> <directory name="src" /> <directory name="tests" /> <ignoreFiles> <directory name="vendor" /> <directory name=".build" /> <directory name=".docker" /> <directory name=".github" /> </ignoreFiles> </projectFiles> <issueHandlers> <PossiblyUndefinedIntArrayOffset errorLevel="error" /> <PossiblyUndefinedStringArrayOffset errorLevel="error" /> </issueHandlers> </psalm>
现在您需要将 phpunit
和 mockery
插件添加到创建的 psalm.xml
文件中
... +<plugins> + <pluginClass class="Psalm\MockeryPlugin\Plugin" /> + <pluginClass class="Psalm\PhpUnitPlugin\Plugin" /> +</plugins> ...
Infection
您第一次在项目中运行 Infection 时,它将询问您创建 infection.json.dist
配置文件的问题
Rector
创建 rector.php
文件并将其配置添加到其中。
<?php declare(strict_types=1); /** * Copyright (c) 2021 Daniel Bannert * * For the full copyright and license information, please view * the LICENSE.md file that was distributed with this source code. * * @see https://github.com/narrowspark/php-cs-fixer-config */ use Rector\Core\Configuration\Option; use Rector\Core\ValueObject\PhpVersion; use Rector\PHPUnit\Set\PHPUnitSetList; use Rector\Set\ValueObject\SetList; use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; return static function (ContainerConfigurator $containerConfigurator): void { $parameters = $containerConfigurator->parameters(); // paths to refactor; solid alternative to CLI arguments $parameters->set(Option::PATHS, [__DIR__ . '/src', __DIR__ . '/tests']); // is your PHP version different from the one your refactor to? [default: your PHP version], uses PHP_VERSION_ID format $parameters->set(Option::PHP_VERSION_FEATURES, PhpVersion::PHP_80); // auto import fully qualified class names? [default: false] $parameters->set(Option::AUTO_IMPORT_NAMES, true); // skip root namespace classes, like \DateTime or \Exception [default: true] $parameters->set(Option::IMPORT_SHORT_CLASSES, false); // skip classes used in PHP DocBlocks, like in /** @var \Some\Class */ [default: true] $parameters->set(Option::IMPORT_DOC_BLOCKS, false); // Run Rector only on changed files $parameters->set(Option::ENABLE_CACHE, true); $phpstanPath = getcwd() . '/phpstan.neon'; $phpstanNeonContent = FileSystem::read($phpstanPath); $bleedingEdgePattern = '#\n\s+-(.*?)bleedingEdge\.neon[\'|"]?#'; // bleeding edge clean out, see https://github.com/rectorphp/rector/issues/2431 if (Strings::match($phpstanNeonContent, $bleedingEdgePattern)) { $temporaryPhpstanNeon = getcwd() . '/rector-temp-phpstan.neon'; $clearedPhpstanNeonContent = Strings::replace($phpstanNeonContent, $bleedingEdgePattern); FileSystem::write($temporaryPhpstanNeon, $clearedPhpstanNeonContent); $phpstanPath = $temporaryPhpstanNeon; } // Path to phpstan with extensions, that PHPStan in Rector uses to determine types $parameters->set(Option::PHPSTAN_FOR_RECTOR_PATH, $phpstanPath); $parameters->set(Option::SETS, [ SetList::CODING_STYLE, SetList::CODE_QUALITY, SetList::CODE_QUALITY_STRICT, SetList::DEAD_CODE, SetList::PRIVATIZATION, SetList::NAMING, SetList::TYPE_DECLARATION, SetList::ORDER, SetList::PHP_71, SetList::PHP_72, SetList::PHP_73, SetList::PHP_74, SetList::PHP_80, SetList::EARLY_RETURN, SetList::TYPE_DECLARATION_STRICT, SetList::PSR_4, // SetList::SAFE_07, enable if https://github.com/thecodingmachine/safe is used PHPUnitSetList::PHPUNIT_CODE_QUALITY, PHPUnitSetList::PHPUNIT_91, PHPUnitSetList::PHPUNIT_EXCEPTION, PHPUnitSetList::PHPUNIT_YIELD_DATA_PROVIDER, PHPUnitSetList::PHPUNIT_SPECIFIC_METHOD, ]); };
信息:有关现有集合和设置选项的更多信息,请参阅 rectorphp 文档。
Composer
然后编辑您的 composer.json
文件并添加以下脚本
{ "scripts": { "cs": "php-cs-fixer fix --config=\"./.php_cs\" --ansi", "cs:check": "php-cs-fixer fix --config=\"./.php_cs\" --ansi --dry-run", "infection": "XDEBUG_MODE=coverage infection --configuration=\"./infection.json\" -j$(nproc) --ansi", "phpstan": "phpstan analyse -c ./phpstan.neon --ansi --memory-limit=-1", "phpstan:baseline": "phpstan analyse -c ./phpstan.neon --ansi --generate-baseline --memory-limit=-1", "psalm": "psalm --threads=$(nproc)", "psalm:fix": "psalm --alter --issues=all --threads=$(nproc)", "rector": "rector process --ansi --dry-run", "rector:fix": "rector process --ansi", "test": "phpunit", "test:coverage": "phpunit --coverage-html=./.build/phpunit/coverage" } }
提示:如果某些过程比默认的 composer
process-timeout: 300
花费的时间更长,您可以在 composer.json 中添加此内容
{
"config": {
"process-timeout": 2000 #choose you needed time
}
}
将 .php_cs.cache
添加到您的 .gitignore
文件中。
版本控制
此库遵循 语义版本控制,并在主要版本中执行代码规则集的添加。
测试
composer test
贡献
如果您想帮忙查看问题列表并检查我们的CONTRIBUTING.md指南。
注意:请注意,该项目以贡献者行为准则发布。通过参与本项目,您同意遵守其条款。
许可
Narrowspark编码标准是开源软件,许可协议为MIT许可证。