Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
10 changes: 10 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,15 @@ public function beforeTraverse(array $nodes): ?array

public function enterNode(Node $node): ?Node
{
// ignore shebang
if (
$this->isFirstStatement
&& $node instanceof Node\Stmt\InlineHTML
&& str_starts_with($node->value, '#!')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a test that tests str_starts_with($node->value, '#!'). So InlineHTML with different contents should still throw an error.

) {
return null;
}

if ($node instanceof Node\Stmt) {
if ($node instanceof Node\Stmt\Declare_) {
$node->setAttribute(self::ATTRIBUTE_NAME, $this->isFirstStatement);
Expand Down
7 changes: 7 additions & 0 deletions tests/PHPStan/Rules/Keywords/DeclareStrictTypesRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ 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'], []);
$this->analyse([__DIR__ . '/data/declare-shebang3.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;
4 changes: 4 additions & 0 deletions tests/PHPStan/Rules/Keywords/data/declare-shebang3.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/php
<?php declare(strict_types = 1);

namespace App;