安裝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[.規則])