Skip to content

Commit 0ddfc87

Browse files
committed
minor PHP-CS-Fixer#2306 DeclareStrictTypesFixer - do not move tokens (SpacePossum)
This PR was merged into the 2.0-dev branch. Discussion ---------- DeclareStrictTypesFixer - do not move tokens This is step one to resolve the "file opening fixing" conflict we have with the current fixers. With this PR the DeclareStrictTypesFixer will: - insert the sequence if not found, and fix the white space around the new sequence - fix the casing of 'strict_types=1' (as we have no other fixer for it, nor would it be useful ATM to split this functionality IMO) It will no longer: - move an existing sequence - fix white around an existing sequence or within such sequence - fix the code style of an existing sequence (with a small exception, see above) So with a bit of pain in my heart.. here we go ;) Commits ------- 8ed737f DeclareStrictTypesFixer - do not move tokens
2 parents b678791 + 8ed737f commit 0ddfc87

7 files changed

+47
-183
lines changed

src/Fixer/Strict/DeclareStrictTypesFixer.php

Lines changed: 43 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -44,39 +44,15 @@ public function fix(\SplFileInfo $file, Tokens $tokens)
4444
return;
4545
}
4646

47-
// check if the declaration is at the right location
48-
reset($sequenceLocation);
49-
$sequenceStartIndex = key($sequenceLocation);
50-
51-
end($sequenceLocation);
52-
$sequenceEndIndex = $tokens->getNextMeaningfulToken(key($sequenceLocation));
53-
54-
if (1 === $sequenceStartIndex) {
55-
// declaration already at the correct location, fix spacing only
56-
$this->fixWhiteSpaceAroundSequence($tokens, $sequenceEndIndex);
57-
58-
return;
59-
}
60-
61-
// Handle end of statement of the sequence; i.e. semicolon vs. close tag. Remove a semicolon as well,
62-
// but don't remove a close tag since comment placement might cause invalid code to be created.
63-
if (!$tokens[$sequenceEndIndex]->isGivenKind(T_CLOSE_TAG)) {
64-
$sequenceLocation[$sequenceEndIndex] = $tokens[$sequenceEndIndex];
65-
}
66-
67-
foreach ($sequenceLocation as $index => $token) {
68-
$tokens->clearTokenAndMergeSurroundingWhitespace($index);
69-
}
70-
71-
$this->insertSequence($tokens);
47+
$this->fixStrictTypesCasing($tokens, $sequenceLocation);
7248
}
7349

7450
/**
7551
* {@inheritdoc}
7652
*/
7753
public function getPriority()
7854
{
79-
// must ran before SingleBlankLineBeforeNamespaceFixer, NoBlankLinesBeforeNamespaceFixer, NoExtraConsecutiveBlankLinesFixer, NoWhitespaceInBlankLinesFixer
55+
// must ran before SingleBlankLineBeforeNamespaceFixer.
8056
return 1;
8157
}
8258

@@ -104,38 +80,6 @@ protected function getDescription()
10480
return 'Force strict types declaration in all files.';
10581
}
10682

107-
/**
108-
* @param Tokens $tokens
109-
* @param int $endIndex
110-
*/
111-
private function fixWhiteSpaceAroundSequence(Tokens $tokens, $endIndex)
112-
{
113-
$lineEnding = $this->whitespacesConfig->getLineEnding();
114-
115-
// start index of the sequence is always 1 here, 0 is always open tag
116-
// transform "<?php\n" to "<?php " if needed
117-
if (false !== strpos($tokens[0]->getContent(), "\n")) {
118-
$tokens[0]->setContent(trim($tokens[0]->getContent()).' ');
119-
}
120-
121-
if ($endIndex === count($tokens) - 1) {
122-
return; // no more tokens afters sequence, single_blank_line_at_eof might add a line
123-
}
124-
125-
if (!$tokens[1 + $endIndex]->isWhitespace()) {
126-
$tokens->insertAt(1 + $endIndex, new Token(array(T_WHITESPACE, $lineEnding)));
127-
128-
return;
129-
}
130-
131-
$content = $tokens[1 + $endIndex]->getContent();
132-
if (false !== strpos($content, "\n")) {
133-
return;
134-
}
135-
136-
$tokens[1 + $endIndex]->setContent($lineEnding.ltrim($content));
137-
}
138-
13983
/**
14084
* @return Token[]
14185
*/
@@ -162,13 +106,52 @@ private function getDeclareStrictTypeSequence()
162106
}
163107

164108
/**
165-
* @param Tokens $tokens
109+
* @param Tokens $tokens
110+
* @param array<int, Token> $sequence
166111
*/
112+
private function fixStrictTypesCasing(Tokens $tokens, array $sequence)
113+
{
114+
/** @var int $index */
115+
/** @var Token $token */
116+
foreach ($sequence as $index => $token) {
117+
if ($token->isGivenKind(T_STRING)) {
118+
$tokens[$index]->setContent(strtolower($token->getContent()));
119+
120+
break;
121+
}
122+
}
123+
}
124+
167125
private function insertSequence(Tokens $tokens)
168126
{
169127
$sequence = $this->getDeclareStrictTypeSequence();
170128
$sequence[] = new Token(';');
129+
$endIndex = count($sequence);
130+
171131
$tokens->insertAt(1, $sequence);
172-
$this->fixWhiteSpaceAroundSequence($tokens, count($sequence));
132+
133+
// start index of the sequence is always 1 here, 0 is always open tag
134+
// transform "<?php\n" to "<?php " if needed
135+
if (false !== strpos($tokens[0]->getContent(), "\n")) {
136+
$tokens[0]->setContent(trim($tokens[0]->getContent()).' ');
137+
}
138+
139+
if ($endIndex === count($tokens) - 1) {
140+
return; // no more tokens afters sequence, single_blank_line_at_eof might add a line
141+
}
142+
143+
$lineEnding = $this->whitespacesConfig->getLineEnding();
144+
if (!$tokens[1 + $endIndex]->isWhitespace()) {
145+
$tokens->insertAt(1 + $endIndex, new Token(array(T_WHITESPACE, $lineEnding)));
146+
147+
return;
148+
}
149+
150+
$content = $tokens[1 + $endIndex]->getContent();
151+
if (false !== strpos($content, "\n")) {
152+
return;
153+
}
154+
155+
$tokens[1 + $endIndex]->setContent($lineEnding.ltrim($content));
173156
}
174157
}

tests/Fixer/Strict/DeclareStrictTypesFixerTest.php

Lines changed: 3 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
/**
1919
* @internal
20+
*
21+
* @author SpacePossum
2022
*/
2123
final class DeclareStrictTypesFixerTest extends AbstractFixerTestCase
2224
{
@@ -36,14 +38,6 @@ public function provideFixCases()
3638
{
3739
return array(
3840
array(
39-
'<?php declare(strict_types=1);
40-
declare(ticks=1);
41-
//
42-
43-
44-
namespace A\B\C;
45-
class A {
46-
}',
4741
'<?php
4842
declare(ticks=1);
4943
//
@@ -53,42 +47,23 @@ class A {
5347
class A {
5448
}',
5549
),
56-
array(
57-
'<?php DECLARE/* A b C*/(strict_types=1);
58-
//abc',
59-
'<?php DECLARE/* A b C*/(strict_types=1); //abc',
60-
),
6150
array(
6251
'<?php declare/* A b C*/(strict_types=1);',
6352
),
6453
array(
65-
'<?php declare(strict_types=1);
66-
/**/ /**/ ?>Test',
54+
'<?php /**/ /**/ deClarE (strict_types=1) ?>Test',
6755
'<?php /**/ /**/ deClarE (STRICT_TYPES=1) ?>Test',
6856
),
6957
array(
70-
'<?php declare(strict_types=1);
71-
',
7258
'<?php DECLARE ( strict_types=1 ) ;',
7359
),
7460
array(
75-
'<?php declare(strict_types=1);
76-
/**/
77-
',
7861
'<?php
7962
/**/
8063
declare(strict_types=1);',
8164
),
8265
array(
8366
'<?php declare(strict_types=1);
84-
/**/ /**/ /**/
85-
/* abc */ ',
86-
'<?php
87-
/**/ /**/ /**/
88-
declare /* abc */ (strict_types=1);',
89-
),
90-
array(
91-
'<?php declare(strict_types=1);
9267
phpinfo();',
9368
'<?php
9469
@@ -105,21 +80,6 @@ class A {
10580
/**
10681
* Foo
10782
*/
108-
phpinfo();',
109-
),
110-
array(
111-
'<?php declare(strict_types=1);
112-
/*
113-
* Foo
114-
*/
115-
116-
phpinfo();',
117-
'<?php
118-
119-
/*
120-
* Foo
121-
*/
122-
12383
phpinfo();',
12484
),
12585
array(
@@ -129,11 +89,6 @@ class A {
12989
),
13090
array(
13191
'<?php declare(strict_types=1);
132-
$a = 1;',
133-
'<?php declare(strict_types=1); $a = 1;',
134-
),
135-
array(
136-
'<?php declare(strict_types=1);
13792
$a = 456;
13893
',
13994
'<?php

tests/FixerFactoryTest.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -282,9 +282,6 @@ public function getFixersPriorityCases()
282282
array($fixers['no_useless_else'], $fixers['no_useless_return']), // tested also in: no_useless_else,no_useless_return.test
283283
array($fixers['no_useless_else'], $fixers['no_trailing_whitespace']), // tested also in: no_useless_else,no_trailing_whitespace.test
284284
array($fixers['no_useless_else'], $fixers['no_whitespace_in_blank_line']), // tested also in: no_useless_else,no_whitespace_in_blank_line.test
285-
array($fixers['declare_strict_types'], $fixers['no_blank_lines_before_namespace']), // tested also in: declare_strict_types,no_blank_lines_before_namespace.test
286-
array($fixers['declare_strict_types'], $fixers['no_extra_consecutive_blank_lines']), // tested also in: declare_strict_types,no_extra_consecutive_blank_lines.test
287-
array($fixers['declare_strict_types'], $fixers['no_whitespace_in_blank_line']), // tested also in: declare_strict_types,no_whitespace_in_blank_line.test
288285
array($fixers['declare_strict_types'], $fixers['single_blank_line_before_namespace']), // tested also in: declare_strict_types,single_blank_line_before_namespace.test
289286
array($fixers['array_syntax'], $fixers['binary_operator_spaces']), // tested also in: array_syntax,binary_operator_spaces.test
290287
array($fixers['array_syntax'], $fixers['ternary_operator_spaces']), // tested also in: array_syntax,ternary_operator_spaces.test

tests/Fixtures/Integration/priority/declare_strict_types,no_blank_lines_before_namespace.test

Lines changed: 0 additions & 21 deletions
This file was deleted.

tests/Fixtures/Integration/priority/declare_strict_types,no_extra_consecutive_blank_lines.test

Lines changed: 0 additions & 22 deletions
This file was deleted.

tests/Fixtures/Integration/priority/declare_strict_types,no_whitespace_in_blank_line.test

Lines changed: 0 additions & 23 deletions
This file was deleted.

tests/Fixtures/Integration/priority/declare_strict_types,single_blank_line_before_namespace.test

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,12 @@ Integration of fixers: declare_strict_types,single_blank_line_before_namespace.
66
{"php": 70000}
77
--EXPECT--
88
<?php declare(strict_types=1);
9-
declare(ticks=1);
109

1110
namespace A\B\C;
1211
class A {
1312
}
1413

1514
--INPUT--
16-
<?php
17-
declare(ticks=1);
18-
declare(strict_types=1);
19-
20-
namespace A\B\C;
15+
<?php namespace A\B\C;
2116
class A {
2217
}

0 commit comments

Comments
 (0)