Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement: Implement Files\DeclareStrictTypesRule #79

Merged
merged 1 commit into from
Sep 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .php_cs.fixture
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ declare(strict_types=1);
use Localheinz\PhpCsFixer\Config;

$config = Config\Factory::fromRuleSet(new Config\RuleSet\Php71(''), [
'declare_strict_types' => false,
'final_class' => false,
'header_comment' => false,
'lowercase_constants' => false,
'lowercase_keywords' => false,
'magic_method_casing' => false,
Expand Down
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ jobs:
- mkdir -p $HOME/.build/infection

script:
- vendor/bin/infection --ignore-msi-with-no-mutations --min-covered-msi=86 --min-msi=86
- vendor/bin/infection --ignore-msi-with-no-mutations --min-covered-msi=98 --min-msi=85

notifications:
email: false
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

For a full diff see [`0.10.0...master`](https://github.com/localheinz/phpstan-rules/compare/0.10.0...master).

### Added

* Added `Files\DeclareStrictTypesRule`, which reports an error when a PHP file does not have a `declare(strict_types=1)` declaration ([#79](https://github.com/localheinz/phpstan-rules/pull/79)), by [@dmecke](https://github.com/dmecke)

### Changed

* Require at least `nikic/php-parser:~0.11.15` ([#102](https://github.com/localheinz/phpstan-rules/pull/102)), by [@localheinz](https://github.com/localheinz)
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ help: ## Displays this list of targets with descriptions

infection: vendor ## Runs mutation tests with infection
mkdir -p .build/infection
vendor/bin/infection --ignore-msi-with-no-mutations --min-covered-msi=86 --min-msi=86
vendor/bin/infection --ignore-msi-with-no-mutations --min-covered-msi=98 --min-msi=85

stan: vendor ## Runs a static analysis with phpstan
mkdir -p .build/phpstan
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ This package provides the following rules for use with [`phpstan/phpstan`](https
* [`Localheinz\PHPStan\Rules\Closures\NoParameterWithNullableTypeDeclarationRule`](https://github.com/localheinz/phpstan-rules#closuresnoparameterwithnullabletypedeclarationrule)
* [`Localheinz\PHPStan\Rules\Closures\NoParameterWithNullDefaultValueRule`](https://github.com/localheinz/phpstan-rules#closuresnoparameterwithnulldefaultvaluerule)
* [`Localheinz\PHPStan\Rules\Expressions\NoIssetRule`](https://github.com/localheinz/phpstan-rules#expressionsnoissetrule)
* [`Localheinz\PHPStan\Rules\Files\DeclareStrictTypesRule`](https://github.com/localheinz/phpstan-rules#filesdeclarestricttypesrule)
* [`Localheinz\PHPStan\Rules\Functions\NoNullableReturnTypeDeclarationRule`](https://github.com/localheinz/phpstan-rules#functionsnonullablereturntypedeclarationrule)
* [`Localheinz\PHPStan\Rules\Functions\NoParameterWithNullableTypeDeclaration`](https://github.com/localheinz/phpstan-rules#functionsnoparameterwithnullabletypedeclarationrule)
* [`Localheinz\PHPStan\Rules\Functions\NoParameterWithNullDefaultValueRule`](https://github.com/localheinz/phpstan-rules#functionsnoparameterwithnulldefaultvaluerule)
Expand Down Expand Up @@ -121,6 +122,12 @@ This rule reports an error when a closure has a parameter with `null` as default

This rule reports an error when the language construct `isset()` is used.

### Files

#### `Files\DeclareStrictTypesRule`

This rule reports an error when a non-empty file does not contain a `declare(strict_types=1)` declaration.

### Functions

#### `Functions\NoNullableReturnTypeDeclarationRule`
Expand Down
1 change: 1 addition & 0 deletions rules.neon
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ rules:
- Localheinz\PHPStan\Rules\Closures\NoNullableReturnTypeDeclarationRule
- Localheinz\PHPStan\Rules\Closures\NoParameterWithNullableTypeDeclarationRule
- Localheinz\PHPStan\Rules\Expressions\NoIssetRule
- Localheinz\PHPStan\Rules\Files\DeclareStrictTypesRule
- Localheinz\PHPStan\Rules\Functions\NoNullableReturnTypeDeclarationRule
- Localheinz\PHPStan\Rules\Functions\NoParameterWithNullableTypeDeclarationRule
- Localheinz\PHPStan\Rules\Functions\NoParameterWithNullDefaultValueRule
Expand Down
63 changes: 63 additions & 0 deletions src/Files/DeclareStrictTypesRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2018 Andreas Möller
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*
* @see https://github.com/localheinz/phpstan-rules
*/

namespace Localheinz\PHPStan\Rules\Files;

use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Node\FileNode;
use PHPStan\Rules\Rule;
use PHPStan\ShouldNotHappenException;

final class DeclareStrictTypesRule implements Rule
{
public function getNodeType(): string
{
return FileNode::class;
}

public function processNode(Node $node, Scope $scope): array
{
if (!$node instanceof FileNode) {
throw new ShouldNotHappenException(\sprintf(
'Expected node to be instance of "%s", but got instance of "%s" instead.',
FileNode::class,
\get_class($node)
));
}

$nodes = $node->getNodes();

if (0 === \count($nodes)) {
return [];
}

$firstNode = \array_shift($nodes);

if ($firstNode instanceof Node\Stmt\Declare_) {
foreach ($firstNode->declares as $declare) {
if (
'strict_types' === $declare->key->toLowerString()
&& $declare->value instanceof Node\Scalar\LNumber
&& 1 === $declare->value->value
) {
return [];
}
}
}

return [
'File is missing a "declare(strict_types=1)" declaration.',
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

// Hmm

declare(sTrIcT_tYpEs=0);

echo 'Hello!';
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

// Hmm

declare(ticks=5, strict_types = 0);

echo 'Hello!';
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

// Hmm

declare(strict_types=0);

echo 'Hello!';
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

declare(sTrIcT_tYpEs=0);

echo 'Hello!';
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

declare(ticks=5, strict_types = 0);

echo 'Hello!';
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

declare(strict_types=0);

echo 'Hello!';
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

declare(ticks=5);

echo 'Hello!';
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

/**
* Hmm.
*/

declare(sTrIcT_tYpEs=0);

echo 'Hello!';
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

/**
* Hmm.
*/

declare(ticks=5, strict_types = 0);

echo 'Hello!';
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

/**
* Hmm.
*/

declare(strict_types=0);

echo 'Hello!';
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

namespace Localheinz\PHPStan\Rules\Test\Fixture\Classes\DeclareStrictTypesRule\Failure;

echo 'Hello!';
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

echo 'Hello!';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

// Hmm

declare(sTrIcT_tYpEs=1);

echo 'Hello!';
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

// Hmm

declare(ticks=5, strict_types = 1);

echo 'Hello!';
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

// Hmm

declare(strict_types=1);

namespace Localheinz\PHPStan\Rules\Test\Fixture\Classes\DeclareStrictTypesRule\Success;

echo 'Hello!';
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

// Hmm

declare(strict_types=1);

echo 'Hello!';
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

declare(sTrIcT_tYpEs=1);

echo 'Hello!';
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

declare(ticks=5, strict_types = 1);

echo 'Hello!';
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

declare(strict_types=1);

namespace Localheinz\PHPStan\Rules\Test\Fixture\Classes\DeclareStrictTypesRule\Success;

echo 'Hello!';
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

declare(strict_types=1);

echo 'Hello!';
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

/**
* Hmm.
*/

declare(sTrIcT_tYpEs=1);

echo 'Hello!';
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

/**
* Hmm.
*/

declare(ticks=5, strict_types = 1);

echo 'Hello!';
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

/**
* Hmm.
*/

declare(strict_types=1);

namespace Localheinz\PHPStan\Rules\Test\Fixture\Classes\DeclareStrictTypesRule\Success;

echo 'Hello!';
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

/**
* Hmm.
*/

declare(strict_types=1);

echo 'Hello!';
Loading