Skip to content

Commit

Permalink
Detect class name with wrong case in catch block
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Mar 31, 2018
1 parent 6b75eeb commit 597bbed
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 3 deletions.
8 changes: 7 additions & 1 deletion conf/config.level0.neon
Expand Up @@ -11,7 +11,6 @@ rules:
- PHPStan\Rules\Classes\InstantiationRule
- PHPStan\Rules\Classes\RequireParentConstructCallRule
- PHPStan\Rules\Classes\UnusedConstructorParametersRule
- PHPStan\Rules\Exceptions\CaughtExceptionExistenceRule
- PHPStan\Rules\Functions\CallToFunctionParametersRule
- PHPStan\Rules\Functions\CallToNonExistentFunctionRule
- PHPStan\Rules\Functions\ExistingClassesInClosureTypehintsRule
Expand All @@ -37,6 +36,13 @@ services:
arguments:
checkClassCaseSensitivity: %checkClassCaseSensitivity%

-
class: PHPStan\Rules\Exceptions\CaughtExceptionExistenceRule
tags:
- phpstan.rules.rule
arguments:
checkClassCaseSensitivity: %checkClassCaseSensitivity%

-
class: PHPStan\Rules\Properties\ExistingClassesInPropertiesRule
tags:
Expand Down
26 changes: 25 additions & 1 deletion src/Rules/Exceptions/CaughtExceptionExistenceRule.php
Expand Up @@ -6,6 +6,7 @@
use PhpParser\Node\Stmt\Catch_;
use PHPStan\Analyser\Scope;
use PHPStan\Broker\Broker;
use PHPStan\Rules\ClassCaseSensitivityCheck;

class CaughtExceptionExistenceRule implements \PHPStan\Rules\Rule
{
Expand All @@ -15,9 +16,25 @@ class CaughtExceptionExistenceRule implements \PHPStan\Rules\Rule
*/
private $broker;

public function __construct(Broker $broker)
/**
* @var \PHPStan\Rules\ClassCaseSensitivityCheck
*/
private $classCaseSensitivityCheck;

/**
* @var bool
*/
private $checkClassCaseSensitivity;

public function __construct(
Broker $broker,
ClassCaseSensitivityCheck $classCaseSensitivityCheck,
bool $checkClassCaseSensitivity
)
{
$this->broker = $broker;
$this->classCaseSensitivityCheck = $classCaseSensitivityCheck;
$this->checkClassCaseSensitivity = $checkClassCaseSensitivity;
}

public function getNodeType(): string
Expand Down Expand Up @@ -51,6 +68,13 @@ public function processNode(Node $node, Scope $scope): array
if (!$classReflection->isInterface() && !$classReflection->getNativeReflection()->implementsInterface(\Throwable::class)) {
$errors[] = sprintf('Caught class %s is not an exception.', $classReflection->getDisplayName());
}

if ($this->checkClassCaseSensitivity) {
$errors = array_merge(
$errors,
$this->classCaseSensitivityCheck->checkClassNames([$class])
);
}
}

return $errors;
Expand Down
Expand Up @@ -2,13 +2,18 @@

namespace PHPStan\Rules\Exceptions;

use PHPStan\Rules\ClassCaseSensitivityCheck;

class CaughtExceptionExistenceRuleTest extends \PHPStan\Testing\RuleTestCase
{

protected function getRule(): \PHPStan\Rules\Rule
{
$broker = $this->createBroker();
return new CaughtExceptionExistenceRule(
$this->createBroker()
$broker,
new ClassCaseSensitivityCheck($broker),
true
);
}

Expand All @@ -23,6 +28,10 @@ public function testCheckCaughtException(): void
'Caught class FooCatchException not found.',
29,
],
[
'Class TestCatch\MyCatchException referenced with incorrect case: TestCatch\MyCatchEXCEPTION.',
41,
],
]);
}

Expand Down
6 changes: 6 additions & 0 deletions tests/PHPStan/Rules/Exceptions/data/catch.php
Expand Up @@ -35,3 +35,9 @@ class MyCatchException extends \Exception
} catch (\TypeError $e) {

}

try {

} catch (\TestCatch\MyCatchEXCEPTION $e) {

}

0 comments on commit 597bbed

Please sign in to comment.