Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions conf/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -1413,6 +1413,11 @@ services:
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension

-
class: PHPStan\Type\Php\DsMapDynamicMethodThrowTypeExtension
tags:
- phpstan.dynamicMethodThrowTypeExtension

-
class: PHPStan\Type\Php\DioStatDynamicFunctionReturnTypeExtension
tags:
Expand Down
31 changes: 31 additions & 0 deletions src/Type/Php/DsMapDynamicMethodThrowTypeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php declare(strict_types = 1);

namespace PHPStan\Type\Php;

use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Type\DynamicMethodThrowTypeExtension;
use PHPStan\Type\Type;
use PHPStan\Type\VoidType;
use function count;

final class DsMapDynamicMethodThrowTypeExtension implements DynamicMethodThrowTypeExtension
{

public function isMethodSupported(MethodReflection $methodReflection): bool
{
return $methodReflection->getDeclaringClass()->getName() === 'Ds\Map'
&& $methodReflection->getName() === 'get';
}

public function getThrowTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type|null
{
if (count($methodCall->args) < 2) {
return $methodReflection->getThrowType();
}

return new VoidType();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,16 @@ public function testBug4814(): void
]);
}

public function testBug9066(): void
{
$this->analyse([__DIR__ . '/data/bug-9066.php'], [
[
'Dead catch - OutOfBoundsException is never thrown in the try block.',
19,
],
]);
}

public function testThrowExpression(): void
{
$this->analyse([__DIR__ . '/data/dead-catch-throw-expr.php'], [
Expand Down
23 changes: 23 additions & 0 deletions tests/PHPStan/Rules/Exceptions/data/bug-9066.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Bug9066;

class Foo
{
public function mayThrow()
{ $map = new \Ds\Map();
try {
$map->get('1');
} catch (\OutOfBoundsException $e) {

}
}
public function neverThrows()
{ $map = new \Ds\Map();
try {
$map->get('1', null);
} catch (\OutOfBoundsException $e) {

}
}
}