-
Notifications
You must be signed in to change notification settings - Fork 438
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Dynamic "assert" throw type extension
- Loading branch information
1 parent
36fb382
commit fd3aa73
Showing
4 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Type\Php; | ||
|
||
use PhpParser\Node\Expr\FuncCall; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Reflection\FunctionReflection; | ||
use PHPStan\Type\DynamicFunctionThrowTypeExtension; | ||
use PHPStan\Type\ObjectType; | ||
use PHPStan\Type\Type; | ||
use Throwable; | ||
use function count; | ||
|
||
class AssertThrowTypeExtension implements DynamicFunctionThrowTypeExtension | ||
{ | ||
|
||
public function isFunctionSupported(FunctionReflection $functionReflection): bool | ||
{ | ||
return $functionReflection->getName() === 'assert'; | ||
} | ||
|
||
public function getThrowTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $funcCall, Scope $scope): ?Type | ||
{ | ||
if (count($funcCall->getArgs()) < 2) { | ||
return $functionReflection->getThrowType(); | ||
} | ||
|
||
$customThrow = $scope->getType($funcCall->getArgs()[1]->value); | ||
if ((new ObjectType(Throwable::class))->isSuperTypeOf($customThrow)->yes()) { | ||
return $customThrow; | ||
} | ||
|
||
return $functionReflection->getThrowType(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
namespace Bug5650; | ||
|
||
function (): void { | ||
try { | ||
assert(false, new RuntimeException("I am being thrown")); | ||
} catch (\RuntimeException $e) { | ||
|
||
} | ||
}; | ||
|
||
function (): void { | ||
try { | ||
assert(true, new RuntimeException("I could be but this time am not being thrown")); | ||
} catch (\RuntimeException $e) { | ||
|
||
} | ||
}; | ||
|
||
function (): void { | ||
try { | ||
assert(false); | ||
} catch (\RuntimeException $e) { | ||
|
||
} | ||
}; | ||
|
||
function (): void { | ||
try { | ||
assert(true); | ||
} catch (\RuntimeException $e) { | ||
|
||
} | ||
}; |