Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,13 @@ jobs:
experimental: true

- php: '7.4'
phpcs_version: '4.0.x-dev'
phpcs_version: '4.x-dev'
risky: false
experimental: true

# Run risky tests separately.
- php: '7.4'
phpcs_version: '4.0.x-dev'
phpcs_version: '4.x-dev'
risky: true
experimental: true

Expand Down Expand Up @@ -151,7 +151,7 @@ jobs:
- name: Setup ini config
id: set_ini
run: |
if [[ "${{ matrix.phpcs_version }}" != "dev-master" && "${{ matrix.phpcs_version }}" != "4.0.x-dev" ]]; then
if [[ "${{ matrix.phpcs_version }}" != "dev-master" && "${{ matrix.phpcs_version }}" != "4.x-dev" ]]; then
echo 'PHP_INI=error_reporting=E_ALL & ~E_DEPRECATED, display_errors=On' >> "$GITHUB_OUTPUT"
else
echo 'PHP_INI=error_reporting=-1, display_errors=On' >> "$GITHUB_OUTPUT"
Expand Down Expand Up @@ -214,7 +214,7 @@ jobs:
if: ${{ matrix.risky == false }}
run: vendor/bin/phpunit -c ${{ steps.phpunit_config.outputs.FILE }} --no-coverage
env:
PHPCS_VERSION: ${{ matrix.phpcs_version == '4.0.x-dev' && '4.0.0' || matrix.phpcs_version }}
PHPCS_VERSION: ${{ matrix.phpcs_version == '4.x-dev' && '4.0.0' || matrix.phpcs_version }}
PHPCSUTILS_USE_CACHE: false

- name: Run the unit tests with caching (non-risky)
Expand All @@ -223,7 +223,7 @@ jobs:
vendor/bin/phpunit -c ${{ steps.phpunit_config.outputs.FILE }}
--testsuite PHPCSUtils --no-coverage ${{ steps.phpunit_config.outputs.EXTRA_ARGS }}
env:
PHPCS_VERSION: ${{ matrix.phpcs_version == '4.0.x-dev' && '4.0.0' || matrix.phpcs_version }}
PHPCS_VERSION: ${{ matrix.phpcs_version == '4.x-dev' && '4.0.0' || matrix.phpcs_version }}
PHPCSUTILS_USE_CACHE: true

# Only run the "compare with PHPCS" group against dev-master as it ensures that PHPCSUtils
Expand All @@ -233,15 +233,15 @@ jobs:
# "nothing" is excluded to force PHPUnit to ignore the <exclude> settings in phpunit.xml.dist.
run: vendor/bin/phpunit -c ${{ steps.phpunit_config.outputs.FILE }} --no-coverage --group compareWithPHPCS --exclude-group nothing
env:
PHPCS_VERSION: ${{ matrix.phpcs_version == '4.0.x-dev' && '4.0.0' || matrix.phpcs_version }}
PHPCS_VERSION: ${{ matrix.phpcs_version == '4.x-dev' && '4.0.0' || matrix.phpcs_version }}

# Run the "xtra" group against high and low PHPCS as these are tests safeguarding PHPCS itself.
- name: Run the unit tests (risky, xtra)
if: ${{ matrix.risky }}
# "nothing" is excluded to force PHPUnit to ignore the <exclude> settings in phpunit.xml.dist.
run: vendor/bin/phpunit -c ${{ steps.phpunit_config.outputs.FILE }} --no-coverage --group xtra --exclude-group nothing
env:
PHPCS_VERSION: ${{ matrix.phpcs_version == '4.0.x-dev' && '4.0.0' || matrix.phpcs_version }}
PHPCS_VERSION: ${{ matrix.phpcs_version == '4.x-dev' && '4.0.0' || matrix.phpcs_version }}


#### CODE COVERAGE STAGE ####
Expand Down
83 changes: 80 additions & 3 deletions PHPCSUtils/BackCompat/BCTokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@
* @since 1.0.0
*
* @method static array<int|string, int|string> arithmeticTokens() Tokens that represent arithmetic operators.
* @method static array<int|string, int|string> assignmentTokens() Tokens that represent assignments.
* @method static array<int|string, int|string> blockOpeners() Tokens that open code blocks.
* @method static array<int|string, int|string> booleanOperators() Tokens that perform boolean operations.
* @method static array<int|string, int|string> bracketTokens() Tokens that represent brackets and parenthesis.
* @method static array<int|string, int|string> castTokens() Tokens that represent type casting.
Expand All @@ -65,7 +63,6 @@
* @method static array<int|string, int|string> phpcsCommentTokens() Tokens that are comments containing PHPCS
* instructions.
* @method static array<int|string, int|string> scopeModifiers() Tokens that represent scope modifiers.
* @method static array<int|string, int|string> scopeOpeners() Tokens that are allowed to open scopes.
* @method static array<int|string, int|string> stringTokens() Tokens that represent strings.
* Note that `T_STRING`s are NOT represented in this
* list as this list is about _text_ strings.
Expand Down Expand Up @@ -98,6 +95,56 @@ public static function __callStatic($name, $args)
throw InvalidTokenArray::create($name);
}

/**
* Tokens that represent assignments.
*
* Retrieve the PHPCS assignments tokens array in a cross-version compatible manner.
*
* Changelog for the PHPCS native array:
* - PHPCS 4.0.0: The JS specific `T_ZSR_EQUAL` token is no longer available and has been removed from the array.
*
* @see \PHP_CodeSniffer\Util\Tokens::$assignmentTokens Original array.
*
* @since 1.0.0
*
* @return array<int|string, int|string> Token array.
*/
public static function assignmentTokens()
{
$tokens = Tokens::$assignmentTokens;

if (\defined('T_ZSR_EQUAL') && isset($tokens[\T_ZSR_EQUAL])) {
unset($tokens[\T_ZSR_EQUAL]);
}

return $tokens;
}

/**
* Tokens that open code blocks.
*
* Retrieve the PHPCS block opener tokens array in a cross-version compatible manner.
*
* Changelog for the PHPCS native array:
* - PHPCS 4.0.0: The JS specific `T_OBJECT` token is no longer available and has been removed from the array.
*
* @see \PHP_CodeSniffer\Util\Tokens::$blockOpeners Original array.
*
* @since 1.0.0
*
* @return array<int|string, int|string> Token array.
*/
public static function blockOpeners()
{
$tokens = Tokens::$blockOpeners;

if (\defined('T_OBJECT') && isset($tokens[\T_OBJECT])) {
unset($tokens[\T_OBJECT]);
}

return $tokens;
}

/**
* Tokens that represent the names of called functions.
*
Expand All @@ -120,4 +167,34 @@ public static function functionNameTokens()

return $tokens;
}

/**
* Tokens that are allowed to open scopes.
*
* Retrieve the PHPCS scope opener tokens array in a cross-version compatible manner.
*
* Changelog for the PHPCS native array:
* - PHPCS 4.0.0: The JS specific `T_PROPERTY` and `T_OBJECT` tokens are no longer available
* and have been removed from the array.
*
* @see \PHP_CodeSniffer\Util\Tokens::$scopeOpeners Original array.
*
* @since 1.0.0
*
* @return array<int|string, int|string> Token array.
*/
public static function scopeOpeners()
{
$tokens = Tokens::$scopeOpeners;

if (\defined('T_PROPERTY') && isset($tokens[\T_PROPERTY])) {
unset($tokens[\T_PROPERTY]);
}

if (\defined('T_OBJECT') && isset($tokens[\T_OBJECT])) {
unset($tokens[\T_OBJECT]);
}

return $tokens;
}
}
2 changes: 0 additions & 2 deletions Tests/BackCompat/BCFile/GetConditionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,6 @@ class GetConditionTest extends UtilityMethodTestCase
'T_TRY' => false,
'T_CATCH' => false,
'T_FINALLY' => false,
'T_PROPERTY' => false,
'T_OBJECT' => false,
'T_USE' => false,
];

Expand Down
86 changes: 86 additions & 0 deletions Tests/BackCompat/BCTokens/AssignmentTokensTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php
/**
* PHPCSUtils, utility functions and classes for PHP_CodeSniffer sniff developers.
*
* @package PHPCSUtils
* @copyright 2019-2020 PHPCSUtils Contributors
* @license https://opensource.org/licenses/LGPL-3.0 LGPL3
* @link https://github.com/PHPCSStandards/PHPCSUtils
*/

namespace PHPCSUtils\Tests\BackCompat\BCTokens;

use PHP_CodeSniffer\Util\Tokens;
use PHPCSUtils\BackCompat\BCTokens;
use PHPCSUtils\BackCompat\Helper;
use PHPUnit\Framework\TestCase;

/**
* Test class.
*
* @covers \PHPCSUtils\BackCompat\BCTokens::assignmentTokens
*
* @group tokens
*
* @since 1.1.0
*/
final class AssignmentTokensTest extends TestCase
{

/**
* Test the method.
*
* @return void
*/
public function testAssignmentTokens()
{
$expected = [
\T_EQUAL => \T_EQUAL,
\T_AND_EQUAL => \T_AND_EQUAL,
\T_OR_EQUAL => \T_OR_EQUAL,
\T_CONCAT_EQUAL => \T_CONCAT_EQUAL,
\T_DIV_EQUAL => \T_DIV_EQUAL,
\T_MINUS_EQUAL => \T_MINUS_EQUAL,
\T_POW_EQUAL => \T_POW_EQUAL,
\T_MOD_EQUAL => \T_MOD_EQUAL,
\T_MUL_EQUAL => \T_MUL_EQUAL,
\T_PLUS_EQUAL => \T_PLUS_EQUAL,
\T_XOR_EQUAL => \T_XOR_EQUAL,
\T_DOUBLE_ARROW => \T_DOUBLE_ARROW,
\T_SL_EQUAL => \T_SL_EQUAL,
\T_SR_EQUAL => \T_SR_EQUAL,
\T_COALESCE_EQUAL => \T_COALESCE_EQUAL,
];

$this->assertSame($expected, BCTokens::assignmentTokens());
}

/**
* Test whether the method in BCTokens is still in sync with the latest version of PHPCS.
*
* This group is not run by default and has to be specifically requested to be run.
*
* @group compareWithPHPCS
*
* @return void
*/
public function testPHPCSAssignmentTokens()
{
$version = Helper::getVersion();

if (\version_compare($version, '3.99.99', '>') === true) {
$this->assertSame(Tokens::$assignmentTokens, BCTokens::assignmentTokens());
} else {
/*
* Don't fail this test on the difference between PHPCS 4.x and 3.x.
* This test is only run against `dev-master` and `dev-master` is still PHPCS 3.x.
*/
$expected = Tokens::$assignmentTokens;
unset($expected[\T_ZSR_EQUAL]);

$result = BCTokens::assignmentTokens();

$this->assertSame($expected, $result);
}
}
}
74 changes: 74 additions & 0 deletions Tests/BackCompat/BCTokens/BlockOpenersTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php
/**
* PHPCSUtils, utility functions and classes for PHP_CodeSniffer sniff developers.
*
* @package PHPCSUtils
* @copyright 2019-2020 PHPCSUtils Contributors
* @license https://opensource.org/licenses/LGPL-3.0 LGPL3
* @link https://github.com/PHPCSStandards/PHPCSUtils
*/

namespace PHPCSUtils\Tests\BackCompat\BCTokens;

use PHP_CodeSniffer\Util\Tokens;
use PHPCSUtils\BackCompat\BCTokens;
use PHPCSUtils\BackCompat\Helper;
use PHPUnit\Framework\TestCase;

/**
* Test class.
*
* @covers \PHPCSUtils\BackCompat\BCTokens::blockOpeners
*
* @group tokens
*
* @since 1.1.0
*/
final class BlockOpenersTest extends TestCase
{

/**
* Test the method.
*
* @return void
*/
public function testBlockOpeners()
{
$expected = [
\T_OPEN_CURLY_BRACKET => \T_OPEN_CURLY_BRACKET,
\T_OPEN_SQUARE_BRACKET => \T_OPEN_SQUARE_BRACKET,
\T_OPEN_PARENTHESIS => \T_OPEN_PARENTHESIS,
];

$this->assertSame($expected, BCTokens::blockOpeners());
}

/**
* Test whether the method in BCTokens is still in sync with the latest version of PHPCS.
*
* This group is not run by default and has to be specifically requested to be run.
*
* @group compareWithPHPCS
*
* @return void
*/
public function testPHPCSBlockOpeners()
{
$version = Helper::getVersion();

if (\version_compare($version, '3.99.99', '>') === true) {
$this->assertSame(Tokens::$blockOpeners, BCTokens::blockOpeners());
} else {
/*
* Don't fail this test on the difference between PHPCS 4.x and 3.x.
* This test is only run against `dev-master` and `dev-master` is still PHPCS 3.x.
*/
$expected = Tokens::$blockOpeners;
unset($expected[\T_OBJECT]);

$result = BCTokens::blockOpeners();

$this->assertSame($expected, $result);
}
}
}
Loading