Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 87d8acc

Browse files
committed
minor #2818 Token become immutable, performance optimizations (keradus)
This PR was squashed before being merged into the 2.4-dev branch (closes #2818). Discussion ---------- Token become immutable, performance optimizations TODO: - [x] rebase on master ``` Executing on Symfony 2.7 with 45files that reports violations before > Checked all files in 77.743 seconds, 20.000 MB memory used > Checked all files in 78.761 seconds, 20.000 MB memory used after improvement > Checked all files in 70.417 seconds, 20.000 MB memory used > Checked all files in 71.144 seconds, 20.000 MB memory used About 10% improvement ``` Deps: - [x] #2819 - [x] #2816 - [x] #2817 - [x] #2823 Commits ------- 964586f Token become immutable, performance optimizations
2 parents dca89c0 + 964586f commit 87d8acc

11 files changed

Lines changed: 229 additions & 30 deletions

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ matrix:
2323
- php: 5.6
2424
env: COMPOSER_FLAGS="--prefer-stable --prefer-lowest"
2525
- php: 7.0
26+
env: SYMFONY_DEPRECATIONS_HELPER=weak PHP_CS_FIXER_TEST_USE_LEGACY_TOKENIZER=1
2627

2728
cache:
2829
directories:

phpunit.xml.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,6 @@
4343
<php>
4444
<ini name="zend.enable_gc" value="0"/>
4545
<env name="SKIP_LINT_TEST_CASES" value="0"/>
46+
<env name="PHP_CS_FIXER_TEST_USE_LEGACY_TOKENIZER" value="0"/>
4647
</php>
4748
</phpunit>

src/Fixer/Import/NoLeadingImportSlashFixer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,12 @@ public function isCandidate(Tokens $tokens)
5656
*/
5757
protected function applyFix(\SplFileInfo $file, Tokens $tokens)
5858
{
59-
$foundNamespace = $tokens->findGivenKind(T_NAMESPACE);
60-
if (empty($foundNamespace)) {
59+
if (!$tokens->isTokenKindFound(T_NAMESPACE)) {
6160
return;
6261
}
6362

6463
$tokensAnalyzer = new TokensAnalyzer($tokens);
64+
$foundNamespace = $tokens->findGivenKind(T_NAMESPACE);
6565
$firstNamespaceIdx = key($foundNamespace);
6666

6767
$usesIdxs = $tokensAnalyzer->getImportUseIndexes();

src/Fixer/PhpTag/NoClosingTagFixer.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,11 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens)
5353
return;
5454
}
5555

56-
$closeTags = $tokens->findGivenKind(T_CLOSE_TAG);
57-
58-
if (empty($closeTags)) {
56+
if (!$tokens->isTokenKindFound(T_CLOSE_TAG)) {
5957
return;
6058
}
6159

60+
$closeTags = $tokens->findGivenKind(T_CLOSE_TAG);
6261
$index = key($closeTags);
6362

6463
$tokens->removeLeadingWhitespace($index);

src/Runner/Runner.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ private function fixFile(\SplFileInfo $file, LintingResultInterface $lintingResu
164164
}
165165

166166
$old = file_get_contents($file->getRealPath());
167+
168+
Tokens::setLegacyMode(false);
167169
$tokens = Tokens::fromCode($old);
168170
$oldHash = $tokens->getCodeHash();
169171

src/Test/AbstractFixerTestCase.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,17 @@ protected function setUp()
5151
{
5252
$this->linter = $this->getLinter();
5353
$this->fixer = $this->createFixer();
54+
55+
// @todo remove at 3.0 together with env var itself
56+
if (getenv('PHP_CS_FIXER_TEST_USE_LEGACY_TOKENIZER')) {
57+
Tokens::setLegacyMode(true);
58+
}
59+
}
60+
61+
protected function tearDown()
62+
{
63+
// @todo remove at 3.0
64+
Tokens::setLegacyMode(false);
5465
}
5566

5667
/**

src/Test/AbstractIntegrationTestCase.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,17 @@ public static function tearDownAfterClass()
9797
protected function setUp()
9898
{
9999
$this->linter = $this->getLinter();
100+
101+
// @todo remove at 3.0 together with env var itself
102+
if (getenv('PHP_CS_FIXER_TEST_USE_LEGACY_TOKENIZER')) {
103+
Tokens::setLegacyMode(true);
104+
}
105+
}
106+
107+
protected function tearDown()
108+
{
109+
// @todo remove at 3.0
110+
Tokens::setLegacyMode(false);
100111
}
101112

102113
/**

src/Test/AbstractTransformerTestCase.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,20 @@
2323
*/
2424
abstract class AbstractTransformerTestCase extends TestCase
2525
{
26+
protected function setUp()
27+
{
28+
// @todo remove at 3.0 together with env var itself
29+
if (getenv('PHP_CS_FIXER_TEST_USE_LEGACY_TOKENIZER')) {
30+
Tokens::setLegacyMode(true);
31+
}
32+
}
33+
34+
protected function tearDown()
35+
{
36+
// @todo remove at 3.0
37+
Tokens::setLegacyMode(false);
38+
}
39+
2640
protected function doTest($source, array $expectedTokens = [], array $observedKinds = [])
2741
{
2842
$tokens = Tokens::fromCode($source);

src/Tokenizer/Token.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ public function __construct($token)
5959
$this->isArray = true;
6060
$this->id = $token[0];
6161
$this->content = $token[1];
62+
63+
if ($token[0] && '' === $token[1]) {
64+
throw new \InvalidArgumentException('Cannot set empty content for id-based Token.');
65+
}
6266
} elseif (is_string($token)) {
6367
$this->isArray = false;
6468
$this->content = $token;
@@ -96,9 +100,14 @@ public static function getClassyTokenKinds()
96100
* Clear token at given index.
97101
*
98102
* Clearing means override token by empty string.
103+
*
104+
* @deprecated since 2.4
99105
*/
100106
public function clear()
101107
{
108+
@trigger_error(__METHOD__.' is deprecated and will be removed in 3.0.', E_USER_DEPRECATED);
109+
Tokens::setLegacyMode(true);
110+
102111
$this->content = '';
103112
$this->id = null;
104113
$this->isArray = false;
@@ -109,6 +118,9 @@ public function clear()
109118
*/
110119
public function clearChanged()
111120
{
121+
@trigger_error(__METHOD__.' is deprecated and will be removed in 3.0.', E_USER_DEPRECATED);
122+
Tokens::setLegacyMode(true);
123+
112124
$this->changed = false;
113125
}
114126

@@ -359,9 +371,13 @@ public function isComment()
359371
* Check if token is empty, e.g. because of clearing.
360372
*
361373
* @return bool
374+
*
375+
* @deprecated since 2.4
362376
*/
363377
public function isEmpty()
364378
{
379+
@trigger_error(__METHOD__.' is deprecated and will be removed in 3.0.', E_USER_DEPRECATED);
380+
365381
return null === $this->id && ('' === $this->content || null === $this->content);
366382
}
367383

@@ -441,9 +457,14 @@ public function isWhitespace($whitespaces = " \t\n\r\0\x0B")
441457
* If called on Token inside Tokens collection please use `Tokens::overrideAt` instead.
442458
*
443459
* @param Token|array|string $other token prototype
460+
*
461+
* @deprecated since 2.4
444462
*/
445463
public function override($other)
446464
{
465+
@trigger_error(__METHOD__.' is deprecated and will be removed in 3.0.', E_USER_DEPRECATED);
466+
Tokens::setLegacyMode(true);
467+
447468
$prototype = $other instanceof self ? $other->getPrototype() : $other;
448469

449470
if ($this->equals($prototype)) {
@@ -470,6 +491,9 @@ public function override($other)
470491
*/
471492
public function setContent($content)
472493
{
494+
@trigger_error(__METHOD__.' is deprecated and will be removed in 3.0.', E_USER_DEPRECATED);
495+
Tokens::setLegacyMode(true);
496+
473497
if ($this->content === $content) {
474498
return;
475499
}
@@ -479,6 +503,7 @@ public function setContent($content)
479503

480504
// setting empty content is clearing the token
481505
if ('' === $content) {
506+
@trigger_error(__METHOD__.' shall not be used to clear token, use Tokens::clearAt instead.', E_USER_DEPRECATED);
482507
$this->id = null;
483508
$this->isArray = false;
484509
}

0 commit comments

Comments
 (0)