Skip to content

Commit

Permalink
Fix: Do not report about not declared strict types if file begins wit…
Browse files Browse the repository at this point in the history
…h shebang
  • Loading branch information
Great-Antique authored and localheinz committed Jan 28, 2020
1 parent a146447 commit ae4ef07
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ on:
- "master"

env:
MIN_COVERED_MSI: 93
MIN_COVERED_MSI: 92
MIN_MSI: 83
REQUIRED_PHP_EXTENSIONS: "mbstring"

Expand Down
16 changes: 14 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,15 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

## Unreleased

For a full diff see [`0.14.2...master`][0.14.2...master].
For a full diff see [`0.14.3...master`][0.14.3...master].

## [`0.14.3`][0.14.3]

For a full diff see [`0.14.2...0.14.3`][0.14.2...0.14.3].

### Fixed

* Ignored first line in `DeclareStrictTypesRule` when it is a shebang ([#186]), by [@Great-Antique]

## [`0.14.2`][0.14.2]

Expand Down Expand Up @@ -296,6 +304,7 @@ For a full diff see [`362c7ea...0.1.0`][362c7ea...0.1.0].
[0.14.0]: https://github.com/ergebnis/phpstan-rules/releases/tag/0.14.0
[0.14.1]: https://github.com/ergebnis/phpstan-rules/releases/tag/0.14.1
[0.14.2]: https://github.com/ergebnis/phpstan-rules/releases/tag/0.14.2
[0.14.3]: https://github.com/ergebnis/phpstan-rules/releases/tag/0.14.3

[362c7ea...0.1.0]: https://github.com/ergebnis/phpstan-rules/compare/362c7ea...0.1.0
[0.1.0...0.2.0]: https://github.com/ergebnis/phpstan-rules/compare/0.1.0...0.2.0
Expand All @@ -318,7 +327,8 @@ For a full diff see [`362c7ea...0.1.0`][362c7ea...0.1.0].
[0.13.0...0.14.0]: https://github.com/ergebnis/phpstan-rules/compare/0.13.0...0.14.0
[0.14.0...0.14.1]: https://github.com/ergebnis/phpstan-rules/compare/0.14.0...0.14.1
[0.14.1...0.14.2]: https://github.com/ergebnis/phpstan-rules/compare/0.14.1...0.14.2
[0.14.2...master]: https://github.com/ergebnis/phpstan-rules/compare/0.14.2...master
[0.14.2...0.14.3]: https://github.com/ergebnis/phpstan-rules/compare/0.14.2...0.14.3
[0.14.3...master]: https://github.com/ergebnis/phpstan-rules/compare/0.14.3...master

[#1]: https://github.com/ergebnis/phpstan-rules/pull/1
[#4]: https://github.com/ergebnis/phpstan-rules/pull/4
Expand Down Expand Up @@ -365,6 +375,8 @@ For a full diff see [`362c7ea...0.1.0`][362c7ea...0.1.0].
[#158]: https://github.com/ergebnis/phpstan-rules/pull/158
[#161]: https://github.com/ergebnis/phpstan-rules/pull/161
[#166]: https://github.com/ergebnis/phpstan-rules/pull/166
[#186]: https://github.com/ergebnis/phpstan-rules/pull/186

[@ergebnis]: https://github.com/ergebnis
[@Great-Antique]: https://github.com/Great-Antique
[@localheinz]: https://github.com/localheinz
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
MIN_COVERED_MSI:=93
MIN_COVERED_MSI:=92
MIN_MSI:=83

.PHONY: it
Expand Down
8 changes: 8 additions & 0 deletions src/Files/DeclareStrictTypesRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ public function processNode(Node $node, Scope $scope): array

$firstNode = \array_shift($nodes);

if (
$firstNode instanceof Node\Stmt\InlineHTML
&& 2 === $firstNode->getEndLine()
&& 0 === \mb_strpos($firstNode->value, '#!')
) {
$firstNode = \array_shift($nodes);
}

if ($firstNode instanceof Node\Stmt\Declare_) {
foreach ($firstNode->declares as $declare) {
if (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env php
Another line below shebang
<?php

echo 'Hello!';
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env php
<?php

declare(strict_types=0);

echo 'Hello!';
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Some text but not shebang "#!"
<?php

echo 'Hello!';
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env php
<?php

declare(strict_types=1);

echo 'Hello!';
22 changes: 22 additions & 0 deletions test/Integration/Files/DeclareStrictTypesRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public function providerAnalysisSucceeds(): iterable
'file-with-doc-block-and-declare-strict-types-on-and-invalid-casing' => __DIR__ . '/../../Fixture/Files/DeclareStrictTypesRule/Success/file-with-doc-block-and-declare-strict-types-on-and-invalid-casing.php',
'file-with-doc-block-and-declare-strict-types-on-and-multiple-declares' => __DIR__ . '/../../Fixture/Files/DeclareStrictTypesRule/Success/file-with-doc-block-and-declare-strict-types-on-and-multiple-declares.php',
'file-with-doc-block-and-declare-strict-types-on-and-namespace-declaration' => __DIR__ . '/../../Fixture/Files/DeclareStrictTypesRule/Success/file-with-doc-block-and-declare-strict-types-on-and-namespace-declaration.php',
'file-with-shebang-and-declare-strict-types-on' => __DIR__ . '/../../Fixture/Files/DeclareStrictTypesRule/Success/file-with-shebang-and-declare-strict-types-on.php',
];

foreach ($paths as $description => $path) {
Expand Down Expand Up @@ -122,6 +123,27 @@ public function providerAnalysisFails(): iterable
7,
],
],
'file-with-shebang-and-another-one-text-line-before-opening-tag' => [
__DIR__ . '/../../Fixture/Files/DeclareStrictTypesRule/Failure/file-with-shebang-and-another-one-text-line-before-opening-tag.php',
[
'File is missing a "declare(strict_types=1)" declaration.',
1,
],
],
'file-with-shebang-and-declare-strict-types-off' => [
__DIR__ . '/../../Fixture/Files/DeclareStrictTypesRule/Failure/file-with-shebang-and-declare-strict-types-off.php',
[
'File is missing a "declare(strict_types=1)" declaration.',
1,
],
],
'file-with-text-before-opening-tag' => [
__DIR__ . '/../../Fixture/Files/DeclareStrictTypesRule/Failure/file-with-text-before-opening-tag.php',
[
'File is missing a "declare(strict_types=1)" declaration.',
1,
],
],
'file-without-declare-strict-types-and-namespace-declaration' => [
__DIR__ . '/../../Fixture/Files/DeclareStrictTypesRule/Failure/file-without-declare-strict-types-and-namespace-declaration.php',
[
Expand Down

0 comments on commit ae4ef07

Please sign in to comment.