From 428b50aab912182ca21675d45b4363e2168c48a7 Mon Sep 17 00:00:00 2001 From: Dariusz Ruminski Date: Thu, 28 Dec 2017 10:25:19 +0100 Subject: [PATCH] Tokenizer - remove deprecations and legacy mode --- .travis.yml | 2 +- phpunit.xml.dist | 1 - src/Runner/Runner.php | 2 - src/Tokenizer/Token.php | 118 --------------------- src/Tokenizer/Tokens.php | 111 ++----------------- tests/Test/AbstractFixerTestCase.php | 13 --- tests/Test/AbstractIntegrationTestCase.php | 13 --- tests/Test/AbstractTransformerTestCase.php | 18 ---- tests/Tokenizer/TokenTest.php | 43 -------- 9 files changed, 11 insertions(+), 310 deletions(-) diff --git a/.travis.yml b/.travis.yml index f8efa1b8a81..17c399155b9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -69,7 +69,7 @@ jobs: <<: *STANDARD_TEST_JOB stage: Test php: 7.1 - env: SYMFONY_DEPRECATIONS_HELPER=weak PHP_CS_FIXER_TEST_USE_LEGACY_TOKENIZER=1 SYMFONY_VERSION="^4.0" MIN_STABILITY=dev + env: SYMFONY_DEPRECATIONS_HELPER=weak SYMFONY_VERSION="^4.0" MIN_STABILITY=dev - <<: *STANDARD_TEST_JOB diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 33825a625d8..d499d97e2fe 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -49,6 +49,5 @@ - diff --git a/src/Runner/Runner.php b/src/Runner/Runner.php index e83e17a156d..d5ae81c041f 100644 --- a/src/Runner/Runner.php +++ b/src/Runner/Runner.php @@ -164,8 +164,6 @@ private function fixFile(\SplFileInfo $file, LintingResultInterface $lintingResu } $old = file_get_contents($file->getRealPath()); - - Tokens::setLegacyMode(false); $tokens = Tokens::fromCode($old); $oldHash = $tokens->getCodeHash(); diff --git a/src/Tokenizer/Token.php b/src/Tokenizer/Token.php index 59d166c4436..e3178d1125e 100644 --- a/src/Tokenizer/Token.php +++ b/src/Tokenizer/Token.php @@ -114,36 +114,6 @@ public static function getClassyTokenKinds() return $classTokens; } - /** - * Clear token at given index. - * - * Clearing means override token by empty string. - * - * @deprecated since 2.4 - */ - public function clear() - { - @trigger_error(__METHOD__.' is deprecated and will be removed in 3.0.', E_USER_DEPRECATED); - Tokens::setLegacyMode(true); - - $this->content = ''; - $this->id = null; - $this->isArray = false; - } - - /** - * Clear internal flag if token was changed. - * - * @deprecated since 2.4 - */ - public function clearChanged() - { - @trigger_error(__METHOD__.' is deprecated and will be removed in 3.0.', E_USER_DEPRECATED); - Tokens::setLegacyMode(true); - - $this->changed = false; - } - /** * Check if token is equals to given one. * @@ -371,20 +341,6 @@ public function isCast() return $this->isGivenKind(self::getCastTokenKinds()); } - /** - * Check if token was changed. - * - * @return bool - * - * @deprecated since 2.4 - */ - public function isChanged() - { - @trigger_error(__METHOD__.' is deprecated and will be removed in 3.0.', E_USER_DEPRECATED); - - return $this->changed; - } - /** * Check if token is one of classy tokens: T_CLASS, T_INTERFACE or T_TRAIT. * @@ -407,20 +363,6 @@ public function isComment() return $this->isGivenKind($commentTokens); } - /** - * Check if token is empty, e.g. because of clearing. - * - * @return bool - * - * @deprecated since 2.4 - */ - public function isEmpty() - { - @trigger_error(__METHOD__.' is deprecated and will be removed in 3.0.', E_USER_DEPRECATED); - - return null === $this->id && ('' === $this->content || null === $this->content); - } - /** * Check if token is one of given kind. * @@ -491,66 +433,6 @@ public function isWhitespace($whitespaces = " \t\n\r\0\x0B") return '' === trim($this->content, $whitespaces); } - /** - * Override token. - * - * If called on Token inside Tokens collection please use `Tokens::overrideAt` instead. - * - * @param array|string|Token $other token prototype - * - * @deprecated since 2.4 - */ - public function override($other) - { - @trigger_error(__METHOD__.' is deprecated and will be removed in 3.0.', E_USER_DEPRECATED); - Tokens::setLegacyMode(true); - - $prototype = $other instanceof self ? $other->getPrototype() : $other; - - if ($this->equals($prototype)) { - return; - } - - $this->changed = true; - - if (is_array($prototype)) { - $this->isArray = true; - $this->id = $prototype[0]; - $this->content = $prototype[1]; - - return; - } - - $this->isArray = false; - $this->id = null; - $this->content = $prototype; - } - - /** - * @param string $content - * - * @deprecated since 2.4 - */ - public function setContent($content) - { - @trigger_error(__METHOD__.' is deprecated and will be removed in 3.0.', E_USER_DEPRECATED); - Tokens::setLegacyMode(true); - - if ($this->content === $content) { - return; - } - - $this->changed = true; - $this->content = $content; - - // setting empty content is clearing the token - if ('' === $content) { - @trigger_error(__METHOD__.' shall not be used to clear token, use Tokens::clearAt instead.', E_USER_DEPRECATED); - $this->id = null; - $this->isArray = false; - } - } - public function toArray() { return [ diff --git a/src/Tokenizer/Tokens.php b/src/Tokenizer/Tokens.php index de4a8ea00c0..213fccbf569 100644 --- a/src/Tokenizer/Tokens.php +++ b/src/Tokenizer/Tokens.php @@ -80,13 +80,6 @@ class Tokens extends \SplFixedArray */ private $foundTokenKinds = []; - /** - * @var bool - * - * @todo remove at 3.0 - */ - private static $isLegacyMode = false; - /** * Clone tokens collection. */ @@ -97,34 +90,6 @@ public function __clone() } } - /** - * @return bool - * - * @internal - * - * @todo remove at 3.0 - */ - public static function isLegacyMode() - { - return self::$isLegacyMode; - } - - /** - * @param bool $isLegacy - * - * @internal - * - * @todo remove at 3.0 - */ - public static function setLegacyMode($isLegacy) - { - if (getenv('PHP_CS_FIXER_FUTURE_MODE') && $isLegacy) { - throw new \RuntimeException('Cannot enable `legacy mode` when using `future mode`. This check was performed as `PHP_CS_FIXER_FUTURE_MODE` env var is set.'); - } - - self::$isLegacyMode = $isLegacy; - } - /** * Clear cache - one position or all of them. * @@ -328,12 +293,6 @@ public function offsetSet($index, $newval) public function clearChanged() { $this->changed = false; - - if (self::isLegacyMode()) { - foreach ($this as $token) { - $token->clearChanged(); - } - } } /** @@ -446,7 +405,7 @@ public function findBlockEnd($type, $searchIndex, $findEnd = true) throw new \InvalidArgumentException(sprintf('Invalid param type: %s.', $type)); } - if (!self::isLegacyMode() && isset($this->blockEndCache[$searchIndex])) { + if (isset($this->blockEndCache[$searchIndex])) { return $this->blockEndCache[$searchIndex]; } @@ -519,11 +478,9 @@ public function findGivenKind($possibleKind, $start = 0, $end = null) $elements[$kind] = []; } - if (!self::isLegacyMode()) { - $possibleKinds = array_filter($possibleKinds, function ($kind) { - return $this->isTokenKindFound($kind); - }); - } + $possibleKinds = array_filter($possibleKinds, function ($kind) { + return $this->isTokenKindFound($kind); + }); if (count($possibleKinds)) { for ($i = $start; $i < $end; ++$i) { @@ -676,11 +633,9 @@ public function getPrevTokenOfKind($index, array $tokens = [], $caseSensitive = */ public function getTokenOfKindSibling($index, $direction, array $tokens = [], $caseSensitive = true) { - if (!self::isLegacyMode()) { - $tokens = array_filter($tokens, function ($token) { - return $this->isTokenKindFound($this->extractTokenKind($token)); - }); - } + $tokens = array_filter($tokens, function ($token) { + return $this->isTokenKindFound($this->extractTokenKind($token)); + }); if (!count($tokens)) { return null; @@ -838,11 +793,9 @@ public function findSequence(array $sequence, $start = 0, $end = null, $caseSens } } - if (!self::isLegacyMode()) { - foreach ($sequence as $token) { - if (!$this->isTokenKindFound($this->extractTokenKind($token))) { - return null; - } + foreach ($sequence as $token) { + if (!$this->isTokenKindFound($this->extractTokenKind($token))) { + return null; } } @@ -938,14 +891,6 @@ public function isChanged() return true; } - if (self::isLegacyMode()) { - foreach ($this as $token) { - if ($token->isChanged()) { - return true; - } - } - } - return false; } @@ -966,23 +911,6 @@ public function clearAt($index) $this[$index] = new Token(''); } - /** - * Override token at given index and register it. - * - * @param int $index - * @param array|string|Token $token token prototype - * - * @deprecated since 2.4, use offsetSet instead - */ - public function overrideAt($index, $token) - { - @trigger_error(__METHOD__.' is deprecated and will be removed in 3.0, use offsetSet instead.', E_USER_DEPRECATED); - self::$isLegacyMode = true; - - $this[$index]->override($token); - $this->registerFoundToken($token); - } - /** * Override tokens at given range. * @@ -1165,10 +1093,6 @@ public function isTokenKindFound($tokenKind) */ public function countTokenKind($tokenKind) { - if (self::isLegacyMode()) { - throw new \RuntimeException(sprintf('%s is not available in legacy mode.', __METHOD__)); - } - return isset($this->foundTokenKinds[$tokenKind]) ? $this->foundTokenKinds[$tokenKind] : 0; } @@ -1201,21 +1125,6 @@ public function isMonolithicPhp() return false; } - if (self::isLegacyMode()) { - // If code is not monolithic there is a great chance that first or last token is `T_INLINE_HTML`: - if ($this[0]->isGivenKind(T_INLINE_HTML) || $this[$size - 1]->isGivenKind(T_INLINE_HTML)) { - return false; - } - - for ($index = 1; $index < $size; ++$index) { - if ($this[$index]->isGivenKind([T_INLINE_HTML, T_OPEN_TAG, T_OPEN_TAG_WITH_ECHO])) { - return false; - } - } - - return true; - } - if ($this->isTokenKindFound(T_INLINE_HTML)) { return false; } diff --git a/tests/Test/AbstractFixerTestCase.php b/tests/Test/AbstractFixerTestCase.php index 2a741b474e2..f1ac16d8118 100644 --- a/tests/Test/AbstractFixerTestCase.php +++ b/tests/Test/AbstractFixerTestCase.php @@ -55,19 +55,6 @@ protected function setUp() $this->linter = $this->getLinter(); $this->fixer = $this->createFixer(); - - // @todo remove at 3.0 together with env var itself - if (getenv('PHP_CS_FIXER_TEST_USE_LEGACY_TOKENIZER')) { - Tokens::setLegacyMode(true); - } - } - - protected function tearDown() - { - parent::tearDown(); - - // @todo remove at 3.0 - Tokens::setLegacyMode(false); } /** diff --git a/tests/Test/AbstractIntegrationTestCase.php b/tests/Test/AbstractIntegrationTestCase.php index c2d2e8d036f..ab2e7134057 100644 --- a/tests/Test/AbstractIntegrationTestCase.php +++ b/tests/Test/AbstractIntegrationTestCase.php @@ -105,19 +105,6 @@ protected function setUp() parent::setUp(); $this->linter = $this->getLinter(); - - // @todo remove at 3.0 together with env var itself - if (getenv('PHP_CS_FIXER_TEST_USE_LEGACY_TOKENIZER')) { - Tokens::setLegacyMode(true); - } - } - - protected function tearDown() - { - parent::tearDown(); - - // @todo remove at 3.0 - Tokens::setLegacyMode(false); } /** diff --git a/tests/Test/AbstractTransformerTestCase.php b/tests/Test/AbstractTransformerTestCase.php index c3c018dfdb0..f2833d410c2 100644 --- a/tests/Test/AbstractTransformerTestCase.php +++ b/tests/Test/AbstractTransformerTestCase.php @@ -23,24 +23,6 @@ */ abstract class AbstractTransformerTestCase extends TestCase { - protected function setUp() - { - parent::setUp(); - - // @todo remove at 3.0 together with env var itself - if (getenv('PHP_CS_FIXER_TEST_USE_LEGACY_TOKENIZER')) { - Tokens::setLegacyMode(true); - } - } - - protected function tearDown() - { - parent::tearDown(); - - // @todo remove at 3.0 - Tokens::setLegacyMode(false); - } - protected function doTest($source, array $expectedTokens = [], array $observedKindsOrPrototypes = []) { $tokens = Tokens::fromCode($source); diff --git a/tests/Tokenizer/TokenTest.php b/tests/Tokenizer/TokenTest.php index b9b3a633f84..8fee67ae43f 100644 --- a/tests/Tokenizer/TokenTest.php +++ b/tests/Tokenizer/TokenTest.php @@ -14,7 +14,6 @@ use PhpCsFixer\Tokenizer\CT; use PhpCsFixer\Tokenizer\Token; -use PhpCsFixer\Tokenizer\Tokens; use PHPUnit\Framework\TestCase; /** @@ -76,22 +75,6 @@ public function provideConstructorValidationCases() ]; } - /** - * @group legacy - * @expectedDeprecation PhpCsFixer\Tokenizer\Token::clear is deprecated and will be removed in 3.0. - */ - public function testClear() - { - $token = $this->getForeachToken(); - $token->clear(); - - Tokens::setLegacyMode(false); - - $this->assertSame('', $token->getContent()); - $this->assertNull($token->getId()); - $this->assertFalse($token->isArray()); - } - public function testGetPrototype() { $this->assertSame($this->getBraceTokenPrototype(), $this->getBraceToken()->getPrototype()); @@ -175,22 +158,6 @@ public function provideIsCommentCases() ]; } - /** - * @group legacy - * @expectedDeprecation PhpCsFixer\Tokenizer\Token::isEmpty is deprecated and will be removed in 3.0. - */ - public function testIsEmpty() - { - $braceToken = $this->getBraceToken(); - $this->assertFalse($braceToken->isEmpty()); - - $emptyToken = new Token(''); - $this->assertTrue($emptyToken->isEmpty()); - - $whitespaceToken = new Token([T_WHITESPACE, ' ']); - $this->assertFalse($whitespaceToken->isEmpty()); - } - public function testIsGivenKind() { $braceToken = $this->getBraceToken(); @@ -471,16 +438,6 @@ public function provideIsKeyCaseSensitiveCases() ]; } - /** - * @group legacy - * @expectedDeprecation PhpCsFixer\Tokenizer\Token::isChanged is deprecated and will be removed in 3.0. - */ - public function testIsChanged() - { - $token = new Token([T_WHITESPACE, ' ']); - $this->assertFalse($token->isChanged()); - } - /** * @param null|string $expected * @param int $id