sinCos/php-coding-standards

sinCos PHP 编码标准

2.0.0 2018-10-08 06:38 UTC

README

为我们的 PHP 项目使用的常见 PHP 编码标准。

设置

在 PHP 项目中,按照以下步骤设置编码标准检查:

要求包

composer require --dev sincos/php-coding-standards

添加一个空的测试输出文件夹

mkdir tests/_output
touch tests/_output/.gitkeep

PHPCS 和 PHPMD 将将其报告放在此文件夹中。

忽略新 _output 文件夹中的文件

tests/_output 添加到您的 .gitignore 文件中。

添加 phpcs.xml

以下是一个示例文件

<?xml version="1.0"?>
<ruleset name="Project name here">
    <config name="installed_paths" value="../../sincos/php-coding-standards/phpcs" />

    <rule ref="Sincos"/>

    <arg name="extensions" value="php"/>
    <file>./src</file>

    <!-- Rule customization/configuration example -->
    <rule ref="SlevomatCodingStandard.Files.TypeNameMatchesFileName">
        <properties>
            <property name="rootNamespaces" type="array">
                <element key="src" value="Sincos\Bring"/>
                <element key="tests" value="Sincos\Bring\Tests"/>
            </property>
        </properties>
    </rule>
</ruleset>

添加 phpmd.xml

<?xml version="1.0"?>
<ruleset name="Project name here"
         xmlns="http://pmd.sf.net/ruleset/1.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0 http://pmd.sf.net/ruleset_xml_schema.xsd"
         xsi:noNamespaceSchemaLocation="http://pmd.sf.net/ruleset_xml_schema.xsd">
    <description>
        A nice description here
    </description>

    <exclude-pattern>vendor</exclude-pattern>

    <rule ref="vendor/sincos/php-coding-standards/phpmd/ruleset.xml"/>
</ruleset>

添加一个 Makefile

这是可选的,但可以使命令更容易运行,并具有合理的默认值

在添加到 Makefile 之后,您可以运行例如 make lint

.DEFAULT_GOAL := help
.PHONY: help

help:
	@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST) | sort

phpcs: ## Check PHP Code Sniffer rules. (Optional parameter PARAMS="-additional --params")
	vendor/bin/phpcs --standard=phpcs.xml -d date.timezone=Europe/Oslo --report=junit --report-file=tests/_output/phpcs.xml -d memory_limit=512M $(PARAMS) -s

phpmd: ## Check PHP Mess Detector rules.
	vendor/bin/phpmd . xml phpmd.xml --suffixes php --reportfile tests/_output/phpmd.xml -d memory_limit=512M

phpcbf: ## Run PHPCBF to fix code
	vendor/bin/phpcbf .

lint: ## Run phpcs and phpmd (Optional parameter "FIX=true" added phpcbf on start)
ifdef FIX
	-make phpcbf
endif
	make phpcs
	make phpmd

将所有这些添加到您的 CI 中

以下是从另一个使用 Circle CI 的项目中的示例,其中我们将在 .circleci/config.yml 中添加以下内容:

# PHP CircleCI 2.0 configuration file
#
# Check https://circleci.com/docs/2.0/language-php/ for more details
#
version: 2
jobs:
  build:
    docker:
      - image: circleci/php:7.2.8-node-browsers

    steps:
      - checkout
      - run: sudo composer self-update
      - run: composer install
      - run: ./vendor/bin/phpunit
      - run: make lint
      - store_test_results:
          path: tests/_output
      - store_artifacts:
          path: tests/_output