安装php
composer require --dev squizlabs/php_codesniffer:~2.7
配置
项目根目录下设定配置文件 phpcs.xml
bootstrap
<?xml version="1.0"?> <ruleset name="Mysite PHPCS Code Standard"> <description>The coding standard at mysite.com</description> <arg name="tab-width" value="4" /> <arg name="encoding" value="utf-8" /> <arg value="s" /> <rule ref="PSR2"> <exclude name="PSR1.Classes.ClassDeclaration.MissingNamespace" /> </rule> <file>./app</file> <file>./tests</file> </ruleset>
运行bash
./vendor/bin/phpcs -i # 查看已安装的规范 # 程序路径 规范路径 ./vendor/bin/phpcs --standard=phpcs.xml ./vendor/bin/phpcbf --standard=phpcs.xml
安装app
composer require --dev phpmd/phpmd:~2.5
运行composer
#程序路径 测试路径 报告格式 规则路径 ./vendor/bin/phpmd app,tests text resources/rulesets/cleancode.xml,resources/rulesets/codesize.xml,resources/rulesets/controversial.xml,resources/rulesets/design.xml,resources/rulesets/naming.xml,resources/rulesets/unusedcode.xml
安装单元测试
composer require --dev phpunit/phpunit:~5.0
配置
项目根目录下设定配置文件 phpunit.xml
测试
<?xml version="1.0" encoding="UTF-8"?> <phpunit backupGlobals="false" backupStaticAttributes="false" bootstrap="bootstrap/app.php" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" syntaxCheck="false"> <testsuites> <testsuite name="Feature Tests"> <directory suffix="Test.php">./tests/Feature</directory> </testsuite> <testsuite name="Unit Tests"> <directory suffix="Test.php">./tests/Unit</directory> </testsuite> </testsuites> <filter> <whitelist processUncoveredFilesFromWhitelist="true"> <directory suffix=".php">./app</directory> </whitelist> </filter> <php> <env name="APP_ENV" value="testing"/> <env name="DB_CONNECTION" value="testing"/> <env name="CACHE_DRIVER" value="array"/> <env name="QUEUE_DRIVER" value="sync"/> <env name="APP_KEY" value="base64:应用秘钥"/> </php> </phpunit>
运行ui
./vendor/bin/phpunit --configuration phpunit.xml
临时关闭规则编码
# 代码块上添加注解命令,phpmd规则举例 @SuppressWarnings(PHPMD[.规则])