Skip to content

Commit

Permalink
bug #4689 DeclareStrictTypesFixer - fix for "strict_types" set to "0"…
Browse files Browse the repository at this point in the history
… (kubawerlos)

This PR was squashed before being merged into the 2.15 branch (closes #4689).

Discussion
----------

DeclareStrictTypesFixer - fix for "strict_types" set to "0"

Fixes #4687

Ping @Seldaek, @SpacePossum (as authors) and @JeroenVanOort (as bug reporter) for review.

Commits
-------

3e64782 DeclareStrictTypesFixer - fix for \"strict_types\" set to \"0\"
  • Loading branch information
julienfalque committed Dec 13, 2019
2 parents f59ced9 + 3e64782 commit 40940df
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 31 deletions.
48 changes: 17 additions & 31 deletions src/Fixer/Strict/DeclareStrictTypesFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,62 +82,48 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens)
return;
}

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

return;
}

$this->fixStrictTypesCasing($tokens, $sequenceLocation);
}

/**
* @return Token[]
*/
private function getDeclareStrictTypeSequence()
{
static $sequence = null;

// do not look for open tag, closing semicolon or empty lines;
// - open tag is tested by isCandidate
// - semicolon or end tag must be there to be valid PHP
// - empty tokens and comments are dealt with later
if (null === $sequence) {
$sequence = [
new Token([T_DECLARE, 'declare']),
new Token('('),
new Token([T_STRING, 'strict_types']),
new Token('='),
new Token([T_LNUMBER, '1']),
new Token(')'),
];
}

return $sequence;
$this->fixStrictTypesCasingAndValue($tokens, $sequenceLocation);
}

/**
* @param array<int, Token> $sequence
*/
private function fixStrictTypesCasing(Tokens $tokens, array $sequence)
private function fixStrictTypesCasingAndValue(Tokens $tokens, array $sequence)
{
/** @var int $index */
/** @var Token $token */
foreach ($sequence as $index => $token) {
if ($token->isGivenKind(T_STRING)) {
$tokens[$index] = new Token([T_STRING, strtolower($token->getContent())]);

continue;
}
if ($token->isGivenKind(T_LNUMBER)) {
$tokens[$index] = new Token([T_LNUMBER, '1']);

break;
}
}
}

private function insertSequence(Tokens $tokens)
{
$sequence = $this->getDeclareStrictTypeSequence();
$sequence[] = new Token(';');
$sequence = [
new Token([T_DECLARE, 'declare']),
new Token('('),
new Token([T_STRING, 'strict_types']),
new Token('='),
new Token([T_LNUMBER, '1']),
new Token(')'),
new Token(';'),
];
$endIndex = \count($sequence);

$tokens->insertAt(1, $sequence);
Expand Down
4 changes: 4 additions & 0 deletions tests/Fixer/Strict/DeclareStrictTypesFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ class A {
/**/',
'<?php /**/',
],
[
'<?php declare(strict_types=1);',
'<?php declare(strict_types=0);',
],
];
}

Expand Down

0 comments on commit 40940df

Please sign in to comment.