Skip to content

Commit

Permalink
Upgrading to PHPStan 0.12
Browse files Browse the repository at this point in the history
This PR upgrades the code to PHPStan 0.12.
Because the new level 6 of PHPStan is about checking type hints, all the type-hint related rules of this package are now useless.
This PR therefore removes all those rules.
  • Loading branch information
moufmouf committed Dec 4, 2019
1 parent a2aa2fc commit 49e9f57
Show file tree
Hide file tree
Showing 22 changed files with 29 additions and 1,005 deletions.
9 changes: 4 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
],
"require": {
"php": "^7.1",
"phpstan/phpstan": "^0.11.7"
"phpstan/phpstan": "^0.12"
},
"require-dev": {
"phpunit/phpunit": "^7.1",
Expand All @@ -24,19 +24,18 @@
},
"autoload-dev": {
"classmap": [
"tests/Rules/Exceptions/data/",
"tests/Rules/TypeHints/data/"
"tests/Rules/Exceptions/data/"
],
"psr-4": {
"TheCodingMachine\\PHPStan\\": "tests/"
}
},
"scripts": {
"phpstan": "phpstan analyse src -c phpstan.neon --level=5 --no-progress -vvv"
"phpstan": "phpstan analyse src -c phpstan.neon --level=6 --no-progress -vvv"
},
"extra": {
"branch-alias": {
"dev-master": "0.10-dev"
"dev-master": "0.12-dev"
},
"phpstan": {
"includes": [
Expand Down
8 changes: 0 additions & 8 deletions phpstan-strict-rules.neon
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,6 @@ services:
class: TheCodingMachine\PHPStan\Rules\Exceptions\MustRethrowRule
tags:
- phpstan.rules.rule
-
class: TheCodingMachine\PHPStan\Rules\TypeHints\MissingTypeHintInFunctionRule
tags:
- phpstan.rules.rule
-
class: TheCodingMachine\PHPStan\Rules\TypeHints\MissingTypeHintInMethodRule
tags:
- phpstan.rules.rule
-
class: TheCodingMachine\PHPStan\Rules\Superglobals\NoSuperglobalsRule
tags:
Expand Down
3 changes: 0 additions & 3 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
parameters:
ignoreErrors:
- '#Access to an undefined property PhpParser\\Node\\FunctionLike::\$name.#'
includes:
- phpstan-strict-rules.neon
2 changes: 2 additions & 0 deletions src/Rules/Conditionals/SwitchMustContainDefaultRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

/**
* A switch statement must always contain a "default" statement.
*
* @implements Rule<Switch_>
*/
class SwitchMustContainDefaultRule implements Rule
{
Expand Down
2 changes: 2 additions & 0 deletions src/Rules/Exceptions/DoNotThrowExceptionBaseClassRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
/**
* This rule checks that the base \Exception class is never thrown. Instead, developers should subclass the \Exception
* base class and throw the sub-type.
*
* @implements Rule<Node\Stmt\Throw_>
*/
class DoNotThrowExceptionBaseClassRule implements Rule
{
Expand Down
3 changes: 3 additions & 0 deletions src/Rules/Exceptions/EmptyExceptionRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
use PHPStan\Rules\Rule;
use function strpos;

/**
* @implements Rule<Catch_>
*/
class EmptyExceptionRule implements Rule
{
public function getNodeType(): string
Expand Down
6 changes: 6 additions & 0 deletions src/Rules/Exceptions/MustRethrowRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
/**
* When catching \Exception, \RuntimeException or \Throwable, the exception MUST be thrown again
* (unless you are developing an exception handler...)
*
* @implements Rule<Catch_>
*/
class MustRethrowRule implements Rule
{
Expand Down Expand Up @@ -49,13 +51,17 @@ public function processNode(Node $node, Scope $scope): array

// Let's visit and find a throw.
$visitor = new class() extends NodeVisitorAbstract {
/**
* @var bool
*/
private $throwFound = false;

public function leaveNode(Node $node)
{
if ($node instanceof Node\Stmt\Throw_) {
$this->throwFound = true;
}
return null;
}

/**
Expand Down
10 changes: 10 additions & 0 deletions src/Rules/Exceptions/ThrowMustBundlePreviousExceptionRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
/**
* When throwing into a catch block, checks that the previous exception is passed to the new "throw" clause
* (the initial stack trace must not be lost).
*
* @implements Rule<Catch_>
*/
class ThrowMustBundlePreviousExceptionRule implements Rule
{
Expand All @@ -34,7 +36,13 @@ public function processNode(Node $node, Scope $scope): array
* @var string
*/
private $catchedVariableName;
/**
* @var int
*/
private $exceptionUsedCount = 0;
/**
* @var Node\Stmt\Throw_[]
*/
private $unusedThrows = [];

public function __construct(string $catchedVariableName)
Expand All @@ -48,6 +56,7 @@ public function leaveNode(Node $node)
if ($node->name === $this->catchedVariableName) {
$this->exceptionUsedCount++;
}
return null;
}

// If the variable is used in the context of a method call (like $e->getMessage()), the exception is not passed as a "previous exception".
Expand All @@ -60,6 +69,7 @@ public function leaveNode(Node $node)
if ($node instanceof Node\Stmt\Throw_ && $this->exceptionUsedCount === 0) {
$this->unusedThrows[] = $node;
}
return null;
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/Rules/Superglobals/NoSuperglobalsRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

/**
* This rule checks that no superglobals are used in code.
*
* @implements Rule<Node\Expr\Variable>
*/
class NoSuperglobalsRule implements Rule
{
Expand Down
Loading

0 comments on commit 49e9f57

Please sign in to comment.