Skip to content

Commit c5f3fe5

Browse files
Support for PHPStan 1.0 (#150)
* Support for PHPStan 1.0 * Use branch-alias 0.12-dev Co-authored-by: Josef Kříž <[email protected]>
1 parent 0de69d8 commit c5f3fe5

13 files changed

+50
-36
lines changed

composer.json

+7-7
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"prefer-stable": true,
99
"extra": {
1010
"branch-alias": {
11-
"dev-master": "0.11-dev"
11+
"dev-master": "0.12-dev"
1212
},
1313
"phpstan": {
1414
"includes": [
@@ -18,16 +18,16 @@
1818
},
1919
"require": {
2020
"php": ">=7.1",
21-
"nikic/php-parser": "^4.4",
22-
"phpstan/phpstan": "^0.12.26"
21+
"nikic/php-parser": "^4.13",
22+
"phpstan/phpstan": "^1.0"
2323
},
2424
"require-dev": {
2525
"nette/utils": "^3.0",
2626
"php-parallel-lint/php-console-highlighter": "^0.4.0",
2727
"php-parallel-lint/php-parallel-lint": "^1.2.0",
28-
"phpstan/phpstan-nette": "^0.12.0",
29-
"phpstan/phpstan-phpunit": "^0.12.0",
30-
"phpstan/phpstan-strict-rules": "^0.12.0",
28+
"phpstan/phpstan-nette": "^1.0",
29+
"phpstan/phpstan-phpunit": "^1.0",
30+
"phpstan/phpstan-strict-rules": "^1.0",
3131
"phpunit/phpunit": "^7.5.6 || ^9.4.2",
3232
"slevomat/coding-standard": "^6.4.1",
3333
"squizlabs/php_codesniffer": "~3.5.2"
@@ -57,7 +57,7 @@
5757
"check:tests": "phpunit",
5858
"check:cs": "phpcs --extensions=php --encoding=utf-8 --tab-width=4 --colors --ignore=tests/*/data -sp src tests/src",
5959
"check:lint": "parallel-lint src tests/src",
60-
"check:types": "phpstan analyse --memory-limit=1G -l max src tests",
60+
"check:types": "phpstan analyse --memory-limit=1G -l 8 src tests",
6161
"fix": "@fix:cs",
6262
"fix:cs": "phpcbf --extensions=php --encoding=utf-8 --tab-width=4 --colors --ignore=tests/*/data -sp src tests/src"
6363
},

extension.neon

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ services:
3131

3232
-
3333
class: Pepakriz\PHPStanExceptionRules\ThrowsAnnotationReader
34+
arguments:
35+
phpParser: @defaultAnalysisParser
3436
-
3537
class: Pepakriz\PHPStanExceptionRules\CheckedExceptionService
3638
factory: Pepakriz\PHPStanExceptionRules\CheckedExceptionService(%exceptionRules.checkedExceptions%, %exceptionRules.uncheckedExceptions%)

phpstan.neon.dist

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ includes:
99

1010
parameters:
1111
tmpDir: %rootDir%/../../../tmp
12-
excludes_analyse:
12+
excludePaths:
1313
- %rootDir%/../../../tests/*/data/*
1414

1515
exceptionRules:

src/Extension/DOMDocumentExtension.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function getThrowTypeFromMethodCall(MethodReflection $methodReflection, M
4444

4545
private function resolveLoadSourceType(MethodCall $methodCall, Scope $scope): Type
4646
{
47-
$valueType = $scope->getType($methodCall->args[0]->value);
47+
$valueType = $scope->getType($methodCall->getArgs()[0]->value);
4848
$exceptionType = new ObjectType(ErrorException::class);
4949

5050
foreach (TypeUtils::getConstantStrings($valueType) as $constantString) {

src/Extension/DateIntervalExtension.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class DateIntervalExtension implements DynamicConstructorThrowTypeExtension
2727
public function getThrowTypeFromConstructor(MethodReflection $methodReflection, New_ $newNode, Scope $scope): Type
2828
{
2929
if (is_a($methodReflection->getDeclaringClass()->getName(), DateInterval::class, true)) {
30-
return $this->resolveThrowType($newNode->args, $scope);
30+
return $this->resolveThrowType($newNode->getArgs(), $scope);
3131
}
3232

3333
throw new UnsupportedClassException();

src/Extension/DateTimeExtension.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function getThrowTypeFromConstructor(MethodReflection $methodReflection,
3232
is_a($methodReflection->getDeclaringClass()->getName(), DateTime::class, true)
3333
|| is_a($methodReflection->getDeclaringClass()->getName(), DateTimeImmutable::class, true)
3434
) {
35-
return $this->resolveThrowType($newNode->args, $scope);
35+
return $this->resolveThrowType($newNode->getArgs(), $scope);
3636
}
3737

3838
throw new UnsupportedClassException();

src/Extension/IntdivExtension.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function getThrowTypeFromFunctionCall(FunctionReflection $functionReflect
3030
}
3131

3232
$containsMax = false;
33-
$valueType = $scope->getType($functionCall->args[0]->value);
33+
$valueType = $scope->getType($functionCall->getArgs()[0]->value);
3434
foreach (TypeUtils::getConstantScalars($valueType) as $constantScalarType) {
3535
if ($constantScalarType->getValue() === PHP_INT_MAX) {
3636
$containsMax = true;
@@ -44,7 +44,7 @@ public function getThrowTypeFromFunctionCall(FunctionReflection $functionReflect
4444
}
4545

4646
$divisionByZero = false;
47-
$divisorType = $scope->getType($functionCall->args[1]->value);
47+
$divisorType = $scope->getType($functionCall->getArgs()[1]->value);
4848
foreach (TypeUtils::getConstantScalars($divisorType) as $constantScalarType) {
4949
if ($constantScalarType->getValue() === 0) {
5050
$divisionByZero = true;

src/Extension/JsonEncodeDecodeExtension.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ public function getThrowTypeFromFunctionCall(FunctionReflection $functionReflect
3535
return new VoidType();
3636
}
3737

38-
if (!isset($functionCall->args[3])) {
38+
if (!isset($functionCall->getArgs()[3])) {
3939
return new VoidType();
4040
}
4141

42-
$valueType = $scope->getType($functionCall->args[0]->value);
42+
$valueType = $scope->getType($functionCall->getArgs()[0]->value);
4343
foreach (TypeUtils::getConstantScalars($valueType) as $constantScalarType) {
4444
try {
4545
json_decode((string) $constantScalarType->getValue(), true, 512, JSON_THROW_ON_ERROR);
@@ -54,7 +54,7 @@ public function getThrowTypeFromFunctionCall(FunctionReflection $functionReflect
5454
}
5555

5656
$exceptionType = new ObjectType(JsonException::class);
57-
$optionsType = $scope->getType($functionCall->args[3]->value);
57+
$optionsType = $scope->getType($functionCall->getArgs()[3]->value);
5858
foreach (TypeUtils::getConstantScalars($optionsType) as $constantScalarType) {
5959
if (!$constantScalarType instanceof IntegerType) {
6060
continue;
@@ -83,11 +83,11 @@ public function getThrowTypeFromFunctionCall(FunctionReflection $functionReflect
8383
return new VoidType();
8484
}
8585

86-
if (!isset($functionCall->args[1])) {
86+
if (!isset($functionCall->getArgs()[1])) {
8787
return new VoidType();
8888
}
8989

90-
$valueType = $scope->getType($functionCall->args[0]->value);
90+
$valueType = $scope->getType($functionCall->getArgs()[0]->value);
9191
foreach (TypeUtils::getConstantScalars($valueType) as $constantScalarType) {
9292
try {
9393
json_encode($constantScalarType->getValue(), JSON_THROW_ON_ERROR);
@@ -102,7 +102,7 @@ public function getThrowTypeFromFunctionCall(FunctionReflection $functionReflect
102102
}
103103

104104
$exceptionType = new ObjectType(JsonException::class);
105-
$optionsType = $scope->getType($functionCall->args[1]->value);
105+
$optionsType = $scope->getType($functionCall->getArgs()[1]->value);
106106
foreach (TypeUtils::getConstantScalars($optionsType) as $constantScalarType) {
107107
if (!$constantScalarType instanceof IntegerType) {
108108
continue;

src/Extension/ReflectionExtension.php

+9-9
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,11 @@ public function getThrowTypeFromConstructor(MethodReflection $methodReflection,
8888
private function resolveReflectionClass(New_ $newNode, Scope $scope): Type
8989
{
9090
$reflectionExceptionType = new ObjectType(ReflectionException::class);
91-
if (!isset($newNode->args[0])) {
91+
if (!isset($newNode->getArgs()[0])) {
9292
return $reflectionExceptionType;
9393
}
9494

95-
$valueType = $this->resolveType($newNode->args[0]->value, $scope);
95+
$valueType = $this->resolveType($newNode->getArgs()[0]->value, $scope);
9696
foreach (TypeUtils::getConstantStrings($valueType) as $constantString) {
9797
if (!$this->broker->hasClass($constantString->getValue())) {
9898
return $reflectionExceptionType;
@@ -111,11 +111,11 @@ private function resolveReflectionClass(New_ $newNode, Scope $scope): Type
111111
private function resolveReflectionFunction(New_ $newNode, Scope $scope): Type
112112
{
113113
$reflectionExceptionType = new ObjectType(ReflectionException::class);
114-
if (!isset($newNode->args[0])) {
114+
if (!isset($newNode->getArgs()[0])) {
115115
return $reflectionExceptionType;
116116
}
117117

118-
$valueType = $this->resolveType($newNode->args[0]->value, $scope);
118+
$valueType = $this->resolveType($newNode->getArgs()[0]->value, $scope);
119119
foreach (TypeUtils::getConstantStrings($valueType) as $constantString) {
120120
if (!$this->broker->hasFunction(new Name($constantString->getValue()), $scope)) {
121121
return $reflectionExceptionType;
@@ -148,12 +148,12 @@ private function resolveReflectionMethod(New_ $newNode, Scope $scope): Type
148148
private function resolveReflectionMethodOrProperty(New_ $newNode, Scope $scope, callable $existenceChecker): Type
149149
{
150150
$reflectionExceptionType = new ObjectType(ReflectionException::class);
151-
if (!isset($newNode->args[1])) {
151+
if (!isset($newNode->getArgs()[1])) {
152152
return $reflectionExceptionType;
153153
}
154154

155-
$valueType = $this->resolveType($newNode->args[0]->value, $scope);
156-
$propertyType = $this->resolveType($newNode->args[1]->value, $scope);
155+
$valueType = $this->resolveType($newNode->getArgs()[0]->value, $scope);
156+
$propertyType = $this->resolveType($newNode->getArgs()[1]->value, $scope);
157157
foreach (TypeUtils::getConstantStrings($valueType) as $constantString) {
158158
try {
159159
$classReflection = $this->broker->getClass($constantString->getValue());
@@ -188,11 +188,11 @@ private function resolveReflectionMethodOrProperty(New_ $newNode, Scope $scope,
188188
private function resolveReflectionExtension(New_ $newNode, Scope $scope): Type
189189
{
190190
$reflectionExceptionType = new ObjectType(ReflectionException::class);
191-
if (!isset($newNode->args[0])) {
191+
if (!isset($newNode->getArgs()[0])) {
192192
return $reflectionExceptionType;
193193
}
194194

195-
$valueType = $this->resolveType($newNode->args[0]->value, $scope);
195+
$valueType = $this->resolveType($newNode->getArgs()[0]->value, $scope);
196196
foreach (TypeUtils::getConstantStrings($valueType) as $constantString) {
197197
if (!extension_loaded($constantString->getValue())) {
198198
return $reflectionExceptionType;

src/Extension/SimpleXMLElementExtension.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class SimpleXMLElementExtension implements DynamicConstructorThrowTypeExtension
2727
public function getThrowTypeFromConstructor(MethodReflection $methodReflection, New_ $newNode, Scope $scope): Type
2828
{
2929
if (is_a($methodReflection->getDeclaringClass()->getName(), SimpleXMLElement::class, true)) {
30-
return $this->resolveThrowType($newNode->args, $scope);
30+
return $this->resolveThrowType($newNode->getArgs(), $scope);
3131
}
3232

3333
throw new UnsupportedClassException();

src/Rules/ThrowsPhpDocRule.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -684,19 +684,19 @@ private function processFuncCall(FuncCall $node, Scope $scope): array
684684

685685
$functionName = $nodeName->toString();
686686
if ($functionName === 'count') {
687-
return $this->processThrowTypesOnMethod($node->args[0]->value, ['count'], $scope);
687+
return $this->processThrowTypesOnMethod($node->getArgs()[0]->value, ['count'], $scope);
688688
}
689689

690690
if ($functionName === 'iterator_count') {
691-
return $this->processThrowTypesOnMethod($node->args[0]->value, ['rewind', 'valid', 'next'], $scope);
691+
return $this->processThrowTypesOnMethod($node->getArgs()[0]->value, ['rewind', 'valid', 'next'], $scope);
692692
}
693693

694694
if ($functionName === 'iterator_to_array') {
695-
return $this->processThrowTypesOnMethod($node->args[0]->value, ['rewind', 'valid', 'current', 'key', 'next'], $scope);
695+
return $this->processThrowTypesOnMethod($node->getArgs()[0]->value, ['rewind', 'valid', 'current', 'key', 'next'], $scope);
696696
}
697697

698698
if ($functionName === 'iterator_apply') {
699-
return $this->processThrowTypesOnMethod($node->args[0]->value, ['rewind', 'valid', 'next'], $scope);
699+
return $this->processThrowTypesOnMethod($node->getArgs()[0]->value, ['rewind', 'valid', 'next'], $scope);
700700
}
701701

702702
try {
@@ -710,7 +710,7 @@ private function processFuncCall(FuncCall $node, Scope $scope): array
710710
if ($functionName === 'json_encode') {
711711
$throwType = TypeCombinator::union(
712712
$throwType,
713-
...$this->getThrowTypesOnMethod($node->args[0]->value, ['jsonSerialize'], $scope)
713+
...$this->getThrowTypesOnMethod($node->getArgs()[0]->value, ['jsonSerialize'], $scope)
714714
);
715715
}
716716

src/ThrowsAnnotationReader.php

+13-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PHPStan\Analyser\NameScope;
99
use PHPStan\Analyser\Scope;
1010
use PHPStan\Parser\Parser;
11+
use PHPStan\Parser\ParserErrorsException;
1112
use PHPStan\PhpDocParser\Lexer\Lexer;
1213
use PHPStan\PhpDocParser\Parser\PhpDocParser;
1314
use PHPStan\PhpDocParser\Parser\TokenIterator;
@@ -105,7 +106,11 @@ private function parse($reflection, string $sourceFile, ?string $namespace = nul
105106

106107
$tokens = new TokenIterator($this->phpDocLexer->tokenize($docBlock));
107108
$phpDocNode = $this->phpDocParser->parse($tokens);
108-
$nameScope = $this->createNameScope($sourceFile, $namespace);
109+
try {
110+
$nameScope = $this->createNameScope($sourceFile, $namespace);
111+
} catch (ParserErrorsException $exception) {
112+
return [];
113+
}
109114

110115
$annotations = [];
111116
foreach ($phpDocNode->getThrowsTagValues() as $tagValue) {
@@ -153,13 +158,18 @@ private function getDocblock($reflection): ?string
153158
return $docBlock !== false ? $docBlock : null;
154159
}
155160

161+
/**
162+
* @throws ParserErrorsException
163+
*/
156164
private function createNameScope(string $sourceFile, ?string $namespace = null): NameScope
157165
{
158166
return new NameScope($namespace, $this->getUsesMap($sourceFile, (string) $namespace));
159167
}
160168

161169
/**
162170
* @return string[]
171+
*
172+
* @throws ParserErrorsException
163173
*/
164174
private function getUsesMap(string $fileName, string $namespace): array
165175
{
@@ -172,6 +182,8 @@ private function getUsesMap(string $fileName, string $namespace): array
172182

173183
/**
174184
* @return string[][]
185+
*
186+
* @throws ParserErrorsException
175187
*/
176188
private function createUsesMap(string $sourceFile): array
177189
{

tests/src/Rules/UselessThrowsPhpDocRuleTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class UselessThrowsPhpDocRuleTest extends RuleTestCase
1414
protected function getRule(): Rule
1515
{
1616
return new UselessThrowsPhpDocRule(
17-
$this->createBroker([]),
17+
$this->createBroker(),
1818
$this->createThrowsAnnotationReader()
1919
);
2020
}

0 commit comments

Comments
 (0)