针对hyperf框架改造----编码规范

编码规范

开发分支

  1. 所有开发分支都以develop分支作为基础分支

  2. 分支命名为feature/开头 + 基础分支 + 姓名 + 功能 + 日期。例如: feature/develop-order-zhaohao-0423

路由命名

  • 全部以小写英文编写,单词与单词之间使用下划线隔离

数据库迁移(migration)

  1. 生成迁移文件

## --path=(可选。可以是migrations文件夹下的一个目录)

php bin/hyperf.php gen:migration create_users_table --table=表名
  1. 命名(不允许执行删除表)
  • 创建表结构: create_表名_table

  • 添加DDL: add_column_字段_to_表名

  • 修改DDL: update_column_字段_to_表名

  • 删除DDL: delete_column_字段_to_表名

  • 添加索引: add_index_索引_to_表名

  • 删除索引: delete_index_索引_to_表名

  • 修改索引: 请示领导

  1. 备份(sql审核)
  • 由于开发需要设置了sql审核机制,此migration为便于开发使用,sql必须有自己备份后,提交到sql审核平台

表注释

  • 由于我们使用的为hyperf2.0版本 migration 表注释在2.1版本才放出,我们可以只用原生sql修改表注释,所以需要在创建表的migration文件中添加。例如:

    use HyperfDbConnectionDb;

    /**

     * Run the migrations.

     */

    public function up(): void

    {

        Schema::create('user', function (Blueprint $table) {

            $table->bigIncrements('id')->comment('用户id');

            $table->bigInteger('uid', false, true)->nullable(false)->comment('用户id');

            $table->tinyInteger('status', false, false)->nullable(false)->default(1)->comment('用户状态 1正常');

            $table->timestamps();

            $table->unique('uid', 'uid');

            $table->index('status', 'status');

        });

        Db::statement("ALTER TABLE `users` COMMENT = '用户表'");

    }

编码时注意一下几点

  • servcie方法 记得增加参数的类型以及返回值的类型,接收外部参数记得转换类型

  • 提交代码前要在根目录下执行 composer check。其中: composer cs-fix 格式化代码,composer analyse 静态检测

  • 每个对应的 外部接口 都要编写自动化测试

  • 所有 队列 必须可以重复执行

  • 所有缓存的cache key 必须在对应配置文件中配置

参数的类型以及返回值的类型例子


<?php



declare(strict_types=1);


use AppConstants;

/**

 * 测试

 * Class DevelopTest

 */

class DevelopTest

{

    /**

     * Test constructor.

     */

    public function __construct()

    {



    }



    /**

     * @param int $userId

     * @return int

     */

    public function test(int $userId): int

    {

        return $userId;

    }

}

格式化代码

执行命令 composer cs-fix 格式化代码


> composer cs-fix && composer analyse

> php-cs-fixer fix $1

Loaded config default from "/hyperf-skeleton/项目/.php_cs".

   1) 项目/app/Repositories/BaseRepository.php

   2) 项目/migrations/2021_04_25_153106_creat_user_wechat_table.php

格式化代码的风格在项目根目录.php_cs 中定义,目前按照以下方式来格式化代码


<?php

$header = <<<'EOF'

This file is part of 666.



@link     666

@document 666

EOF;



return PhpCsFixerConfig::create()

    ->setRiskyAllowed(true)

    ->setRules([

        '@PSR2' => true,

        '@Symfony' => true,

        '@DoctrineAnnotation' => true,

        '@PhpCsFixer' => true,

        'header_comment' => [

            'commentType' => 'PHPDoc',

            'header' => $header,

            'separate' => 'none',

            'location' => 'after_declare_strict',

        ],

        'array_syntax' => [

            'syntax' => 'short'

        ],

        'list_syntax' => [

            'syntax' => 'short'

        ],

        'concat_space' => [

            'spacing' => 'one'

        ],

        'blank_line_before_statement' => [

            'statements' => [

                'declare',

            ],

        ],

        'general_phpdoc_annotation_remove' => [

            'annotations' => [

                'author'

            ],

        ],

        'ordered_imports' => [

            'imports_order' => [

                'class', 'function', 'const',

            ],

            'sort_algorithm' => 'alpha',

        ],

        'single_line_comment_style' => [

            'comment_types' => [

            ],

        ],

        'yoda_style' => [

            'always_move_variable' => false,

            'equal' => false,

            'identical' => false,

        ],

        'phpdoc_align' => [

            'align' => 'left',

        ],

        'multiline_whitespace_before_semicolons' => [

            'strategy' => 'no_multi_line',

        ],

        'class_attributes_separation' => true,

        'combine_consecutive_unsets' => true,

        'declare_strict_types' => true,

        'linebreak_after_opening_tag' => true,

        'lowercase_constants' => true,

        'lowercase_static_reference' => true,

        'no_useless_else' => true,

        'no_unused_imports' => true,

        'not_operator_with_successor_space' => true,

        'not_operator_with_space' => false,

        'ordered_class_elements' => true,

        'php_unit_strict' => false,

        'phpdoc_separation' => false,

        'single_quote' => true,

        'standardize_not_equals' => true,

        'multiline_comment_opening_closing' => true,

    ])

    ->setFinder(

        PhpCsFixerFinder::create()

            ->exclude('public')

            ->exclude('runtime')

            ->exclude('vendor')

            ->in(__DIR__)

    )

    ->setUsingCache(false);

静态检测

执行脚本 composer analyse,对项目进行静态检测,便可以找到出现问题的代码段。


$ salesperson-service(develop*) » composer analyse

> phpstan analyse --memory-limit 300M -l 0 -c phpstan.neon ./app ./src ./config

 181/181 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%



 [OK] No errors                        
本作品采用《CC 协议》,转载必须注明作者和本文链接
codezhao
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!