Wake Me Up When September Ends.

A wanderer capable of grasping the beauty of the grass, with a heart full of ancient charm, and a fondness for playful wit. Those who understand my words are knowledgeable individuals; those who decipher my code truly comprehend the essence. I am a wandering code swordsman, carrying my skills and riding freely through the digital world.

PHP代码统一规范规则细节

PHPDoc

  1. 变量以及属性的辅助注解.方便IDE跳转和追踪
 <?php
/** @var int $foo */
 $foo = 2 + 2;


 final class Foo
 {
     /**
      * @var int
      */
     public $bar;

     /**
      * @type float
      */
     public $baz;
 }
>
  1. 变量以及属性多态类型优先级
# 修正前: 

/**
 * @param null|string|int|\Foo $bar
 */
# 修正后:
/**
 * @param \Foo|int|null|string $bar
 */
  1. 注释对齐(居中非依左或不对齐)
/**
 
# 修改前
 * @param  EngineInterface $templating
 * @param string      $format
 * @param  int  $code       an HTTP response status code
 * @param    bool         $debug
 * @param  mixed    &$reference     a parameter passed by reference

 
# 修改后
 * @param EngineInterface $templating
 * @param string          $format
 * @param int             $code       an HTTP response status code
 * @param bool            $debug
 * @param mixed           &$reference a parameter passed by reference
  1. 未确定类型使用mixed声明,默认值带为null需要用?修饰符
/**
  * @param string|null   $foo 
  * @param int           $bar
  * @param mixed         $baz
  *
  * @return void
  */
 function foo(?string = null $foo, int $bar, $baz) {}
  1. 数组key以及类属性对齐
# 修改前
class Foo
{
    public  $a = 0;

    public $b = 0;
    public $c = 0;

        public$d = 0;
}
# 修改后
class Foo
{
    public $a = 0;

    public $b = 0;

    public $c = 0;

    public $d = 0;
}
# 修改前
$arr = [
    'a'=>'tom',
    'c' =>  'sevne',
    'd'=>  'mazda',
    'b'  =>'cross m78 unm',
]
# 修改后
$arr = [
    'a' => 'tom',
    'c' => 'sevne',
    'd' => 'mazda',
    'b' => 'cross m78 unm',
]

代码约束

  1. declare_strict 严格声明。主要解决程序在处理类型时候避免隐性转换
declare(strict_types=1);
//code ....
  1. 使用常量PHP_EOL替代 "\n" 换行符号

echo  "some thing \n";
echo  "some thing PHP_EOL";
  1. 声明函数必须使用function_exists包裹判断,避免函数重复声明
if (!function_exists('dd')) {
    /**
     * dd 调试
     *
     * @param ...$vars
     *
     * @return void
     */
    function dd(...$vars)
    {
        foreach ($vars as $v) {
            VarDumper::dump($v);
        }
        exit(1);
    }
}
  1. 函数或类的方法没有返回类型需要声明void类型
function foo(string $a): void {}


class Foo
{
    public function handle(): void;
    {
        //code ...
    }
}
  1. 命名空间导入,导入或完全限定全局类/函数/常量
<?php

# 修改前
$d = new \DateTimeImmutable();
# 修改后
$d = new DateTimeImmutable();
  1. 使用::class替换完整类名使用,以及替代get_class函数
# 修改前
$className = 'Foo\Bar\Baz';
# 修改后
$className = Baz::class;
  1. 多次调用转换成单次调用
# 修改前
$a = isset($a) && isset($b);
# 修改后
$a = isset($a, $b)  ;

# 修改前
unset($a);
unset($b);
# 修改后
unset($a, $b);
  1. 使用语法糖简化调用
# 修改前
$boo = isset($foo) ? $foo : null;
# 修改后
$boo = $foo ?? null; 
//或
$foo ??= null;

# 修改前
$boo = $foo ? $foo : '';
# 修改后
$boo = $foo ?:'';


# 修改前
list($a, $b) = $foos;
# 修改后
[$a, $b] = $foos;
  1. 变量占位符由${ 转换{$) php的高版本已经抛弃${用法
$name = 'World';
# 修改前
echo "Hello ${name}!";
# 修改后
echo "Hello {$name}!";
  1. PHP常量true、false和null须使用小写
# 修改前
$a = FALSE;
$b = True;
$c = NULL;
# 修改后
$a = false;
$b = true;
$c = null;
  1. 使用动态属性或方法,属性需要实现__get__set,方法需要_call()__callStatic以及在class 头部声明,便于ide识别以及查阅
/**
 * Class Foo
 * 
 * @property string $attr  
 * @method Foo handleA(string $arg) 
 */
class Foo
{
    public function __set($name, $value)
    {
        //code 
    }

    
    public function __get($name)
    {
        //code 
    }


    public function __call($method, $args)
    {
        //code 
    }

    public static function __callStatic($method, $args)
    {
        //code 
    }
}

耦合优化

  1. 使用依赖对象处理多参数方法或函数,避免后面迭代出现超级函数
# 修改前
function  foo(string $argv, int $argc, array $arg, \Closure $argz) :void
{
    //code 
}
# 修改后

Class Boo
{
    public string $argv;

    public string $argc;

    public string $argg;

    public function argz() : Closure
    {
        return  function (){ };
    }
}

function  foo(Boo $boo) :void
{
    //code 
}