symplify/coding-standard

用于PHP_CodeSniffer和PHP CS Fixer的Symplify规则集。

12.2.3 2024-08-08 08:38 UTC

This package is auto-updated.

Last update: 2024-09-08 08:49:38 UTC


README

Downloads

用于Symplify项目的PHP_CodeSniffer和PHP-CS-Fixer的规则集。

它们与EasyCodingStandard配合使用最佳。.


安装

composer require symplify/coding-standard --dev
composer require symplify/easy-coding-standard --dev
  1. 使用ECS运行
# ecs.php
 use Symplify\EasyCodingStandard\Config\ECSConfig;
+use Symplify\EasyCodingStandard\ValueObject\Set\SetList;

 return static function (ECSConfig $ecsConfig): void {
+    $ecsConfig->sets([SetList::SYMPLIFY]);

12个规则概述

ArrayListItemNewlineFixer

索引PHP数组项必须每项一行

-$value = ['simple' => 1, 'easy' => 2];
+$value = ['simple' => 1,
+'easy' => 2];

ArrayOpenerAndCloserNewlineFixer

索引PHP数组的开方括号[和闭方括号]必须独占一行

-$items = [1 => 'Hey'];
+$items = [
+1 => 'Hey'
+];

BlankLineAfterStrictTypesFixer

严格类型声明后必须跟空行

 declare(strict_types=1);
+
 namespace App;

LineLengthFixer

数组项、方法参数、方法调用参数、新参数应位于同一行/独立行,以适应行长度。

🔧 配置它!

-function some($veryLong, $superLong, $oneMoreTime)
-{
+function some(
+    $veryLong,
+    $superLong,
+    $oneMoreTime
+) {
 }

-function another(
-    $short,
-    $now
-) {
+function another($short, $now) {
 }

MethodChainingNewlineFixer

每个链式方法调用必须独占一行

-$someClass->firstCall()->secondCall();
+$someClass->firstCall()
+->secondCall();

ParamReturnAndVarTagMalformsFixer

修复@param、@return、@var和内联@var注释的损坏格式

 /**
- * @param string
+ * @param string $name
  */
 function getPerson($name)
 {
 }

RemovePHPStormAnnotationFixer

删除“由PhpStorm创建”注释

-/**
- * Created by PhpStorm.
- * User: ...
- * Date: 17/10/17
- * Time: 8:50 AM
- */
 class SomeClass
 {
 }

RemoveUselessDefaultCommentFixer

删除无用的PhpStorm生成的@todo注释、多余的“类XY”或“获取服务”注释等。

-/**
- * class SomeClass
- */
 class SomeClass
 {
-    /**
-     * SomeClass Constructor.
-     */
     public function __construct()
     {
-        // TODO: Change the autogenerated stub
-        // TODO: Implement whatever() method.
     }
 }

SpaceAfterCommaHereNowDocFixer

在nowdoc和heredoc关键字后添加空格,以防止PHP 7.2及以下版本出现错误,请参阅https://news.laravel.net.cn/flexible-heredoc-and-nowdoc-coming-to-php-7-3

 $values = [
     <<<RECTIFY
 Some content
-RECTIFY,
+RECTIFY
+,
     1000
 ];

StandaloneLineConstructorParamFixer

构造函数参数应独占一行,以便于在新依赖项上的git差异

 final class PromotedProperties
 {
-    public function __construct(int $age, string $name)
-    {
+    public function __construct(
+        int $age,
+        string $name
+    ) {
     }
 }

StandaloneLineInMultilineArrayFixer

索引数组必须每行1项

-$friends = [1 => 'Peter', 2 => 'Paul'];
+$friends = [
+    1 => 'Peter',
+    2 => 'Paul'
+];

StandaloneLinePromotedPropertyFixer

提升的属性应该单独一行

 final class PromotedProperties
 {
-    public function __construct(public int $age, private string $name)
-    {
+    public function __construct(
+        public int $age,
+        private string $name
+    ) {
     }
 }