前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >针对hyperf框架改造----编码规范

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

作者头像
美团骑手
发布2021-04-28 09:39:04
1K0
发布2021-04-28 09:39:04
举报
文章被收录于专栏:技术进阶技术进阶

编码规范

  • 遵循psr规范 https://learnku.com/docs/psr

开发分支

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

路由命名

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

数据库迁移(migration)

  1. 生成迁移文件
代码语言:javascript
复制
## --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文件中添加。例如:
代码语言:javascript
复制
    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 必须在对应配置文件中配置

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

代码语言:javascript
复制
<?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 格式化代码

代码语言:javascript
复制
> composer cs-fix && composer analyze

> 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 中定义,目前按照以下方式来格式化代码

代码语言:javascript
复制
<?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,对项目进行静态检测,便可以找到出现问题的代码段。

代码语言:javascript
复制
$ salesperson-service(develop*) » composer analyse

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

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

 [OK] No errors                        
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2021-04-27 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 编码规范
  • 开发分支
    • 路由命名
      • 数据库迁移(migration)
      • 表注释
      • 编码时注意一下几点
        • 参数的类型以及返回值的类型例子
        • 格式化代码
          • 静态检测
          相关产品与服务
          数据传输服务
          腾讯云数据传输服务(Data Transfer Service,DTS)可帮助用户在业务不停服的前提下轻松完成数据库迁移上云,利用实时同步通道轻松构建高可用的数据库多活架构,通过数据订阅来满足商业数据挖掘、业务异步解耦等场景需求。同时,DTS 还提供私有化独立输出版本 DTS-DBbridge,支持异构数据库和同构数据库之间迁移和同步,可以帮助企业实现完整数据库迁移(如 Oracle)。
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档