Skip to content

Commit 40940df

Browse files
committed
bug PHP-CS-Fixer#4689 DeclareStrictTypesFixer - fix for "strict_types" set to "0" (kubawerlos)
This PR was squashed before being merged into the 2.15 branch (closes PHP-CS-Fixer#4689). Discussion ---------- DeclareStrictTypesFixer - fix for "strict_types" set to "0" Fixes PHP-CS-Fixer#4687 Ping @Seldaek, @SpacePossum (as authors) and @JeroenVanOort (as bug reporter) for review. Commits ------- 3e64782 DeclareStrictTypesFixer - fix for \"strict_types\" set to \"0\"
2 parents f59ced9 + 3e64782 commit 40940df

File tree

2 files changed

+21
-31
lines changed

2 files changed

+21
-31
lines changed

src/Fixer/Strict/DeclareStrictTypesFixer.php

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -82,62 +82,48 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens)
8282
return;
8383
}
8484

85-
$sequence = $this->getDeclareStrictTypeSequence();
86-
$sequenceLocation = $tokens->findSequence($sequence, $searchIndex, null, false);
85+
$sequenceLocation = $tokens->findSequence([[T_DECLARE, 'declare'], '(', [T_STRING, 'strict_types'], '=', [T_LNUMBER], ')'], $searchIndex, null, false);
8786
if (null === $sequenceLocation) {
8887
$this->insertSequence($tokens); // declaration not found, insert one
8988

9089
return;
9190
}
9291

93-
$this->fixStrictTypesCasing($tokens, $sequenceLocation);
94-
}
95-
96-
/**
97-
* @return Token[]
98-
*/
99-
private function getDeclareStrictTypeSequence()
100-
{
101-
static $sequence = null;
102-
103-
// do not look for open tag, closing semicolon or empty lines;
104-
// - open tag is tested by isCandidate
105-
// - semicolon or end tag must be there to be valid PHP
106-
// - empty tokens and comments are dealt with later
107-
if (null === $sequence) {
108-
$sequence = [
109-
new Token([T_DECLARE, 'declare']),
110-
new Token('('),
111-
new Token([T_STRING, 'strict_types']),
112-
new Token('='),
113-
new Token([T_LNUMBER, '1']),
114-
new Token(')'),
115-
];
116-
}
117-
118-
return $sequence;
92+
$this->fixStrictTypesCasingAndValue($tokens, $sequenceLocation);
11993
}
12094

12195
/**
12296
* @param array<int, Token> $sequence
12397
*/
124-
private function fixStrictTypesCasing(Tokens $tokens, array $sequence)
98+
private function fixStrictTypesCasingAndValue(Tokens $tokens, array $sequence)
12599
{
126100
/** @var int $index */
127101
/** @var Token $token */
128102
foreach ($sequence as $index => $token) {
129103
if ($token->isGivenKind(T_STRING)) {
130104
$tokens[$index] = new Token([T_STRING, strtolower($token->getContent())]);
131105

106+
continue;
107+
}
108+
if ($token->isGivenKind(T_LNUMBER)) {
109+
$tokens[$index] = new Token([T_LNUMBER, '1']);
110+
132111
break;
133112
}
134113
}
135114
}
136115

137116
private function insertSequence(Tokens $tokens)
138117
{
139-
$sequence = $this->getDeclareStrictTypeSequence();
140-
$sequence[] = new Token(';');
118+
$sequence = [
119+
new Token([T_DECLARE, 'declare']),
120+
new Token('('),
121+
new Token([T_STRING, 'strict_types']),
122+
new Token('='),
123+
new Token([T_LNUMBER, '1']),
124+
new Token(')'),
125+
new Token(';'),
126+
];
141127
$endIndex = \count($sequence);
142128

143129
$tokens->insertAt(1, $sequence);

tests/Fixer/Strict/DeclareStrictTypesFixerTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,10 @@ class A {
118118
/**/',
119119
'<?php /**/',
120120
],
121+
[
122+
'<?php declare(strict_types=1);',
123+
'<?php declare(strict_types=0);',
124+
],
121125
];
122126
}
123127

0 commit comments

Comments
 (0)