Skip to content

Commit

Permalink
Merge pull request #51 from moufmouf/phpstan0.12
Browse files Browse the repository at this point in the history
Upgrading to PHPStan 0.12
  • Loading branch information
moufmouf authored Dec 4, 2019
2 parents a2aa2fc + 5050cad commit 8c58cc8
Show file tree
Hide file tree
Showing 24 changed files with 29 additions and 1,155 deletions.
10 changes: 0 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,6 @@ They are more "strict" than the default PHPStan rules and some may be controvers
- When throwing an exception inside a catch block, [you should pass the catched exception as the "previous" exception](http://bestpractices.thecodingmachine.com/php/error_handling.html#wrapping-an-exception-do-not-lose-the-previous-exception)
- If you catch a `Throwable`, an `Exception` or a `RuntimeException`, you must rethrow the exception.

### Type-hinting related rules

This is a PHP 7.1+ rule:

- You should use type-hinting when possible
- If not possible, you should use a Docblock to specify the type
- If type-hinting against an array, you should use a Docblock to further explain the content of the array

[More about type-hinting related rules...](doc/typehinting_rules.md)

### Superglobal related rules

- The use of [`$_GET`, `$_POST`, `$_FILES`, `$_COOKIE`, `$_SESSION`, `$_REQUEST` is forbidden](http://bestpractices.thecodingmachine.com/php/organize_your_code.html#stop-using-superglobals-).
Expand Down
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
140 changes: 0 additions & 140 deletions doc/typehinting_rules.md

This file was deleted.

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 8c58cc8

Please sign in to comment.