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

安装
composer require squizlabs/php_codesniffer --dev
依据yaf框架制定php-cs的标准文件 phpcs.xml
- "1.0"?>
- <ruleset name="PHP_CodeSniffer">
-
- <rule ref="PSR2">
- <exclude name="PSR1.Classes.ClassDeclaration.MissingNamespace"/>
- <exclude name="Squiz.Classes.ValidClassName.NotCamelCaps"/>
- <exclude name="PSR1.Methods.CamelCapsMethodName.NotCamelCaps"/>
- rule>
- <rule ref="Generic.Files.LineLength">
- <properties>
- <property name="lineLimit" value="200"/>
- properties>
- rule>
- <file>applicationfile>
- <arg name="encoding" value="utf-8"/>
- ruleset>
这时候,我们可以运行以下命令进行php代码规范的检查
vendor/bin/phpcs --standard=phpcs.xml --colors
运行以下命令尝试自我修复
vendor/bin/phpcbf --standard=phpcs.xml --colors
如果还有一些没有自动修复的格式规范问题,则需要进行手动修复
可以在一段代码的开始和结尾处加上以下内容忽略这个区间的代码报错检查
- // @codingStandardsIgnoreStart
- // @codingStandardsIgnoreEnd
php-cs-fixer 代码规范修复安装
composer require friendsofphp/php-cs-fixer --dev
创建php-cs-fixer 修复规则文件 .php-cs-fixer.php
-
- return (new PhpCsFixer\Config())
- ->setRiskyAllowed(true)
- ->setRules([
- '@PSR2' => true,
- '@Symfony' => true,
- '@DoctrineAnnotation' => true,
- '@PhpCsFixer' => true,
- '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' => 'length',
- ],
- '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',
- ],
- 'constant_case' => [
- 'case' => 'lower',
- ],
- 'class_attributes_separation' => true,
- 'combine_consecutive_unsets' => true,
- 'declare_strict_types' => false,
- '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('conf')
- ->exclude('vendor')
- ->in(__DIR__)
- )
- ->setUsingCache(false);
运行以下命令进行代码规范修复
vendor/bin/php-cs-fixer fix
为了方便记住phpcs 和 phpcbf 命令的内容,可以在composer.json中添加 scripts 片段
- "scripts": {
- "cs": "phpcs --standard=phpcs.xml --colors",
- "cs-fix": "phpcbf --standard=phpcs.xml --colors && php-cs-fixer fix"
- }
以后可以用下面两个命令进行php cs规范检查和 cs-fix自动修复
- composer cs
-
- composer cs-fix
安装
composer require phpstan/phpstan --dev
依据yaf框架特点,设置配置文件 phpstan.neon ,忽略一些报错
- parameters:
- reportUnmatchedIgnoredErrors: false
- ignoreErrors:
- - '#Result of static method Yaf\_Application::app\(\) \(void\) is used#'
- - '#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语法检查了
安装
composer require phpunit/phpunit --dev
单元测试代码放在 tests 目录下,依据yaf框架特点 单元测试的入口文件 tests/index.php 内容如下
-
- define('APPLICATION_PATH', dirname(__DIR__), true);
-
- $application = new Yaf_Application(APPLICATION_PATH . "/conf/application.ini");
-
- Yaf_Dispatcher::getInstance()->autoRender(false);
-
- $application->bootstrap()->getDispatcher()->dispatch(new Yaf_Request_Simple());
创建单元测试文件 tests/models/SampleTest.php
内容如下
-
- use PHPUnit\Framework\TestCase;
-
- class SampleTest extends TestCase
- {
- public function testSelectSample()
- {
- $model = new SampleModel();
- $ret = $model->selectSample();
- $this->assertEquals('Hello World!', $ret);
- }
-
- }
运行这个函数的测试用例,方法如下
./vendor/bin/phpunit --bootstrap tests/index.php tests/models/SampleTest.php --filter SelectSample