Skip to content

Commit

Permalink
Merge pull request #7 from PHPCSStandards/feature/add-the-base-rulesets
Browse files Browse the repository at this point in the history
Add the base rulesets
  • Loading branch information
jrfnl authored Dec 26, 2019
2 parents 39c5671 + ded6919 commit 8a17759
Show file tree
Hide file tree
Showing 9 changed files with 296 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
/.gitattributes export-ignore
/.gitignore export-ignore
/.travis.yml export-ignore
/phpcs.xml.dist export-ignore

#
# Auto detect text files and perform LF normalization
Expand Down
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
vendor/
composer.lock
/composer.lock
/.phpcs.xml
/phpcs.xml
43 changes: 40 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ language: php

## Cache composer and apt downloads.
cache:
apt: true
directories:
# Cache directory for older Composer versions.
- $HOME/.composer/cache/files
Expand All @@ -13,20 +14,56 @@ cache:
php:
- 5.4
- 7.3
- "nightly"

# Define the stages used.
stages:
- name: sniff
- name: test

jobs:
fast_finish: true
include:
#### SNIFF STAGE ####
- stage: sniff
php: 7.3
addons:
apt:
packages:
- libxml2-utils
script:
# Validate the composer.json file.
# @link https://getcomposer.org/doc/03-cli.md#validate
- composer validate --no-check-all --strict

# Check the code style of the code base.
- composer travis-checkcs

# Validate the xml files.
# @link http://xmlsoft.org/xmllint.html
- xmllint --noout --schema ./vendor/squizlabs/php_codesniffer/phpcs.xsd ./PHPCSUtils/ruleset.xml
- xmllint --noout --schema ./vendor/squizlabs/php_codesniffer/phpcs.xsd ./PHPCS23Utils/ruleset.xml

# Check the code-style consistency of the xml files.
- diff -B ./PHPCSUtils/ruleset.xml <(xmllint --format "./PHPCSUtils/ruleset.xml")
- diff -B ./PHPCS23Utils/ruleset.xml <(xmllint --format "./PHPCS23Utils/ruleset.xml")

allow_failures:
# Allow failures for unstable builds.
- php: "nightly"


before_install:
# Speed up build time by disabling Xdebug when its not needed.
- phpenv config-rm xdebug.ini || echo 'No xdebug config.'

- export XMLLINT_INDENT=" "

# --prefer-dist will allow for optimal use of the travis caching ability.
# The Composer PHPCS plugin takes care of setting the installed_paths for PHPCS.
- composer install --prefer-dist --no-suggest


script:
# Validate the composer.json file on low/high PHP versions.
# @link https://getcomposer.org/doc/03-cli.md#validate
- composer validate --no-check-all --strict
# Lint PHP files against parse errors.
- composer lint
52 changes: 52 additions & 0 deletions PHPCS23Utils/Sniffs/Load/LoadUtilsSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/**
* PHPCSUtils, utility functions and classes for PHP_CodeSniffer sniff developers.
*
* @package PHPCSUtils
* @copyright 2019 PHPCSUtils Contributors
* @license https://opensource.org/licenses/LGPL-3.0 LGPL3
* @link https://github.com/PHPCSStandards/PHPCSUtils
*/

namespace PHPCS23Utils\Sniffs\Load;

/*
* Here be magic.
*
* This include allows for the Utility functions to work PHPCS cross-version.
*/
require_once \dirname(\dirname(\dirname(__DIR__))) . '/phpcsutils-autoload.php';

/**
* Dummy Sniff.
*
* This sniff doesn't do anything. It's just here to trigger the above include.
*
* @since 1.0.0
*/
class LoadUtilsSniff
{

/**
* Returns an array of tokens this test wants to listen for.
*
* @return array
*/
public function register()
{
return [];
}

/**
* Processes this test, when one of its tokens is encountered.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token in
* the stack passed in $tokens.
*
* @return void
*/
public function process($phpcsFile, $stackPtr)
{
}
}
4 changes: 4 additions & 0 deletions PHPCS23Utils/ruleset.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0"?>
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="PHPCS23Utils" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/squizlabs/PHP_CodeSniffer/master/phpcs.xsd">
<description>Standard which makes the PHPCSUtils utility methods for external PHPCS standards available in PHPCS 2.x.</description>
</ruleset>
4 changes: 4 additions & 0 deletions PHPCSUtils/ruleset.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0"?>
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="PHPCSUtils" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/squizlabs/PHP_CodeSniffer/master/phpcs.xsd">
<description>Utility methods for external PHPCS standards.</description>
</ruleset>
34 changes: 34 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,39 @@
"php" : ">=5.4",
"squizlabs/php_codesniffer" : "^2.6.0 || ^3.1.0",
"dealerdirect/phpcodesniffer-composer-installer" : "^0.3 || ^0.4.1 || ^0.5.0"
},
"require-dev" : {
"jakub-onderka/php-parallel-lint": "^1.0",
"jakub-onderka/php-console-highlighter": "^0.4"
},
"minimum-stability": "dev",
"prefer-stable": true,
"autoload": {
"classmap": ["PHPCSUtils/"]
},
"scripts" : {
"lint": [
"@php ./vendor/jakub-onderka/php-parallel-lint/parallel-lint . -e php --exclude vendor --exclude .git"
],
"install-devtools": [
"composer require --dev phpcsstandards/phpcsdevtools:\"^1.0 || dev-develop\" --no-suggest"
],
"remove-devtools": [
"composer remove --dev phpcsstandards/phpcsdevtools"
],
"checkcs": [
"@install-devtools",
"@php ./vendor/squizlabs/php_codesniffer/bin/phpcs",
"@remove-devtools"
],
"fixcs": [
"@install-devtools",
"@php ./vendor/squizlabs/php_codesniffer/bin/phpcbf",
"@remove-devtools"
],
"travis-checkcs": [
"@install-devtools",
"@php ./vendor/squizlabs/php_codesniffer/bin/phpcs"
]
}
}
63 changes: 63 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?xml version="1.0"?>
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="Coding Standard for PHPCSUtils" xsi:noNamespaceSchemaLocation="./vendor/squizlabs/php_codesniffer/phpcs.xsd">
<description>Check the code of the PHPCSUtils standard.</description>

<!--
#############################################################################
COMMAND LINE ARGUMENTS
https://github.com/squizlabs/PHP_CodeSniffer/wiki/Annotated-ruleset.xml
#############################################################################
-->

<file>.</file>

<!-- Exclude Composer vendor directory. -->
<exclude-pattern>*/vendor/*</exclude-pattern>

<!-- Only check PHP files. -->
<arg name="extensions" value="php"/>

<!-- Show progress, show the error codes for each message (source). -->
<arg value="ps"/>

<!-- Strip the filepaths down to the relevant bit. -->
<arg name="basepath" value="./"/>

<!-- Check up to 8 files simultaneously. -->
<arg name="parallel" value="8"/>

<!--
#############################################################################
USE THE PHPCSDev RULESET
#############################################################################
-->

<rule ref="PHPCSDev">
<!-- Allow for the file docblock on the line directly following the PHP open tag.
As the sniff in PHPCS does not use modular error codes (yet - see PR #2729),
the complete error code needs to be disabled, not just the bit involving
the file docblocks.
-->
<exclude name="PSR12.Files.FileHeader.SpacingAfterBlock"/>
</rule>

<!-- Set minimum PHP version supported to PHP 5.4. -->
<config name="testVersion" value="5.4-"/>

<!-- Enforce short arrays. -->
<rule ref="Generic.Arrays.DisallowLongArraySyntax"/>


<!--
#############################################################################
SELECTIVE EXCLUSIONS
Exclude specific files for specific sniffs and/or exclude sub-groups in sniffs.
#############################################################################
-->

<rule ref="PSR1.Files.SideEffects.FoundWithSymbols">
<exclude-pattern>/phpcsutils-autoload\.php$</exclude-pattern>
<exclude-pattern>/PHPCS23Utils/Sniffs/Load/LoadUtilsSniff\.php$</exclude-pattern>
</rule>

</ruleset>
95 changes: 95 additions & 0 deletions phpcsutils-autoload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php
/**
* PHPCSUtils, utility functions and classes for PHP_CodeSniffer sniff developers.
*
* Autoloader for the PHPCSUtils files.
* Also provides PHPCS cross-version class aliases.
*
* - If an external standard only supports PHPCS >= 3.1.0 and uses the PHPCS
* native unit test framework, this file does not need to be included.
*
* - When PHPCS 2.x support is desired, include the "PHPCS23Utils" standard
* in the ruleset of the external standard and this file will be included
* automatically.
* Including this file will allow PHPCSUtils to work in both PHPCS 2.x
* as well as PHPCS 3.x.
*
* - If an external standard uses its own unit test setup, this file should
* be included from the unit test bootstrap file.
*
* @package PHPCSUtils
* @copyright 2019 PHPCSUtils Contributors
* @license https://opensource.org/licenses/LGPL-3.0 LGPL3
* @link https://github.com/PHPCSStandards/PHPCSUtils
*
* @since 1.0.0
*/

if (defined('PHPCSUTILS_AUTOLOAD') === false) {
/*
* Register an autoloader.
*
* External PHPCS standards which have their own unit test suite
* should include this file in their test runner bootstrap.
*/
spl_autoload_register(function ($class) {
// Only try & load our own classes.
if (stripos($class, 'PHPCSUtils') !== 0) {
return;
}

$file = realpath(__DIR__) . DIRECTORY_SEPARATOR . strtr($class, '\\', DIRECTORY_SEPARATOR) . '.php';

if (file_exists($file)) {
include_once $file;
}
});

define('PHPCSUTILS_AUTOLOAD', true);
}

if (defined('PHPCSUTILS_PHPCS_ALIASES_SET') === false) {
/*
* Alias a number of PHPCS 2.x classes to their PHPCS 3.x equivalents.
*
* {@internal The PHPCS file have been reorganized in PHPCS 3.x, quite
* a few "old" classes have been split and spread out over several "new"
* classes. In other words, this will only work for a limited number
* of classes.}}
*
* {@internal The `class_exists` wrappers are needed to play nice with other
* external PHPCS standards creating cross-version compatibility in a
* similar manner.}}
*/
if (interface_exists('\PHP_CodeSniffer_Sniff') === true
&& interface_exists('\PHP_CodeSniffer\Sniffs\Sniff') === false
) {
class_alias('\PHP_CodeSniffer_Sniff', '\PHP_CodeSniffer\Sniffs\Sniff');
}

if (class_exists('\PHP_CodeSniffer_File') === true
&& class_exists('\PHP_CodeSniffer\Files\File') === false
) {
class_alias('\PHP_CodeSniffer_File', '\PHP_CodeSniffer\Files\File');
}

if (class_exists('\PHP_CodeSniffer_Tokens') === true
&& class_exists('\PHP_CodeSniffer\Util\Tokens') === false
) {
class_alias('\PHP_CodeSniffer_Tokens', '\PHP_CodeSniffer\Util\Tokens');
}

if (class_exists('\PHP_CodeSniffer_Exception') === true
&& class_exists('\PHP_CodeSniffer\Exceptions\RuntimeException') === false
) {
class_alias('\PHP_CodeSniffer_Exception', '\PHP_CodeSniffer\Exceptions\RuntimeException');
}

if (class_exists('\PHP_CodeSniffer_Exception') === true
&& class_exists('\PHP_CodeSniffer\Exceptions\TokenizerException') === false
) {
class_alias('\PHP_CodeSniffer_Exception', '\PHP_CodeSniffer\Exceptions\TokenizerException');
}

define('PHPCSUTILS_PHPCS_ALIASES_SET', true);
}

0 comments on commit 8a17759

Please sign in to comment.