Hyperf 开发注意事项(自己使用)

博客分类: software 阅读次数: comments

Hyperf 开发注意事项(自己使用)

注意一下几点

例子以及文档

git flow 文档

1.代码从 develop分支 切功能分支开发,最后变基回 develop分支,可看下 git pull --rebase 命令
2.影响 master分支 流程的 bug 要从 master分支hotfix分支 开发

文档地址:https://www.jianshu.com/p/41910dc6ef29

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

<?php

declare(strict_types=1);

/**
 * class Foo
 *
 * @author shixuefeng <shixuefeng@shopex.cn>
 */
class Foo() {

    
    /**
     * function test
     *
     * @author shixuefeng <shixuefeng@shopex.cn>
     * @param integer $id 传递id
     * @return integer 传递id增加1
     */
    public function test(int $id): int 
    {
        return $id + 1;
    }
}

格式化代码

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

$ demo(develop*) » composer cs-fix
> php-cs-fixer fix $1
Loaded config default from "/Users/xxxx/workspace/www/salesperson/demo/.php_cs".
   1) demo/App/Extend/Tests/FooTest.php

Fixed all files in 5.551 seconds, 20.000 MB memory used

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

<?php
$header = <<<'EOF'
This file is part of Canren .
@link     https://canren.github.io/
@document https://canren.github.io/2021/02/20/02.html
@contact  sxffind@126.com
EOF;
return PhpCsFixer\Config::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_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(
        PhpCsFixer\Finder::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