For the transformation of hyperf framework -- Coding specification

Coding specification

Development Branch

  1. All development branches are based on the development branch

  2. The branch is named as feature / beginning + basic branch + name + function + date. For example: feature / develop-order-zhao-0423

Route naming

  • All are written in lowercase English, with underlined isolation between words

Database migration

  1. Generate migration file
## --path = (optional. It can be a directory under the migrations folder)

php bin/hyperf.php gen:migration create_users_table --table=Table name
  1. Naming (deleting tables is not allowed)
  • Create table structure: create_ Table name_ table

  • Add DDL: add_column_ Field_ to_ Table name

  • Modify DDL: update_column_ Field_ to_ Table name

  • Delete DDL: delete_column_ Field_ to_ Table name

  • Add index: add_index_ Index_ to_ Table name

  • Delete index: delete_index_ Index_ to_ Table name

  • Modify the index: ask the leader for instructions

  1. Backup (sql audit)
  • Because the development needs to set up the sql audit mechanism, in order to facilitate the development and use of this migration, the sql must be backed up and submitted to the sql audit platform

Table notes

  • Because we use hyperf2 The migration table annotation of version 0 is released only in version 2.1. We can modify the table annotation only with native sql, so we need to add it in the migration file where the table is created. For example:
    use HyperfDbConnectionDb;

    /**

     * Run the migrations.

     */

    public function up(): void

    {

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

            $table->bigIncrements('id')->comment('user id');

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

            $table->tinyInteger('status', false, false)->nullable(false)->default(1)->comment('User status 1 normal');

            $table->timestamps();

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

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

        });

        Db::statement("ALTER TABLE `users` COMMENT = 'User table'");

    }

Pay attention to some points when coding

  • The servcie method remembers to add the type of parameter and the type of return value, and remember to convert the type of external parameter

  • Before submitting code, execute composer check in the root directory. Where: composer CS fix format code, composer analyze static detection

  • Each corresponding external interface should write automated tests

  • All queues must be repeatable

  • All cached cache key s must be configured in the corresponding configuration file

The type of the parameter and an example of the type of the return value

<?php



declare(strict_types=1);


use AppConstants;

/**

 * test

 * Class DevelopTest

 */

class DevelopTest

{

    /**

     * Test constructor.

     */

    public function __construct()

    {



    }



    /**

     * @param int $userId

     * @return int

     */

    public function test(int $userId): int

    {

        return $userId;

    }

}

formatting code

Execute the command composer CS fix to format the code

> composer cs-fix && composer analyse

> php-cs-fixer fix $1

Loaded config default from "/hyperf-skeleton/project/.php_cs".

   1) project/app/Repositories/BaseRepository.php

   2) project/migrations/2021_04_25_153106_creat_user_wechat_table.php

The style of formatting code is in the project root directory php_cs. At present, the code is formatted as follows

<?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);

Static detection

Execute the script composer analyze to statically detect the project, and then you can find the code segment with the problem.

$ salesperson-service(develop*) » composer analyse

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

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



 [OK] No errors                        

Keywords: PHP

Added by gio2k on Mon, 10 Jan 2022 08:20:53 +0200