Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Ignore shebang in DeclareStrictTypesRule
  • Loading branch information
staabm committed Nov 26, 2023
commit eecfa2d337019544f08eafb9e5b81dd17470593f
9 changes: 9 additions & 0 deletions src/Parser/DeclarePositionVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PhpParser\Node;
use PhpParser\NodeVisitorAbstract;
use function str_starts_with;

class DeclarePositionVisitor extends NodeVisitorAbstract
{
Expand All @@ -20,6 +21,14 @@ public function beforeTraverse(array $nodes): ?array

public function enterNode(Node $node): ?Node
{
if (
$this->isFirstStatement
&& $node instanceof Node\Stmt\InlineHTML
&& str_starts_with($node->value, '#!/usr/bin/')
) {
return null;
}

if ($node instanceof Node\Stmt) {
if ($node instanceof Node\Stmt\Declare_) {
$node->setAttribute(self::ATTRIBUTE_NAME, $this->isFirstStatement);
Expand Down
6 changes: 6 additions & 0 deletions tests/PHPStan/Rules/Keywords/DeclareStrictTypesRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ public function testMulti(): void
$this->analyse([__DIR__ . '/data/declare-multi.php'], []);
}

public function testShebang(): void
{
$this->analyse([__DIR__ . '/data/declare-shebang.php'], []);
$this->analyse([__DIR__ . '/data/declare-shebang2.php'], []);
}

public function testNonsense(): void
{
$this->analyse([__DIR__ . '/data/declare-strict-nonsense.php'], [
Expand Down
4 changes: 4 additions & 0 deletions tests/PHPStan/Rules/Keywords/data/declare-shebang.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env php
<?php declare(strict_types = 1);

namespace App;
4 changes: 4 additions & 0 deletions tests/PHPStan/Rules/Keywords/data/declare-shebang2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/php
<?php declare(strict_types = 1);

namespace App;