• yaf常用开发工具:代码格式化&修复、语法检查、单元测试 —— k8s从入门到高并发系列教程 (五)


            上文使用脚手架初始化了yaf框架,并根据github上yar的demo打了两个微服务的镜像运行进行联调。代码规范检查&修复、语法报错检查、单元测试是几乎所有互联网公司上线流程中必须通过的过程,本教程通过安装 php_codesniffer 进行代码规范的检查与修复,phpstan 进行语法报错的检查,phpunit 进行单元测试,依据yaf框架的特征设置配置文件,达成上述目的。

     

    composer初始化

    composer init

    填写的内容如下

    php_codesniffer 代码规范检查与修复

    安装 

    composer require squizlabs/php_codesniffer --dev

     依据yaf框架制定php-cs的标准文件 phpcs.xml

    1. "1.0"?>
    2. <ruleset name="PHP_CodeSniffer">
    3. <rule ref="PSR2">
    4. <exclude name="PSR1.Classes.ClassDeclaration.MissingNamespace"/>
    5. <exclude name="Squiz.Classes.ValidClassName.NotCamelCaps"/>
    6. <exclude name="PSR1.Methods.CamelCapsMethodName.NotCamelCaps"/>
    7. rule>
    8. <rule ref="Generic.Files.LineLength">
    9. <properties>
    10. <property name="lineLimit" value="200"/>
    11. properties>
    12. rule>
    13. <file>applicationfile>
    14. <arg name="encoding" value="utf-8"/>
    15. ruleset>

    这时候,我们可以运行以下命令进行php代码规范的检查

    vendor/bin/phpcs --standard=phpcs.xml --colors

    运行以下命令尝试自我修复

    vendor/bin/phpcbf --standard=phpcs.xml --colors

    如果还有一些没有自动修复的格式规范问题,则需要进行手动修复

    可以在一段代码的开始和结尾处加上以下内容忽略这个区间的代码报错检查 

    1. // @codingStandardsIgnoreStart
    2. // @codingStandardsIgnoreEnd

    php-cs-fixer 代码规范修复

    安装

    composer require friendsofphp/php-cs-fixer --dev

    创建php-cs-fixer 修复规则文件 ​​​​​​.php-cs-fixer.php

    1. return (new PhpCsFixer\Config())
    2. ->setRiskyAllowed(true)
    3. ->setRules([
    4. '@PSR2' => true,
    5. '@Symfony' => true,
    6. '@DoctrineAnnotation' => true,
    7. '@PhpCsFixer' => true,
    8. 'array_syntax' => [
    9. 'syntax' => 'short',
    10. ],
    11. 'list_syntax' => [
    12. 'syntax' => 'short',
    13. ],
    14. 'concat_space' => [
    15. 'spacing' => 'one',
    16. ],
    17. 'blank_line_before_statement' => [
    18. 'statements' => [
    19. 'declare',
    20. ],
    21. ],
    22. 'general_phpdoc_annotation_remove' => [
    23. 'annotations' => [
    24. 'author',
    25. ],
    26. ],
    27. 'ordered_imports' => [
    28. 'imports_order' => [
    29. 'class', 'function', 'const',
    30. ],
    31. 'sort_algorithm' => 'length',
    32. ],
    33. 'single_line_comment_style' => [
    34. 'comment_types' => [
    35. ],
    36. ],
    37. 'yoda_style' => [
    38. 'always_move_variable' => false,
    39. 'equal' => false,
    40. 'identical' => false,
    41. ],
    42. 'phpdoc_align' => [
    43. 'align' => 'left',
    44. ],
    45. 'multiline_whitespace_before_semicolons' => [
    46. 'strategy' => 'no_multi_line',
    47. ],
    48. 'constant_case' => [
    49. 'case' => 'lower',
    50. ],
    51. 'class_attributes_separation' => true,
    52. 'combine_consecutive_unsets' => true,
    53. 'declare_strict_types' => false,
    54. 'linebreak_after_opening_tag' => true,
    55. 'lowercase_static_reference' => true,
    56. 'no_useless_else' => true,
    57. 'no_unused_imports' => true,
    58. 'not_operator_with_successor_space' => true,
    59. 'not_operator_with_space' => false,
    60. 'ordered_class_elements' => true,
    61. 'php_unit_strict' => false,
    62. 'phpdoc_separation' => false,
    63. 'single_quote' => true,
    64. 'standardize_not_equals' => true,
    65. 'multiline_comment_opening_closing' => true,
    66. ])
    67. ->setFinder(
    68. PhpCsFixer\Finder::create()
    69. ->exclude('conf')
    70. ->exclude('vendor')
    71. ->in(__DIR__)
    72. )
    73. ->setUsingCache(false);

    运行以下命令进行代码规范修复

    vendor/bin/php-cs-fixer fix


    为了方便记住phpcs 和 phpcbf 命令的内容,可以在composer.json中添加 scripts 片段 

    1. "scripts": {
    2. "cs": "phpcs --standard=phpcs.xml --colors",
    3. "cs-fix": "phpcbf --standard=phpcs.xml --colors && php-cs-fixer fix"
    4. }

     以后可以用下面两个命令进行php cs规范检查和 cs-fix自动修复

    1. composer cs
    2. composer cs-fix

    phpstan语法报错检查

    安装

    composer require phpstan/phpstan --dev

    依据yaf框架特点,设置配置文件 phpstan.neon ,忽略一些报错

    1. parameters:
    2. reportUnmatchedIgnoredErrors: false
    3. ignoreErrors:
    4. - '#Result of static method Yaf\_Application::app\(\) \(void\) is used#'
    5. - '#Instantiated class XHProfRuns\_Default not found.#'

    这时候,我们可以运行以下命令进行php语法报错的检查

    vendor/bin/phpstan analyse --memory-limit 800M -l 0 -c phpstan.neon ./application

    根据报错提示内容自己手动修复

    为了方便记住phpstan 检查报错命令的内容,可以在composer.json中scripts片段添加以下内容

    "analyse": "phpstan analyse --memory-limit 800M -l 0 -c phpstan.neon ./application"

    以后可以运行 composer analyse 命令进行php语法检查了

    phpunit单元测试

    安装

    composer require phpunit/phpunit --dev

    单元测试代码放在 tests 目录下,依据yaf框架特点 单元测试的入口文件 tests/index.php 内容如下

    1. define('APPLICATION_PATH', dirname(__DIR__), true);
    2. $application = new Yaf_Application(APPLICATION_PATH . "/conf/application.ini");
    3. Yaf_Dispatcher::getInstance()->autoRender(false);
    4. $application->bootstrap()->getDispatcher()->dispatch(new Yaf_Request_Simple());

    创建单元测试文件 tests/models/SampleTest.php

    内容如下

    1. use PHPUnit\Framework\TestCase;
    2. class SampleTest extends TestCase
    3. {
    4. public function testSelectSample()
    5. {
    6. $model = new SampleModel();
    7. $ret = $model->selectSample();
    8. $this->assertEquals('Hello World!', $ret);
    9. }
    10. }

    运行这个函数的测试用例,方法如下

    ./vendor/bin/phpunit --bootstrap  tests/index.php tests/models/SampleTest.php --filter SelectSample

  • 相关阅读:
    阿里技术官首发的Java核心框架指导手册,为了大厂得码住学起来~
    ELFK 分布式日志收集系统
    Python-爬虫(正则表达式基础、修饰符、元字符、数量修饰符,练习判断身份证是否正确)
    ETCD快速入门-03 常用命令
    【数据结构】树与二叉树(八):二叉树的中序遍历(非递归算法NIO)
    【OpenCV】基于cv2的图像阈值化处理【超详细的注释和解释】掌握基本操作
    playwright的安装与使用
    C#使用Selenium WebDriver模拟人工操作网页方法
    【C++红黑树】带图详细解答红黑树的插入,测试自己的红黑树是否正确的代码
    变电站监控视频中运动异常运动物检测和跟踪技术的研究
  • 原文地址:https://blog.csdn.net/fanghailiang2016/article/details/126555118