Skip to content

Commit

Permalink
fix: FullyQualifiedStrictTypesFixer - do not add imports before PHP…
Browse files Browse the repository at this point in the history
… opening tag (#7955)
  • Loading branch information
kubawerlos committed Apr 16, 2024
1 parent 35aca55 commit f40f18e
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/Tokenizer/Analyzer/NamespacesAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,14 @@ public function getDeclarations(Tokens $tokens): array
}

if (0 === \count($namespaces) && $tokens->isTokenKindFound(T_OPEN_TAG)) {
$namespaces[] = new NamespaceAnalysis('', '', 0, 0, 0, \count($tokens) - 1);
$namespaces[] = new NamespaceAnalysis(
'',
'',
$openTagIndex = $tokens[0]->isGivenKind(T_INLINE_HTML) ? 1 : 0,
$openTagIndex,
$openTagIndex,
\count($tokens) - 1,
);
}

return $namespaces;
Expand Down
16 changes: 16 additions & 0 deletions tests/Fixer/Import/FullyQualifiedStrictTypesFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2604,6 +2604,22 @@ function foo($a) {}',
*/
function foo($a) {}',
];
yield 'with shebang' => [
<<<'PHP'
#!/usr/bin/env php
<?php
use Bar\Baz;
$foo = new Baz();
PHP,
<<<'PHP'
#!/usr/bin/env php
<?php
$foo = new Bar\Baz();
PHP,
['import_symbols' => true],
];
}

/**
Expand Down
19 changes: 18 additions & 1 deletion tests/Tokenizer/Analyzer/NamespacesAnalyzerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,27 @@ public static function provideNamespacesCases(): iterable
]];

yield [
'there is no namespace if there is no PHP code',
<<<'PHP'
#!/usr/bin/php
<?php
return true;
PHP,
[
new NamespaceAnalysis(
'',
'',
1,
1,
1,
5
),
],
];

yield [
'there is no namespace if there is no PHP code',
[],
];
}

/**
Expand Down

0 comments on commit f40f18e

Please sign in to comment.