Skip to content

Commit

Permalink
Merge pull request #33 from PHPCSStandards/develop
Browse files Browse the repository at this point in the history
Release version 1.0.0
  • Loading branch information
jrfnl authored Feb 12, 2020
2 parents 5d29b83 + 2bd8c36 commit 0f0776e
Show file tree
Hide file tree
Showing 19 changed files with 1,436 additions and 10 deletions.
4 changes: 4 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
/.gitattributes export-ignore
/.gitignore export-ignore
/.travis.yml export-ignore
/phpcs.xml.dist export-ignore
/phpunit.xml.dist export-ignore
/phpunit-bootstrap.php export-ignore
/PHPCSDebug/Tests export-ignore

#
# Auto detect text files and perform LF normalization
Expand Down
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
vendor/
composer.lock
.phpcs.xml
phpcs.xml
phpunit.xml
.phpunit.result.cache
130 changes: 126 additions & 4 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 @@ -12,21 +13,142 @@ cache:

php:
- 5.4
- 7.3
- 5.5
- 5.6
- 7.0
- 7.1
- 7.2

env:
# `master`
- PHPCS_VERSION="dev-master" LINT=1
# Lowest supported PHPCS version.
- PHPCS_VERSION="3.0.2"

# Define the stages used.
# For non-PRs, only the sniff and quicktest stages are run.
# For pull requests and merges, the full script is run (skipping quicktest).
# Note: for pull requests, "develop" is the base branch name.
# See: https://docs.travis-ci.com/user/conditions-v1
stages:
- name: sniff
- name: quicktest
if: type = push AND branch NOT IN (master, develop)
- name: test
if: branch IN (master, develop)

jobs:
fast_finish: true
include:
#### SNIFF STAGE ####
- stage: sniff
php: 7.3
env: PHPCS_VERSION="dev-master"
addons:
apt:
packages:
- libxml2-utils
script:
# Check the code style of the code base.
- composer check-cs

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

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

# Validate the composer.json file.
# @link https://getcomposer.org/doc/03-cli.md#validate
- composer validate --no-check-all --strict

#### QUICK TEST STAGE ####
# This is a much quicker test which only runs the unit tests and linting against the low/high
# supported PHP/PHPCS combinations.
- stage: quicktest
php: 7.3
env: PHPCS_VERSION="dev-master" LINT=1
- stage: quicktest
php: 7.2
env: PHPCS_VERSION="3.0.2"

- stage: quicktest
php: 5.4
env: PHPCS_VERSION="dev-master" LINT=1
- stage: quicktest
php: 5.4
env: PHPCS_VERSION="3.0.2"

#### TEST STAGE ####
# Additional builds to prevent issues with PHPCS versions incompatible with certain PHP versions.
- stage: test
php: 7.3
env: PHPCS_VERSION="dev-master" LINT=1
# PHPCS is only compatible with PHP 7.3 as of version 3.3.1.
- php: 7.3
env: PHPCS_VERSION="3.3.1"
- php: 7.4
env: PHPCS_VERSION="dev-master"
# PHPCS is only compatible with PHP 7.4 as of version 3.5.0.
- php: 7.4
env: PHPCS_VERSION="3.5.0"
- php: "nightly"
env: PHPCS_VERSION="n/a" LINT=1

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.'

# On stable PHPCS versions, allow for PHP deprecation notices.
# Unit tests don't need to fail on those for stable releases where those issues won't get fixed anymore.
- |
if [[ "$TRAVIS_BUILD_STAGE_NAME" != "Sniff" && "$PHPCS_VERSION" != "dev-master" && "$PHPCS_VERSION" != "n/a" ]]; then
echo 'error_reporting = E_ALL & ~E_DEPRECATED' >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini
fi
- export XMLLINT_INDENT=" "

# Set up test environment using Composer.
- |
if [[ "$TRAVIS_BUILD_STAGE_NAME" != "Sniff" ]]; then
# Remove the PHPCSDevCS dependency as it has different PHPCS requirements and would block installs.
composer remove --dev phpcsstandards/phpcsdevcs --no-update --no-scripts
fi
- |
if [[ $PHPCS_VERSION != "n/a" ]]; then
composer require --no-update --no-scripts squizlabs/php_codesniffer:${PHPCS_VERSION}
fi
- |
if [[ "$TRAVIS_BUILD_STAGE_NAME" == "Sniff" || $PHPCS_VERSION == "n/a" ]]; then
# The sniff stage doesn't run the unit tests, so no need for PHPUnit.
composer remove --dev phpunit/phpunit --no-update --no-scripts
elif [[ "$PHPCS_VERSION" < "3.1.0" ]]; then
# PHPCS < 3.1.0 is not compatible with PHPUnit 6.x.
composer require --dev phpunit/phpunit:"^4.0||^5.0" --no-update --no-scripts
elif [[ "$PHPCS_VERSION" < "3.2.3" ]]; then
# PHPCS < 3.2.3 is not compatible with PHPUnit 7.x.
composer require --dev phpunit/phpunit:"^4.0||^5.0||^6.0" --no-update --no-scripts
fi
# --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.
- if [[ "$LINT" == "1" ]]; then composer lint; fi

# Check that any sniffs available are feature complete.
# This also acts as an integration test for the feature completeness script,
# which is why it is run against various PHP versions and not in the "Sniff" stage.
- if [[ "$LINT" == "1" ]]; then composer check-complete; fi

# Run the unit tests.
- if [[ $PHPCS_VERSION != "n/a" ]]; then composer run-tests; fi
21 changes: 21 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Change Log for the PHPCSDevTools standard for PHP Codesniffer

All notable changes to this project will be documented in this file.

This projects adheres to [Keep a CHANGELOG](http://keepachangelog.com/) and uses [Semantic Versioning](http://semver.org/).


## [Unreleased]

_Nothing yet._


## 1.0.0 - 2020-02-12

Initial release containing:
* Feature completeness checking tool for PHPCS sniffs.
* A `PHPCSDebug` standard to help debugging sniffs.


[Unreleased]: https://github.com/PHPCSStandards/PHPCSDevTools/compare/1.0.0...HEAD

9 changes: 9 additions & 0 deletions PHPCSDebug/Docs/Debug/TokenListStandard.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<documentation title="Token List">
<standard>
<![CDATA[
Lists how PHPCS tokenizes code.
This sniff will not throw any warnings or errors, but is solely intended as a tool for sniff developers.
]]>
</standard>
</documentation>
131 changes: 131 additions & 0 deletions PHPCSDebug/Sniffs/Debug/TokenListSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php
/**
* PHPCSDevTools, tools for PHP_CodeSniffer sniff developers.
*
* @package PHPCSDevTools
* @copyright 2019 PHPCSDevTools Contributors
* @license https://opensource.org/licenses/LGPL-3.0 LGPL3
* @link https://github.com/PHPCSStandards/PHPCSDevTools
*/

namespace PHPCSDebug\Sniffs\Debug;

use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Files\File;

/**
* Lists how PHPCS tokenizes code.
*
* This sniff will not throw any warnings or errors, but is solely intended
* as a tool for sniff developers.
*
* @since 1.0.0
*/
class TokenListSniff implements Sniff
{

/**
* A list of tokenizers this sniff supports.
*
* @var array
*/
public $supportedTokenizers = [
'PHP',
'JS',
'CSS',
];

/**
* Default values for the token indexes accessed.
*
* This prevents issues with "undefined index" notices in case of rare tokenizer issues.
*
* @var array
*/
private $tokenDefaults = [
'type' => '?',
'code' => '?',
'content' => '',
'line' => '?',
'column' => '?',
'level' => 0,
'conditions' => [],
];

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

/**
* 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.
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$last = ($phpcsFile->numTokens - 1);

$ptrPadding = \max(3, \strlen($last));
$linePadding = \strlen($tokens[$last]['line']);

echo \PHP_EOL;
echo \str_pad('Ptr', $ptrPadding, ' ', \STR_PAD_BOTH),
' :: ', \str_pad('Ln', ($linePadding + 1), ' ', \STR_PAD_BOTH),
' :: ', \str_pad('Col', 4, ' ', \STR_PAD_BOTH),
' :: ', 'Cond',
' :: ', \str_pad('Token Type', 26), // Longest token type name is 26 chars.
' :: [len]: Content', \PHP_EOL;

echo \str_repeat('-', ($ptrPadding + $linePadding + 35 + 16 + 18)), \PHP_EOL;

foreach ($tokens as $ptr => $token) {
$token += $this->tokenDefaults;
$content = $token['content'];

if (isset($token['length']) === false) {
$token['length'] = 0;
if (isset($token['content'])) {
$token['length'] = \strlen($content);
}
}

if ($token['code'] === \T_WHITESPACE
|| (\defined('T_DOC_COMMENT_WHITESPACE')
&& $token['code'] === \T_DOC_COMMENT_WHITESPACE)
) {
if (\strpos($content, "\t") !== false) {
$content = \str_replace("\t", '\t', $content);
}
if (isset($token['orig_content'])) {
$content .= ' :: Orig: ' . \str_replace("\t", '\t', $token['orig_content']);
}
}

$conditionCount = \count($token['conditions']);

echo \str_pad($ptr, $ptrPadding, ' ', \STR_PAD_LEFT),
' :: L', \str_pad($token['line'], $linePadding, '0', \STR_PAD_LEFT),
' :: C', \str_pad($token['column'], 3, ' ', \STR_PAD_LEFT),
' :: CC', \str_pad($conditionCount, 2, ' ', \STR_PAD_LEFT),
' :: ', \str_pad($token['type'], 26), // Longest token type name is 26 chars.
' :: [', $token['length'], ']: ', $content, \PHP_EOL;
}

// Only do this once per file.
return ($phpcsFile->numTokens + 1);
}
}
3 changes: 3 additions & 0 deletions PHPCSDebug/Tests/Debug/TokenListUnitTest.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

function
Loading

0 comments on commit 0f0776e

Please sign in to comment.