Skip to content

Commit 099d467

Browse files
authored
PHPStan 0.12 support (#111)
1 parent f1081f3 commit 099d467

17 files changed

+90
-52
lines changed

composer.json

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,17 @@
1818
},
1919
"require": {
2020
"php": "^7.1",
21-
"phpstan/phpstan": "^0.11.7"
21+
"phpstan/phpstan": "^0.12.0"
2222
},
2323
"require-dev": {
2424
"jakub-onderka/php-console-highlighter": "0.4.0",
2525
"jakub-onderka/php-parallel-lint": "1.0.0",
26+
"nette/utils": "^3.0",
2627
"php-coveralls/php-coveralls": "^2.1",
27-
"phpstan/phpstan": "^0.11.19",
28-
"phpstan/phpstan-nette": "^0.11",
29-
"phpstan/phpstan-phpunit": "^0.11",
30-
"phpstan/phpstan-strict-rules": "^0.11",
28+
"phpstan/phpstan": "^0.12.0",
29+
"phpstan/phpstan-nette": "^0.12.0",
30+
"phpstan/phpstan-phpunit": "^0.12.0",
31+
"phpstan/phpstan-strict-rules": "^0.12.0",
3132
"phpunit/phpunit": "^7.5.6",
3233
"slevomat/coding-standard": "^5.0.4",
3334
"squizlabs/php_codesniffer": "~3.5.2"
@@ -57,7 +58,7 @@
5758
"check:tests": "phpunit --coverage-text",
5859
"check:cs": "phpcs --extensions=php --encoding=utf-8 --tab-width=4 --colors --ignore=tests/*/data -sp src tests/src",
5960
"check:lint": "parallel-lint src tests/src",
60-
"check:types": "phpstan analyse src tests",
61+
"check:types": "phpstan analyse -l max src tests",
6162
"fix": "@fix:cs",
6263
"fix:cs": "phpcbf --extensions=php --encoding=utf-8 --tab-width=4 --colors --ignore=tests/*/data -sp src tests/src",
6364
"coveralls": "php-coveralls -v"

phpstan.neon.dist

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
includes:
2-
- vendor/phpstan/phpstan/conf/config.levelmax.neon
32
- vendor/phpstan/phpstan/conf/bleedingEdge.neon
43
- vendor/phpstan/phpstan-phpunit/extension.neon
54
- vendor/phpstan/phpstan-phpunit/rules.neon
@@ -13,6 +12,13 @@ parameters:
1312
excludes_analyse:
1413
- %rootDir%/../../../tests/*/data/*
1514

15+
ignoreErrors:
16+
- "#^Method Pepakriz\\\\PHPStanExceptionRules\\\\RuleTestCase\\:\\:getRule\\(\\) return type with generic interface PHPStan\\\\Rules\\\\Rule does not specify its types\\: TNodeType$#"
17+
-
18+
message: "#^Parameter \\#1 \\$rules of class PHPStan\\\\Rules\\\\Registry constructor expects array\\<PHPStan\\\\Rules\\\\Rule\\>, array\\<int, TRule of PHPStan\\\\Rules\\\\Rule\\> given\\.$#"
19+
count: 1
20+
path: tests/src/RuleTestCase.php
21+
1622
exceptionRules:
1723
reportUnusedCatchesOfUncheckedExceptions: true
1824
uncheckedExceptions:

src/DynamicThrowTypeService.php

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use PHPStan\Analyser\Scope;
1010
use PHPStan\Reflection\FunctionReflection;
1111
use PHPStan\Reflection\MethodReflection;
12-
use PHPStan\Reflection\ThrowableReflection;
1312
use PHPStan\Type\Type;
1413
use PHPStan\Type\VoidType;
1514
use function spl_object_hash;
@@ -125,10 +124,7 @@ public function getMethodThrowType(MethodReflection $methodReflection, MethodCal
125124
}
126125
}
127126

128-
$throwType = null;
129-
if ($methodReflection instanceof ThrowableReflection) {
130-
$throwType = $methodReflection->getThrowType();
131-
}
127+
$throwType = $methodReflection->getThrowType();
132128

133129
return $throwType ?? new VoidType();
134130
}
@@ -157,10 +153,7 @@ public function getStaticMethodThrowType(MethodReflection $methodReflection, Sta
157153
}
158154
}
159155

160-
$throwType = null;
161-
if ($methodReflection instanceof ThrowableReflection) {
162-
$throwType = $methodReflection->getThrowType();
163-
}
156+
$throwType = $methodReflection->getThrowType();
164157

165158
return $throwType ?? new VoidType();
166159
}
@@ -187,10 +180,7 @@ public function getConstructorThrowType(MethodReflection $methodReflection, New_
187180
}
188181
}
189182

190-
$throwType = null;
191-
if ($methodReflection instanceof ThrowableReflection) {
192-
$throwType = $methodReflection->getThrowType();
193-
}
183+
$throwType = $methodReflection->getThrowType();
194184

195185
return $throwType ?? new VoidType();
196186
}

src/Rules/DeadCatchUnionRule.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
use function count;
1414
use function sprintf;
1515

16+
/**
17+
* @implements Rule<Catch_>
18+
*/
1619
class DeadCatchUnionRule implements Rule
1720
{
1821

src/Rules/ThrowsPhpDocInheritanceRule.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use PHPStan\Broker\Broker;
1313
use PHPStan\Broker\ClassNotFoundException;
1414
use PHPStan\Reflection\MissingMethodFromReflectionException;
15-
use PHPStan\Reflection\ThrowableReflection;
1615
use PHPStan\Rules\Rule;
1716
use PHPStan\ShouldNotHappenException;
1817
use PHPStan\Type\FileTypeMapper;
@@ -27,6 +26,9 @@
2726
use function count;
2827
use function sprintf;
2928

29+
/**
30+
* @implements Rule<ClassMethod>
31+
*/
3032
class ThrowsPhpDocInheritanceRule implements Rule
3133
{
3234

@@ -99,6 +101,7 @@ public function processNode(Node $node, Scope $scope): array
99101
$scope->getFile(),
100102
$classReflection->getName(),
101103
$traitName,
104+
$methodName,
102105
$docComment->getText()
103106
);
104107

@@ -126,10 +129,6 @@ public function processNode(Node $node, Scope $scope): array
126129
continue;
127130
}
128131

129-
if (!$methodReflection instanceof ThrowableReflection) {
130-
continue;
131-
}
132-
133132
try {
134133
$parentThrowType = $this->defaultThrowTypeService->getMethodThrowType($methodReflection);
135134
} catch (UnsupportedClassException | UnsupportedFunctionException $e) {

src/Rules/ThrowsPhpDocRule.php

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@
3232
use PHPStan\Broker\Broker;
3333
use PHPStan\Broker\ClassNotFoundException;
3434
use PHPStan\Broker\FunctionNotFoundException;
35+
use PHPStan\Node\UnreachableStatementNode;
3536
use PHPStan\Reflection\MethodReflection;
3637
use PHPStan\Reflection\MissingMethodFromReflectionException;
37-
use PHPStan\Reflection\ThrowableReflection;
3838
use PHPStan\Rules\Rule;
3939
use PHPStan\ShouldNotHappenException;
4040
use PHPStan\Type\NeverType;
@@ -56,6 +56,9 @@
5656
use function preg_match;
5757
use function sprintf;
5858

59+
/**
60+
* @implements Rule<Node>
61+
*/
5962
class ThrowsPhpDocRule implements Rule
6063
{
6164

@@ -158,6 +161,10 @@ public function getNodeType(): string
158161
*/
159162
public function processNode(Node $node, Scope $scope): array
160163
{
164+
if ($node instanceof UnreachableStatementNode) {
165+
return $this->processNode($node->getOriginalStatement(), $scope);
166+
}
167+
161168
if ($node instanceof TryCatch) {
162169
return $this->processTryCatch($node);
163170
}
@@ -236,10 +243,6 @@ public function processNode(Node $node, Scope $scope): array
236243
*/
237244
private function processWhitelistedMethod(MethodReflection $methodReflection): array
238245
{
239-
if (!$methodReflection instanceof ThrowableReflection) {
240-
return [];
241-
}
242-
243246
$throwType = $methodReflection->getThrowType();
244247

245248
if ($throwType === null) {
@@ -491,9 +494,7 @@ private function processFunction(Node\FunctionLike $node, Scope $scope): array
491494
}
492495
}
493496

494-
if ($methodReflection instanceof ThrowableReflection) {
495-
$this->throwsScope->enterToThrowsAnnotationBlock($methodReflection->getThrowType());
496-
}
497+
$this->throwsScope->enterToThrowsAnnotationBlock($methodReflection->getThrowType());
497498

498499
if (!$node->hasAttribute(self::ATTRIBUTE_HAS_CLASS_METHOD_END)) {
499500
$node->setAttribute(self::ATTRIBUTE_HAS_CLASS_METHOD_END, true);
@@ -523,10 +524,6 @@ private function processFunctionEnd(Scope $scope): array
523524
return [];
524525
}
525526

526-
if (!$functionReflection instanceof ThrowableReflection) {
527-
return [];
528-
}
529-
530527
$throwType = $functionReflection->getThrowType();
531528
if ($throwType === null) {
532529
return [];
@@ -765,10 +762,6 @@ private function getThrowTypesOnMethod($class, array $methods, Scope $scope): ar
765762
$throwTypes = [];
766763
$targetMethodReflections = $this->getMethodReflections($class, $methods, $scope);
767764
foreach ($targetMethodReflections as $targetMethodReflection) {
768-
if (!$targetMethodReflection instanceof ThrowableReflection) {
769-
continue;
770-
}
771-
772765
$throwType = $targetMethodReflection->getThrowType();
773766
if ($throwType === null) {
774767
continue;

src/Rules/UnreachableCatchRule.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
use function is_a;
1515
use function sprintf;
1616

17+
/**
18+
* @implements Rule<Stmt>
19+
*/
1720
class UnreachableCatchRule implements Rule
1821
{
1922

src/Rules/UselessThrowsPhpDocRule.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
use function sprintf;
2020
use function uksort;
2121

22+
/**
23+
* @implements Rule<FunctionLike>
24+
*/
2225
class UselessThrowsPhpDocRule implements Rule
2326
{
2427

tests/src/RuleTestCase.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
use PHPStan\Cache\Cache;
1515
use PHPStan\File\FileHelper;
1616
use PHPStan\File\FuzzyRelativePathHelper;
17+
use PHPStan\PhpDoc\PhpDocNodeResolver;
1718
use PHPStan\PhpDoc\PhpDocStringResolver;
18-
use PHPStan\PhpDoc\TypeNodeResolver;
1919
use PHPStan\PhpDoc\TypeNodeResolverExtension;
2020
use PHPStan\PhpDocParser\Lexer\Lexer;
2121
use PHPStan\PhpDocParser\Parser\PhpDocParser;
@@ -33,6 +33,9 @@
3333
use function trim;
3434
use const DIRECTORY_SEPARATOR;
3535

36+
/**
37+
* @template TRule of \PHPStan\Rules\Rule
38+
*/
3639
abstract class RuleTestCase extends TestCase
3740
{
3841

@@ -41,6 +44,9 @@ abstract class RuleTestCase extends TestCase
4144
*/
4245
private $analyser;
4346

47+
/**
48+
* @phpstan-return TRule
49+
*/
4450
abstract protected function getRule(): Rule;
4551

4652
protected function getTypeSpecifier(): TypeSpecifier
@@ -84,14 +90,14 @@ private function getAnalyser(): Analyser
8490
new NodeScopeResolver(
8591
$broker,
8692
$this->getParser(),
87-
new FileTypeMapper($this->getParser(), self::getContainer()->getByType(PhpDocStringResolver::class), $this->createMock(Cache::class), new AnonymousClassNameHelper(new FileHelper($currentWorkingDirectory), new FuzzyRelativePathHelper($currentWorkingDirectory, DIRECTORY_SEPARATOR, [])), new TypeNodeResolver($this->getTypeNodeResolverExtensions())),
93+
new FileTypeMapper($this->getParser(), self::getContainer()->getByType(PhpDocStringResolver::class), self::getContainer()->getByType(PhpDocNodeResolver::class), $this->createMock(Cache::class), new AnonymousClassNameHelper(new FileHelper($currentWorkingDirectory), new FuzzyRelativePathHelper($currentWorkingDirectory, DIRECTORY_SEPARATOR, []))),
8894
$fileHelper,
8995
$typeSpecifier,
9096
$this->shouldPolluteScopeWithLoopInitialAssignments(),
9197
$this->shouldPolluteCatchScopeWithTryAssignments(),
9298
$this->shouldPolluteScopeWithAlwaysIterableForeach(),
9399
[],
94-
false
100+
[]
95101
),
96102
$fileHelper,
97103
[],

tests/src/Rules/DeadCatchUnionRuleTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
use Pepakriz\PHPStanExceptionRules\RuleTestCase;
66
use PHPStan\Rules\Rule;
77

8+
/**
9+
* @extends RuleTestCase<DeadCatchUnionRule>
10+
*/
811
class DeadCatchUnionRuleTest extends RuleTestCase
912
{
1013

0 commit comments

Comments
 (0)