Skip to content

Commit

Permalink
Tasks: Php - fixed strictTypesDeclarationFixer()
Browse files Browse the repository at this point in the history
  • Loading branch information
janpecha committed Dec 14, 2023
1 parent e77e974 commit 67d71ae
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Tasks/Php.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static function strictTypesDeclarationFixer(string &$contents, \Nette\Cod
{
$declarations = Utils\PhpCode::getDeclarations($contents);

if (!preg_match('#\bstrict_types\s*=\s*1\b#', implode('', $declarations))) {
if (!preg_match('#\bstrict_types\s*=\s*1\b#', implode("\n", $declarations))) {
if (str_starts_with($contents, '<?php')) {
$result->fix('Added missing declare(strict_types=1)');
$indent = Utils\FileContent::detectIndentation($contents);
Expand Down
13 changes: 13 additions & 0 deletions tests/CodeChecker/Tasks/Php.strictTypesDeclarationFixer.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,16 @@ test('Fix no indentation', function () {
"\t\tnamespace Foo;",
]), $content);
});


test('Multiple declares (invalid)', function () {
$result = new Result;
$content = implode("\n", [
'<?php',
'declare(strict_types=1);',
'declare(strict_types=1);',
"\t\tnamespace Foo;",
]);
Tasks\Php::strictTypesDeclarationFixer($content, $result);
Assert::same([], $result->getMessages());
});

0 comments on commit 67d71ae

Please sign in to comment.