diff --git a/.composer-require-checker.json b/.composer-require-checker.json index ad1c755a393..5de9a2a6347 100644 --- a/.composer-require-checker.json +++ b/.composer-require-checker.json @@ -1,6 +1,7 @@ { "symbol-whitelist" : [ "LegacyPHPUnit\\TestCase", + "PhpCsFixer\\AccessibleObject\\AccessibleObject", "PhpCsFixer\\PhpunitConstraintIsIdenticalString\\Constraint\\IsIdenticalString", "PhpCsFixer\\Tests\\Test\\Constraint\\SameStringsConstraint", "PhpCsFixer\\Tests\\Test\\IsIdenticalConstraint", diff --git a/.github/workflows/sca.yml b/.github/workflows/sca.yml index 5c75d0db317..df9120ed024 100644 --- a/.github/workflows/sca.yml +++ b/.github/workflows/sca.yml @@ -114,5 +114,6 @@ jobs: grep -v tests/Test/IntegrationCaseFactoryInterface.php | grep -v tests/Test/InternalIntegrationCaseFactory.php | grep -v tests/Test/IsIdenticalConstraint.php | + grep -v tests/Test/TokensWithObservedTransformers.php | grep -v tests/TestCase.php \ && (echo "UNKNOWN FILES DETECTED" && exit 1) || echo "NO UNKNOWN FILES" diff --git a/composer.json b/composer.json index a6aaa91df46..971b425b629 100644 --- a/composer.json +++ b/composer.json @@ -71,6 +71,7 @@ "tests/Test/IntegrationCaseFactoryInterface.php", "tests/Test/InternalIntegrationCaseFactory.php", "tests/Test/IsIdenticalConstraint.php", + "tests/Test/TokensWithObservedTransformers.php", "tests/TestCase.php" ] }, diff --git a/src/Fixer/ControlStructure/NoUnneededControlParenthesesFixer.php b/src/Fixer/ControlStructure/NoUnneededControlParenthesesFixer.php index cb932ee904c..32a1e705a4f 100644 --- a/src/Fixer/ControlStructure/NoUnneededControlParenthesesFixer.php +++ b/src/Fixer/ControlStructure/NoUnneededControlParenthesesFixer.php @@ -46,11 +46,12 @@ public function __construct() { parent::__construct(); - // To be moved back to compile time property declaration when PHP support of PHP CS Fixer will be 7.0+ + // @TODO: To be moved back to compile time property declaration when PHP support of PHP CS Fixer will be 7.0+ if (\defined('T_COALESCE')) { self::$loops['clone']['forbiddenContents'][] = [T_COALESCE, '??']; } + // @TODO: To be moved back to compile time property declaration when PHP support of PHP CS Fixer will be 7.0+ if (\defined('T_YIELD_FROM')) { self::$loops['yield_from'] = ['lookupTokens' => T_YIELD_FROM, 'neededSuccessors' => [';', ')']]; } diff --git a/src/Fixer/ControlStructure/YodaStyleFixer.php b/src/Fixer/ControlStructure/YodaStyleFixer.php index f58bc2703d0..76ef0bc151a 100644 --- a/src/Fixer/ControlStructure/YodaStyleFixer.php +++ b/src/Fixer/ControlStructure/YodaStyleFixer.php @@ -485,10 +485,12 @@ private function isOfLowerPrecedence(Token $token) T_XOR_EQUAL, // ^= ]; + // @TODO: drop condition when PHP 7.0+ is required if (\defined('T_COALESCE')) { $tokens[] = T_COALESCE; // ?? } + // @TODO: drop condition when PHP 7.4+ is required if (\defined('T_COALESCE_EQUAL')) { $tokens[] = T_COALESCE_EQUAL; // ??= } diff --git a/src/Fixer/LanguageConstruct/SingleSpaceAfterConstructFixer.php b/src/Fixer/LanguageConstruct/SingleSpaceAfterConstructFixer.php index 8c432d51dac..2f807aa3399 100644 --- a/src/Fixer/LanguageConstruct/SingleSpaceAfterConstructFixer.php +++ b/src/Fixer/LanguageConstruct/SingleSpaceAfterConstructFixer.php @@ -105,10 +105,12 @@ public function configure(array $configuration = null) { parent::configure($configuration); + // @TODO: drop condition when PHP 7.0+ is required if (\defined('T_YIELD_FROM')) { self::$tokenMap['yield_from'] = T_YIELD_FROM; } + // @TODO: drop condition when PHP 8.0+ is required if (\defined('T_MATCH')) { self::$tokenMap['match'] = T_MATCH; } diff --git a/src/Fixer/Operator/BinaryOperatorSpacesFixer.php b/src/Fixer/Operator/BinaryOperatorSpacesFixer.php index 1b9f7d42f45..520d6b29dcf 100644 --- a/src/Fixer/Operator/BinaryOperatorSpacesFixer.php +++ b/src/Fixer/Operator/BinaryOperatorSpacesFixer.php @@ -457,14 +457,17 @@ private function resolveOperatorsFromConfig() } } + // @TODO: drop condition when PHP 7.0+ is required if (!\defined('T_SPACESHIP')) { unset($operators['<=>']); } + // @TODO: drop condition when PHP 7.0+ is required if (!\defined('T_COALESCE')) { unset($operators['??']); } + // @TODO: drop condition when PHP 7.4+ is required if (!\defined('T_COALESCE_EQUAL')) { unset($operators['??=']); } diff --git a/src/Fixer/Operator/NewWithBracesFixer.php b/src/Fixer/Operator/NewWithBracesFixer.php index c3ef6177f56..506a2e1e366 100644 --- a/src/Fixer/Operator/NewWithBracesFixer.php +++ b/src/Fixer/Operator/NewWithBracesFixer.php @@ -95,6 +95,7 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens) [CT::T_BRACE_CLASS_INSTANTIATION_CLOSE], ]; + // @TODO: drop condition when PHP 7.0+ is required if (\defined('T_SPACESHIP')) { $nextTokenKinds[] = [T_SPACESHIP]; } diff --git a/src/Fixer/Whitespace/BlankLineBeforeStatementFixer.php b/src/Fixer/Whitespace/BlankLineBeforeStatementFixer.php index 8fbea62fcc0..ca9c2bdc97a 100644 --- a/src/Fixer/Whitespace/BlankLineBeforeStatementFixer.php +++ b/src/Fixer/Whitespace/BlankLineBeforeStatementFixer.php @@ -71,7 +71,7 @@ public function __construct() { parent::__construct(); - // To be moved back to compile time property declaration when PHP support of PHP CS Fixer will be 7.0+ + // @TODO: To be moved back to compile time property declaration when PHP support of PHP CS Fixer will be 7.0+ if (\defined('T_YIELD_FROM')) { self::$tokenMap['yield_from'] = T_YIELD_FROM; } diff --git a/src/Linter/TokenizerLinter.php b/src/Linter/TokenizerLinter.php index 24bf3324f57..28d2ff2deed 100644 --- a/src/Linter/TokenizerLinter.php +++ b/src/Linter/TokenizerLinter.php @@ -27,7 +27,12 @@ final class TokenizerLinter implements LinterInterface { public function __construct() { - if (false === \defined('TOKEN_PARSE') || false === class_exists(\CompileError::class)) { + if ( + // @TODO: drop condition when PHP 7.0+ is required + false === \defined('TOKEN_PARSE') + // @TODO: drop condition when PHP 7.3+ is required + || false === class_exists(\CompileError::class) + ) { throw new UnavailableLinterException('Cannot use tokenizer as linter.'); } } diff --git a/src/Tokenizer/AbstractTransformer.php b/src/Tokenizer/AbstractTransformer.php index 1ec41650171..5294bcf9e49 100644 --- a/src/Tokenizer/AbstractTransformer.php +++ b/src/Tokenizer/AbstractTransformer.php @@ -43,15 +43,5 @@ public function getPriority() /** * {@inheritdoc} */ - public function getCustomTokens() - { - @trigger_error(sprintf('%s is deprecated and will be removed in 3.0.', __METHOD__), E_USER_DEPRECATED); - - return $this->getDeprecatedCustomTokens(); - } - - /** - * @return int[] - */ - abstract protected function getDeprecatedCustomTokens(); + abstract public function getCustomTokens(); } diff --git a/src/Tokenizer/Analyzer/CommentsAnalyzer.php b/src/Tokenizer/Analyzer/CommentsAnalyzer.php index 58d29bf06f4..8bef550d506 100644 --- a/src/Tokenizer/Analyzer/CommentsAnalyzer.php +++ b/src/Tokenizer/Analyzer/CommentsAnalyzer.php @@ -85,6 +85,7 @@ public function isBeforeStructuralElement(Tokens $tokens, $index) do { $nextIndex = $tokens->getNextMeaningfulToken($nextIndex); + // @TODO: drop condition when PHP 8.0+ is required if (\defined('T_ATTRIBUTE')) { while (null !== $nextIndex && $tokens[$nextIndex]->isGivenKind(T_ATTRIBUTE)) { $nextIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_ATTRIBUTE, $nextIndex); diff --git a/src/Tokenizer/Tokens.php b/src/Tokenizer/Tokens.php index 5d640558b1e..bb8689348ff 100644 --- a/src/Tokenizer/Tokens.php +++ b/src/Tokenizer/Tokens.php @@ -276,6 +276,7 @@ public static function getBlockEdgeDefinitions() ], ]; + // @TODO: drop condition when PHP 8.0+ is required if (\defined('T_ATTRIBUTE')) { $definitions[self::BLOCK_TYPE_ATTRIBUTE] = [ 'start' => [T_ATTRIBUTE, '#['], @@ -1092,7 +1093,7 @@ public function setCode($code) // clear memory $this->setSize(0); - $tokens = \defined('TOKEN_PARSE') + $tokens = \defined('TOKEN_PARSE') // @TODO: drop condition when PHP 7.0+ is required ? token_get_all($code, TOKEN_PARSE) : token_get_all($code); @@ -1102,8 +1103,7 @@ public function setCode($code) $this[$index] = new Token($token); } - $transformers = Transformers::create(); - $transformers->transform($this); + $this->applyTransformers(); $this->foundTokenKinds = []; @@ -1370,6 +1370,15 @@ public function valid() return parent::valid(); } + /** + * @internal + */ + protected function applyTransformers() + { + $transformers = Transformers::create(); + $transformers->transform($this); + } + private function warnPhp8SplFixerArrayChange($method) { if (80000 <= \PHP_VERSION_ID) { diff --git a/src/Tokenizer/TokensAnalyzer.php b/src/Tokenizer/TokensAnalyzer.php index 6a64916a812..df45d40ad22 100644 --- a/src/Tokenizer/TokensAnalyzer.php +++ b/src/Tokenizer/TokensAnalyzer.php @@ -560,14 +560,17 @@ public function isBinaryOperator($index) CT::T_TYPE_ALTERNATION => true, // | ]; + // @TODO: drop condition when PHP 7.0+ is required if (\defined('T_SPACESHIP')) { $arrayOperators[T_SPACESHIP] = true; // <=> } + // @TODO: drop condition when PHP 7.0+ is required if (\defined('T_COALESCE')) { $arrayOperators[T_COALESCE] = true; // ?? } + // @TODO: drop condition when PHP 7.4+ is required if (\defined('T_COALESCE_EQUAL')) { $arrayOperators[T_COALESCE_EQUAL] = true; // ??= } diff --git a/src/Tokenizer/Transformer/ArrayTypehintTransformer.php b/src/Tokenizer/Transformer/ArrayTypehintTransformer.php index 24306cf1759..c6b0dbaef21 100644 --- a/src/Tokenizer/Transformer/ArrayTypehintTransformer.php +++ b/src/Tokenizer/Transformer/ArrayTypehintTransformer.php @@ -54,7 +54,7 @@ public function process(Tokens $tokens, Token $token, $index) /** * {@inheritdoc} */ - protected function getDeprecatedCustomTokens() + public function getCustomTokens() { return [CT::T_ARRAY_TYPEHINT]; } diff --git a/src/Tokenizer/Transformer/AttributeTransformer.php b/src/Tokenizer/Transformer/AttributeTransformer.php index 14faf4ebad5..613a5c6fc82 100644 --- a/src/Tokenizer/Transformer/AttributeTransformer.php +++ b/src/Tokenizer/Transformer/AttributeTransformer.php @@ -68,7 +68,7 @@ public function process(Tokens $tokens, Token $token, $index) /** * {@inheritdoc} */ - protected function getDeprecatedCustomTokens() + public function getCustomTokens() { return [ CT::T_ATTRIBUTE_CLOSE, diff --git a/src/Tokenizer/Transformer/BraceClassInstantiationTransformer.php b/src/Tokenizer/Transformer/BraceClassInstantiationTransformer.php index b7681990eaa..781e08cf6bc 100644 --- a/src/Tokenizer/Transformer/BraceClassInstantiationTransformer.php +++ b/src/Tokenizer/Transformer/BraceClassInstantiationTransformer.php @@ -81,7 +81,7 @@ public function process(Tokens $tokens, Token $token, $index) /** * {@inheritdoc} */ - protected function getDeprecatedCustomTokens() + public function getCustomTokens() { return [CT::T_BRACE_CLASS_INSTANTIATION_OPEN, CT::T_BRACE_CLASS_INSTANTIATION_CLOSE]; } diff --git a/src/Tokenizer/Transformer/ClassConstantTransformer.php b/src/Tokenizer/Transformer/ClassConstantTransformer.php index fc21e30dbec..6903e23b028 100644 --- a/src/Tokenizer/Transformer/ClassConstantTransformer.php +++ b/src/Tokenizer/Transformer/ClassConstantTransformer.php @@ -57,7 +57,7 @@ public function process(Tokens $tokens, Token $token, $index) /** * {@inheritdoc} */ - protected function getDeprecatedCustomTokens() + public function getCustomTokens() { return [CT::T_CLASS_CONSTANT]; } diff --git a/src/Tokenizer/Transformer/ConstructorPromotionTransformer.php b/src/Tokenizer/Transformer/ConstructorPromotionTransformer.php index 21e86f642fe..1e93cb43511 100644 --- a/src/Tokenizer/Transformer/ConstructorPromotionTransformer.php +++ b/src/Tokenizer/Transformer/ConstructorPromotionTransformer.php @@ -67,7 +67,7 @@ public function process(Tokens $tokens, Token $token, $index) /** * {@inheritdoc} */ - protected function getDeprecatedCustomTokens() + public function getCustomTokens() { return [ CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PUBLIC, diff --git a/src/Tokenizer/Transformer/CurlyBraceTransformer.php b/src/Tokenizer/Transformer/CurlyBraceTransformer.php index 0db6a0ee20b..a2eb0e0de20 100644 --- a/src/Tokenizer/Transformer/CurlyBraceTransformer.php +++ b/src/Tokenizer/Transformer/CurlyBraceTransformer.php @@ -61,7 +61,7 @@ public function process(Tokens $tokens, Token $token, $index) /** * {@inheritdoc} */ - protected function getDeprecatedCustomTokens() + public function getCustomTokens() { return [ CT::T_CURLY_CLOSE, diff --git a/src/Tokenizer/Transformer/ImportTransformer.php b/src/Tokenizer/Transformer/ImportTransformer.php index 3fc37bd7488..93ff4340462 100644 --- a/src/Tokenizer/Transformer/ImportTransformer.php +++ b/src/Tokenizer/Transformer/ImportTransformer.php @@ -60,7 +60,7 @@ public function process(Tokens $tokens, Token $token, $index) /** * {@inheritdoc} */ - protected function getDeprecatedCustomTokens() + public function getCustomTokens() { return [CT::T_CONST_IMPORT, CT::T_FUNCTION_IMPORT]; } diff --git a/src/Tokenizer/Transformer/NameQualifiedTransformer.php b/src/Tokenizer/Transformer/NameQualifiedTransformer.php index 2f48c226edb..b5b02640756 100644 --- a/src/Tokenizer/Transformer/NameQualifiedTransformer.php +++ b/src/Tokenizer/Transformer/NameQualifiedTransformer.php @@ -58,7 +58,7 @@ public function process(Tokens $tokens, Token $token, $index) /** * {@inheritdoc} */ - protected function getDeprecatedCustomTokens() + public function getCustomTokens() { return []; } diff --git a/src/Tokenizer/Transformer/NamedArgumentTransformer.php b/src/Tokenizer/Transformer/NamedArgumentTransformer.php index 7ef92cf2850..cb7327aa70d 100644 --- a/src/Tokenizer/Transformer/NamedArgumentTransformer.php +++ b/src/Tokenizer/Transformer/NamedArgumentTransformer.php @@ -75,7 +75,7 @@ public function process(Tokens $tokens, Token $token, $index) /** * {@inheritdoc} */ - protected function getDeprecatedCustomTokens() + public function getCustomTokens() { return [ CT::T_NAMED_ARGUMENT_COLON, diff --git a/src/Tokenizer/Transformer/NamespaceOperatorTransformer.php b/src/Tokenizer/Transformer/NamespaceOperatorTransformer.php index cbb24fc8cb0..0a2ec194339 100644 --- a/src/Tokenizer/Transformer/NamespaceOperatorTransformer.php +++ b/src/Tokenizer/Transformer/NamespaceOperatorTransformer.php @@ -53,7 +53,7 @@ public function process(Tokens $tokens, Token $token, $index) /** * {@inheritdoc} */ - protected function getDeprecatedCustomTokens() + public function getCustomTokens() { return [CT::T_NAMESPACE_OPERATOR]; } diff --git a/src/Tokenizer/Transformer/NullableTypeTransformer.php b/src/Tokenizer/Transformer/NullableTypeTransformer.php index c444d47aa15..bfadc28fb11 100644 --- a/src/Tokenizer/Transformer/NullableTypeTransformer.php +++ b/src/Tokenizer/Transformer/NullableTypeTransformer.php @@ -75,7 +75,7 @@ public function process(Tokens $tokens, Token $token, $index) /** * {@inheritdoc} */ - protected function getDeprecatedCustomTokens() + public function getCustomTokens() { return [CT::T_NULLABLE_TYPE]; } diff --git a/src/Tokenizer/Transformer/ReturnRefTransformer.php b/src/Tokenizer/Transformer/ReturnRefTransformer.php index 9940ebd0bca..6690a15dda0 100644 --- a/src/Tokenizer/Transformer/ReturnRefTransformer.php +++ b/src/Tokenizer/Transformer/ReturnRefTransformer.php @@ -55,7 +55,7 @@ public function process(Tokens $tokens, Token $token, $index) /** * {@inheritdoc} */ - protected function getDeprecatedCustomTokens() + public function getCustomTokens() { return [CT::T_RETURN_REF]; } diff --git a/src/Tokenizer/Transformer/SquareBraceTransformer.php b/src/Tokenizer/Transformer/SquareBraceTransformer.php index ebcb962bc91..24b25d26f3b 100644 --- a/src/Tokenizer/Transformer/SquareBraceTransformer.php +++ b/src/Tokenizer/Transformer/SquareBraceTransformer.php @@ -70,7 +70,7 @@ public function process(Tokens $tokens, Token $token, $index) /** * {@inheritdoc} */ - protected function getDeprecatedCustomTokens() + public function getCustomTokens() { return [ CT::T_ARRAY_SQUARE_BRACE_OPEN, diff --git a/src/Tokenizer/Transformer/TypeAlternationTransformer.php b/src/Tokenizer/Transformer/TypeAlternationTransformer.php index 2a57f15b3da..15eb0842ccf 100644 --- a/src/Tokenizer/Transformer/TypeAlternationTransformer.php +++ b/src/Tokenizer/Transformer/TypeAlternationTransformer.php @@ -121,7 +121,7 @@ public function process(Tokens $tokens, Token $token, $index) /** * {@inheritdoc} */ - protected function getDeprecatedCustomTokens() + public function getCustomTokens() { return [CT::T_TYPE_ALTERNATION]; } diff --git a/src/Tokenizer/Transformer/TypeColonTransformer.php b/src/Tokenizer/Transformer/TypeColonTransformer.php index 52494f7162c..7d85ca31a36 100644 --- a/src/Tokenizer/Transformer/TypeColonTransformer.php +++ b/src/Tokenizer/Transformer/TypeColonTransformer.php @@ -83,7 +83,7 @@ public function process(Tokens $tokens, Token $token, $index) /** * {@inheritdoc} */ - protected function getDeprecatedCustomTokens() + public function getCustomTokens() { return [CT::T_TYPE_COLON]; } diff --git a/src/Tokenizer/Transformer/UseTransformer.php b/src/Tokenizer/Transformer/UseTransformer.php index 081d3e212c3..36e6e08825e 100644 --- a/src/Tokenizer/Transformer/UseTransformer.php +++ b/src/Tokenizer/Transformer/UseTransformer.php @@ -88,7 +88,7 @@ public function process(Tokens $tokens, Token $token, $index) /** * {@inheritdoc} */ - protected function getDeprecatedCustomTokens() + public function getCustomTokens() { return [CT::T_USE_TRAIT, CT::T_USE_LAMBDA]; } diff --git a/src/Tokenizer/Transformer/WhitespacyCommentTransformer.php b/src/Tokenizer/Transformer/WhitespacyCommentTransformer.php index 3a06ad329fa..345e593f570 100644 --- a/src/Tokenizer/Transformer/WhitespacyCommentTransformer.php +++ b/src/Tokenizer/Transformer/WhitespacyCommentTransformer.php @@ -64,7 +64,7 @@ public function process(Tokens $tokens, Token $token, $index) /** * {@inheritdoc} */ - protected function getDeprecatedCustomTokens() + public function getCustomTokens() { return []; } diff --git a/src/Tokenizer/TransformerInterface.php b/src/Tokenizer/TransformerInterface.php index 933122c2b21..ad5aa9912c1 100644 --- a/src/Tokenizer/TransformerInterface.php +++ b/src/Tokenizer/TransformerInterface.php @@ -30,8 +30,6 @@ interface TransformerInterface * Get tokens created by Transformer. * * @return int[] - * - * @deprecated will be removed in 3.0 */ public function getCustomTokens(); diff --git a/tests/Fixtures/Test/AbstractTransformerTest/FooTransformer.php b/tests/Fixtures/Test/AbstractTransformerTest/FooTransformer.php index f149578a8a3..915a6729f4a 100644 --- a/tests/Fixtures/Test/AbstractTransformerTest/FooTransformer.php +++ b/tests/Fixtures/Test/AbstractTransformerTest/FooTransformer.php @@ -38,7 +38,7 @@ public function process(Tokens $tokens, Token $token, $index) /** * {@inheritdoc} */ - protected function getDeprecatedCustomTokens() + public function getCustomTokens() { return []; } diff --git a/tests/Test/AbstractTransformerTestCase.php b/tests/Test/AbstractTransformerTestCase.php index 26776af61d2..400af44117d 100644 --- a/tests/Test/AbstractTransformerTestCase.php +++ b/tests/Test/AbstractTransformerTestCase.php @@ -14,6 +14,7 @@ use PhpCsFixer\Tests\TestCase; use PhpCsFixer\Tokenizer\CT; +use PhpCsFixer\Tokenizer\Token; use PhpCsFixer\Tokenizer\Tokens; use PhpCsFixer\Tokenizer\TransformerInterface; @@ -64,10 +65,6 @@ public function testGetName() static::assertMatchesRegularExpression('/^[a-z]+[a-z_]*[a-z]$/', $name); } - /** - * @group legacy - * @expectedDeprecation PhpCsFixer\Tokenizer\AbstractTransformer::getCustomTokens is deprecated and will be removed in 3.0. - */ public function testGetCustomTokens() { $name = $this->transformer->getName(); @@ -120,7 +117,8 @@ public function testTransformDoesNotChangeSimpleCode() protected function doTest($source, array $expectedTokens = [], array $observedKindsOrPrototypes = []) { Tokens::clearCache(); - $tokens = Tokens::fromCode($source); + $tokens = new TokensWithObservedTransformers(); + $tokens->setCode($source); static::assertSame( \count($expectedTokens), @@ -136,6 +134,35 @@ static function ($kindOrPrototype) { 'Number of expected tokens does not match actual token count.' ); + $customTokensOfTransformer = $this->transformer->getCustomTokens(); + $transformerName = $this->transformer->getName(); + + foreach ($tokens->observedModificationsPerTransformer as $appliedTransformerName => $modificationsOfTransformer) { + foreach ($modificationsOfTransformer as $modification) { + if ($appliedTransformerName === $transformerName) { + static::assertContains( + $modification, + $customTokensOfTransformer, + sprintf( + 'Transformation into "%s" must be allowed in self-documentation of the Transformer, currently allowed custom tokens are: %s', + Token::getNameForId($modification), + implode(', ', array_map(function ($ct) { return Token::getNameForId($ct); }, $customTokensOfTransformer)) + ) + ); + } else { + static::assertNotContains( + $modification, + $customTokensOfTransformer, + sprintf( + 'Transformation into "%s" must NOT be applied by other Transformer than "%s".', + Token::getNameForId($modification), + $transformerName + ) + ); + } + } + } + foreach ($expectedTokens as $index => $tokenIdOrContent) { if (\is_string($tokenIdOrContent)) { static::assertTrue($tokens[$index]->equals($tokenIdOrContent), sprintf('The token at index %d should be %s, got %s', $index, json_encode($tokenIdOrContent), $tokens[$index]->toJson())); diff --git a/tests/Test/TokensWithObservedTransformers.php b/tests/Test/TokensWithObservedTransformers.php new file mode 100644 index 00000000000..89f5645e9aa --- /dev/null +++ b/tests/Test/TokensWithObservedTransformers.php @@ -0,0 +1,68 @@ + + * Dariusz RumiƄski + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace PhpCsFixer\Tests\Test; + +use PhpCsFixer\AccessibleObject\AccessibleObject; +use PhpCsFixer\Tokenizer\Token; +use PhpCsFixer\Tokenizer\Tokens; +use PhpCsFixer\Tokenizer\Transformers; + +class TokensWithObservedTransformers extends Tokens +{ + /** + * @var null|string + */ + public $currentTransformer; + public $observedModificationsPerTransformer = []; + + public function offsetSet($index, $newval) + { + if (null !== $this->currentTransformer) { + $this->observedModificationsPerTransformer[$this->currentTransformer][] = $this->extractTokenKind($newval); + } + parent::offsetSet($index, $newval); + } + + /** + * @internal + */ + protected function applyTransformers() + { + $this->observedModificationsPerTransformer = []; + + $transformers = Transformers::create(); + foreach (AccessibleObject::create($transformers)->items as $transformer) { + $this->currentTransformer = $transformer->getName(); + $this->observedModificationsPerTransformer[$this->currentTransformer] = []; + + foreach ($this as $index => $token) { + $transformer->process($this, $token, $index); + } + } + + $this->currentTransformer = null; + } + + /** + * @param array|string|Token $token token prototype + * + * @return int|string + */ + private function extractTokenKind($token) + { + return $token instanceof Token + ? ($token->isArray() ? $token->getId() : $token->getContent()) + : (\is_array($token) ? $token[0] : $token) + ; + } +} diff --git a/tests/Tokenizer/AbstractTransformerTest.php b/tests/Tokenizer/AbstractTransformerTest.php index f0af3f7f45e..224c94b13a4 100644 --- a/tests/Tokenizer/AbstractTransformerTest.php +++ b/tests/Tokenizer/AbstractTransformerTest.php @@ -32,10 +32,6 @@ public function testNameAndPriorityDefault() static::assertSame('foo', $transformer->getName()); } - /** - * @group legacy - * @expectedDeprecation PhpCsFixer\Tokenizer\AbstractTransformer::getCustomTokens is deprecated and will be removed in 3.0. - */ public function testCustomTokens() { $transformer = new FooTransformer(); diff --git a/tests/Tokenizer/TokenTest.php b/tests/Tokenizer/TokenTest.php index 40b23a05d41..13a115918cf 100644 --- a/tests/Tokenizer/TokenTest.php +++ b/tests/Tokenizer/TokenTest.php @@ -151,6 +151,7 @@ public function provideIsCommentCases() yield $index => $test; } + // @TODO: drop condition when PHP 8.0+ is required if (\defined('T_ATTRIBUTE')) { yield [new Token([T_ATTRIBUTE, '#[', 1]), false]; }