Skip to content

Commit 0f0776e

Browse files
authored
Merge pull request #33 from PHPCSStandards/develop
Release version 1.0.0
2 parents 5d29b83 + 2bd8c36 commit 0f0776e

19 files changed

+1436
-10
lines changed

.gitattributes

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
/.gitattributes export-ignore
99
/.gitignore export-ignore
1010
/.travis.yml export-ignore
11+
/phpcs.xml.dist export-ignore
12+
/phpunit.xml.dist export-ignore
13+
/phpunit-bootstrap.php export-ignore
14+
/PHPCSDebug/Tests export-ignore
1115

1216
#
1317
# Auto detect text files and perform LF normalization

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
vendor/
22
composer.lock
3+
.phpcs.xml
4+
phpcs.xml
5+
phpunit.xml
6+
.phpunit.result.cache

.travis.yml

Lines changed: 126 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ language: php
44

55
## Cache composer and apt downloads.
66
cache:
7+
apt: true
78
directories:
89
# Cache directory for older Composer versions.
910
- $HOME/.composer/cache/files
@@ -12,21 +13,142 @@ cache:
1213

1314
php:
1415
- 5.4
15-
- 7.3
16+
- 5.5
17+
- 5.6
18+
- 7.0
19+
- 7.1
20+
- 7.2
21+
22+
env:
23+
# `master`
24+
- PHPCS_VERSION="dev-master" LINT=1
25+
# Lowest supported PHPCS version.
26+
- PHPCS_VERSION="3.0.2"
27+
28+
# Define the stages used.
29+
# For non-PRs, only the sniff and quicktest stages are run.
30+
# For pull requests and merges, the full script is run (skipping quicktest).
31+
# Note: for pull requests, "develop" is the base branch name.
32+
# See: https://docs.travis-ci.com/user/conditions-v1
33+
stages:
34+
- name: sniff
35+
- name: quicktest
36+
if: type = push AND branch NOT IN (master, develop)
37+
- name: test
38+
if: branch IN (master, develop)
1639

1740
jobs:
1841
fast_finish: true
42+
include:
43+
#### SNIFF STAGE ####
44+
- stage: sniff
45+
php: 7.3
46+
env: PHPCS_VERSION="dev-master"
47+
addons:
48+
apt:
49+
packages:
50+
- libxml2-utils
51+
script:
52+
# Check the code style of the code base.
53+
- composer check-cs
54+
55+
# Validate the xml file.
56+
# @link http://xmlsoft.org/xmllint.html
57+
- xmllint --noout --schema ./vendor/squizlabs/php_codesniffer/phpcs.xsd ./PHPCSDebug/ruleset.xml
58+
59+
# Check the code-style consistency of the xml files.
60+
- diff -B ./PHPCSDebug/ruleset.xml <(xmllint --format "./PHPCSDebug/ruleset.xml")
61+
62+
# Validate the composer.json file.
63+
# @link https://getcomposer.org/doc/03-cli.md#validate
64+
- composer validate --no-check-all --strict
65+
66+
#### QUICK TEST STAGE ####
67+
# This is a much quicker test which only runs the unit tests and linting against the low/high
68+
# supported PHP/PHPCS combinations.
69+
- stage: quicktest
70+
php: 7.3
71+
env: PHPCS_VERSION="dev-master" LINT=1
72+
- stage: quicktest
73+
php: 7.2
74+
env: PHPCS_VERSION="3.0.2"
75+
76+
- stage: quicktest
77+
php: 5.4
78+
env: PHPCS_VERSION="dev-master" LINT=1
79+
- stage: quicktest
80+
php: 5.4
81+
env: PHPCS_VERSION="3.0.2"
82+
83+
#### TEST STAGE ####
84+
# Additional builds to prevent issues with PHPCS versions incompatible with certain PHP versions.
85+
- stage: test
86+
php: 7.3
87+
env: PHPCS_VERSION="dev-master" LINT=1
88+
# PHPCS is only compatible with PHP 7.3 as of version 3.3.1.
89+
- php: 7.3
90+
env: PHPCS_VERSION="3.3.1"
91+
- php: 7.4
92+
env: PHPCS_VERSION="dev-master"
93+
# PHPCS is only compatible with PHP 7.4 as of version 3.5.0.
94+
- php: 7.4
95+
env: PHPCS_VERSION="3.5.0"
96+
- php: "nightly"
97+
env: PHPCS_VERSION="n/a" LINT=1
98+
99+
allow_failures:
100+
# Allow failures for unstable builds.
101+
- php: "nightly"
19102

20103

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

108+
# On stable PHPCS versions, allow for PHP deprecation notices.
109+
# Unit tests don't need to fail on those for stable releases where those issues won't get fixed anymore.
110+
- |
111+
if [[ "$TRAVIS_BUILD_STAGE_NAME" != "Sniff" && "$PHPCS_VERSION" != "dev-master" && "$PHPCS_VERSION" != "n/a" ]]; then
112+
echo 'error_reporting = E_ALL & ~E_DEPRECATED' >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini
113+
fi
114+
115+
- export XMLLINT_INDENT=" "
116+
117+
# Set up test environment using Composer.
118+
- |
119+
if [[ "$TRAVIS_BUILD_STAGE_NAME" != "Sniff" ]]; then
120+
# Remove the PHPCSDevCS dependency as it has different PHPCS requirements and would block installs.
121+
composer remove --dev phpcsstandards/phpcsdevcs --no-update --no-scripts
122+
fi
123+
- |
124+
if [[ $PHPCS_VERSION != "n/a" ]]; then
125+
composer require --no-update --no-scripts squizlabs/php_codesniffer:${PHPCS_VERSION}
126+
fi
127+
- |
128+
if [[ "$TRAVIS_BUILD_STAGE_NAME" == "Sniff" || $PHPCS_VERSION == "n/a" ]]; then
129+
# The sniff stage doesn't run the unit tests, so no need for PHPUnit.
130+
composer remove --dev phpunit/phpunit --no-update --no-scripts
131+
elif [[ "$PHPCS_VERSION" < "3.1.0" ]]; then
132+
# PHPCS < 3.1.0 is not compatible with PHPUnit 6.x.
133+
composer require --dev phpunit/phpunit:"^4.0||^5.0" --no-update --no-scripts
134+
elif [[ "$PHPCS_VERSION" < "3.2.3" ]]; then
135+
# PHPCS < 3.2.3 is not compatible with PHPUnit 7.x.
136+
composer require --dev phpunit/phpunit:"^4.0||^5.0||^6.0" --no-update --no-scripts
137+
fi
138+
25139
# --prefer-dist will allow for optimal use of the travis caching ability.
140+
# The Composer PHPCS plugin takes care of setting the installed_paths for PHPCS.
26141
- composer install --prefer-dist --no-suggest
27142

28143

29144
script:
30-
# Validate the composer.json file on low/high PHP versions.
31-
# @link https://getcomposer.org/doc/03-cli.md#validate
32-
- composer validate --no-check-all --strict
145+
# Lint PHP files against parse errors.
146+
- if [[ "$LINT" == "1" ]]; then composer lint; fi
147+
148+
# Check that any sniffs available are feature complete.
149+
# This also acts as an integration test for the feature completeness script,
150+
# which is why it is run against various PHP versions and not in the "Sniff" stage.
151+
- if [[ "$LINT" == "1" ]]; then composer check-complete; fi
152+
153+
# Run the unit tests.
154+
- if [[ $PHPCS_VERSION != "n/a" ]]; then composer run-tests; fi

CHANGELOG.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Change Log for the PHPCSDevTools standard for PHP Codesniffer
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
This projects adheres to [Keep a CHANGELOG](http://keepachangelog.com/) and uses [Semantic Versioning](http://semver.org/).
6+
7+
8+
## [Unreleased]
9+
10+
_Nothing yet._
11+
12+
13+
## 1.0.0 - 2020-02-12
14+
15+
Initial release containing:
16+
* Feature completeness checking tool for PHPCS sniffs.
17+
* A `PHPCSDebug` standard to help debugging sniffs.
18+
19+
20+
[Unreleased]: https://github.com/PHPCSStandards/PHPCSDevTools/compare/1.0.0...HEAD
21+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<documentation title="Token List">
2+
<standard>
3+
<![CDATA[
4+
Lists how PHPCS tokenizes code.
5+
6+
This sniff will not throw any warnings or errors, but is solely intended as a tool for sniff developers.
7+
]]>
8+
</standard>
9+
</documentation>
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
<?php
2+
/**
3+
* PHPCSDevTools, tools for PHP_CodeSniffer sniff developers.
4+
*
5+
* @package PHPCSDevTools
6+
* @copyright 2019 PHPCSDevTools Contributors
7+
* @license https://opensource.org/licenses/LGPL-3.0 LGPL3
8+
* @link https://github.com/PHPCSStandards/PHPCSDevTools
9+
*/
10+
11+
namespace PHPCSDebug\Sniffs\Debug;
12+
13+
use PHP_CodeSniffer\Sniffs\Sniff;
14+
use PHP_CodeSniffer\Files\File;
15+
16+
/**
17+
* Lists how PHPCS tokenizes code.
18+
*
19+
* This sniff will not throw any warnings or errors, but is solely intended
20+
* as a tool for sniff developers.
21+
*
22+
* @since 1.0.0
23+
*/
24+
class TokenListSniff implements Sniff
25+
{
26+
27+
/**
28+
* A list of tokenizers this sniff supports.
29+
*
30+
* @var array
31+
*/
32+
public $supportedTokenizers = [
33+
'PHP',
34+
'JS',
35+
'CSS',
36+
];
37+
38+
/**
39+
* Default values for the token indexes accessed.
40+
*
41+
* This prevents issues with "undefined index" notices in case of rare tokenizer issues.
42+
*
43+
* @var array
44+
*/
45+
private $tokenDefaults = [
46+
'type' => '?',
47+
'code' => '?',
48+
'content' => '',
49+
'line' => '?',
50+
'column' => '?',
51+
'level' => 0,
52+
'conditions' => [],
53+
];
54+
55+
/**
56+
* Returns an array of tokens this test wants to listen for.
57+
*
58+
* @return array
59+
*/
60+
public function register()
61+
{
62+
return [
63+
\T_OPEN_TAG,
64+
\T_OPEN_TAG_WITH_ECHO,
65+
];
66+
}
67+
68+
/**
69+
* Processes this test, when one of its tokens is encountered.
70+
*
71+
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
72+
* @param int $stackPtr The position of the current
73+
* token in the stack.
74+
*
75+
* @return void
76+
*/
77+
public function process(File $phpcsFile, $stackPtr)
78+
{
79+
$tokens = $phpcsFile->getTokens();
80+
$last = ($phpcsFile->numTokens - 1);
81+
82+
$ptrPadding = \max(3, \strlen($last));
83+
$linePadding = \strlen($tokens[$last]['line']);
84+
85+
echo \PHP_EOL;
86+
echo \str_pad('Ptr', $ptrPadding, ' ', \STR_PAD_BOTH),
87+
' :: ', \str_pad('Ln', ($linePadding + 1), ' ', \STR_PAD_BOTH),
88+
' :: ', \str_pad('Col', 4, ' ', \STR_PAD_BOTH),
89+
' :: ', 'Cond',
90+
' :: ', \str_pad('Token Type', 26), // Longest token type name is 26 chars.
91+
' :: [len]: Content', \PHP_EOL;
92+
93+
echo \str_repeat('-', ($ptrPadding + $linePadding + 35 + 16 + 18)), \PHP_EOL;
94+
95+
foreach ($tokens as $ptr => $token) {
96+
$token += $this->tokenDefaults;
97+
$content = $token['content'];
98+
99+
if (isset($token['length']) === false) {
100+
$token['length'] = 0;
101+
if (isset($token['content'])) {
102+
$token['length'] = \strlen($content);
103+
}
104+
}
105+
106+
if ($token['code'] === \T_WHITESPACE
107+
|| (\defined('T_DOC_COMMENT_WHITESPACE')
108+
&& $token['code'] === \T_DOC_COMMENT_WHITESPACE)
109+
) {
110+
if (\strpos($content, "\t") !== false) {
111+
$content = \str_replace("\t", '\t', $content);
112+
}
113+
if (isset($token['orig_content'])) {
114+
$content .= ' :: Orig: ' . \str_replace("\t", '\t', $token['orig_content']);
115+
}
116+
}
117+
118+
$conditionCount = \count($token['conditions']);
119+
120+
echo \str_pad($ptr, $ptrPadding, ' ', \STR_PAD_LEFT),
121+
' :: L', \str_pad($token['line'], $linePadding, '0', \STR_PAD_LEFT),
122+
' :: C', \str_pad($token['column'], 3, ' ', \STR_PAD_LEFT),
123+
' :: CC', \str_pad($conditionCount, 2, ' ', \STR_PAD_LEFT),
124+
' :: ', \str_pad($token['type'], 26), // Longest token type name is 26 chars.
125+
' :: [', $token['length'], ']: ', $content, \PHP_EOL;
126+
}
127+
128+
// Only do this once per file.
129+
return ($phpcsFile->numTokens + 1);
130+
}
131+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
function

0 commit comments

Comments
 (0)