Skip to content

Commit

Permalink
Update nikic/php-parser to 4.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jun 3, 2020
1 parent c1a171b commit 876d146
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 27 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"nette/neon": "^3.0",
"nette/schema": "^1.0",
"nette/utils": "^3.1.1",
"nikic/php-parser": "^4.4.0",
"nikic/php-parser": "^4.5.0",
"ondram/ci-detector": "^3.1",
"ondrejmirtes/better-reflection": "^4.3.1",
"phpdocumentor/type-resolver": "1.0.1",
Expand Down
8 changes: 6 additions & 2 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -2701,11 +2701,15 @@ public function enterForeachKey(Expr $iteratee, string $keyName): self

/**
* @param \PhpParser\Node\Name[] $classes
* @param string $variableName
* @param string|null $variableName
* @return self
*/
public function enterCatch(array $classes, string $variableName): self
public function enterCatch(array $classes, ?string $variableName): self
{
if ($variableName === null) {
return $this;
}

$type = TypeCombinator::union(...array_map(static function (string $class): ObjectType {
return new ObjectType($class);
}, $classes));
Expand Down
15 changes: 8 additions & 7 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -978,10 +978,6 @@ private function processStmtNode(

foreach ($stmt->catches as $catchNode) {
$nodeCallback($catchNode, $scope);
if (!is_string($catchNode->var->name)) {
throw new \PHPStan\ShouldNotHappenException();
}

if (!$this->polluteCatchScopeWithTryAssignments) {
$catchScopeResult = $this->processCatchNode($catchNode, $scope->mergeWith($tryScope), $nodeCallback);
$catchScopeForFinally = $catchScopeResult->getScope();
Expand Down Expand Up @@ -1106,11 +1102,16 @@ private function processCatchNode(
\Closure $nodeCallback
): StatementResult
{
if (!is_string($catchNode->var->name)) {
throw new \PHPStan\ShouldNotHappenException();
$variableName = null;
if ($catchNode->var !== null) {
if (!is_string($catchNode->var->name)) {
throw new \PHPStan\ShouldNotHappenException();
}

$variableName = $catchNode->var->name;
}

$catchScope = $catchScope->enterCatch($catchNode->types, $catchNode->var->name);
$catchScope = $catchScope->enterCatch($catchNode->types, $variableName);
return $this->processStmtNodes($catchNode, $catchNode->stmts, $catchScope, $nodeCallback);
}

Expand Down
17 changes: 16 additions & 1 deletion tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9917,7 +9917,7 @@ public function dataAssignNestedArray(): array
public function dataBug3276(): array
{
if (!self::$useStaticReflectionProvider && PHP_VERSION_ID < 70400) {
$this->markTestSkipped('Test requires PHP 7.4.');
return [];
}
return $this->gatherAssertTypes(__DIR__ . '/data/bug-3276.php');
}
Expand Down Expand Up @@ -9947,6 +9947,19 @@ public function dataBug3336(): array
return $this->gatherAssertTypes(__DIR__ . '/data/bug-3336.php');
}

public function dataCatchWithoutVariable(): array
{
if (!self::$useStaticReflectionProvider && PHP_VERSION_ID < 80000) {
return [];
}
return $this->gatherAssertTypes(__DIR__ . '/data/catch-without-variable.php');
}

public function dataMixedTypehint(): array
{
return $this->gatherAssertTypes(__DIR__ . '/data/mixed-typehint.php');
}

/**
* @dataProvider dataBug2574
* @dataProvider dataBug2577
Expand Down Expand Up @@ -10000,6 +10013,8 @@ public function dataBug3336(): array
* @dataProvider dataConstInFunctionsNamespaced
* @dataProvider dataRootScopeMaybeDefined
* @dataProvider dataBug3336
* @dataProvider dataCatchWithoutVariable
* @dataProvider dataMixedTypehint
* @param ConstantStringType $expectedType
* @param Type $actualType
*/
Expand Down
19 changes: 19 additions & 0 deletions tests/PHPStan/Analyser/data/catch-without-variable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace CatchWithoutVariable;

use function PHPStan\Analyser\assertType;

class Foo
{

public function doFoo(): void
{
try {

} catch (\FooException) {
assertType('*ERROR*', $e);
}
}

}
34 changes: 34 additions & 0 deletions tests/PHPStan/Analyser/data/mixed-typehint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace MixedTypehint;

use function PHPStan\Analyser\assertType;

class Foo
{

public function doFoo(mixed $foo)
{
assertType('mixed', $foo);
assertType('mixed', $this->doBar());
}

public function doBar(): mixed
{

}

}

function doFoo(mixed $foo)
{
assertType('mixed', $foo);
}

function (mixed $foo) {
assertType('mixed', $foo);
$f = function (): mixed {

};
assertType('mixed', $f());
};
16 changes: 0 additions & 16 deletions tests/PHPStan/Rules/Methods/ReturnTypeRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,6 @@ public function testReturnTypeRule(): void
'Method ReturnTypes\Foo::misleadingIntReturnType() should return ReturnTypes\integer but returns true.',
371,
],
[
'Method ReturnTypes\Foo::misleadingMixedReturnType() should return ReturnTypes\mixed but returns int.',
381,
],
[
'Method ReturnTypes\Foo::misleadingMixedReturnType() should return ReturnTypes\mixed but returns true.',
384,
],
[
'Method ReturnTypes\Stock::getAnotherStock() should return ReturnTypes\Stock but returns ReturnTypes\Stock|null.',
429,
Expand Down Expand Up @@ -305,14 +297,6 @@ public function testMisleadingTypehintsInClassWithoutNamespace(): void
'Method FooWithoutNamespace::misleadingIntReturnType() should return integer but returns true.',
28,
],
[
'Method FooWithoutNamespace::misleadingMixedReturnType() should return mixed but returns int.',
39,
],
[
'Method FooWithoutNamespace::misleadingMixedReturnType() should return mixed but returns true.',
43,
],
]);
}

Expand Down

0 comments on commit 876d146

Please sign in to comment.