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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Analyser/ExprHandler/ArrayDimFetchHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use PHPStan\Analyser\NodeScopeResolver;
use PHPStan\Analyser\NoopNodeCallback;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Node\Expr\TypeExpr;
use PHPStan\Type\NeverType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
Expand Down Expand Up @@ -99,7 +100,7 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
if (!$varType->isArray()->yes() && !(new ObjectType(ArrayAccess::class))->isSuperTypeOf($varType)->no()) {
$throwPoints = array_merge($throwPoints, $nodeScopeResolver->processExprNode(
$stmt,
new MethodCall($expr->var, 'offsetGet'),
new MethodCall(new TypeExpr($varType), 'offsetGet'),
$scope,
$storage,
new NoopNodeCallback(),
Expand Down
2 changes: 1 addition & 1 deletion src/Analyser/ExprHandler/AssignHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ public function processAssignVar(
) {
$throwPoints = array_merge($throwPoints, $nodeScopeResolver->processExprNode(
$stmt,
new MethodCall($originalVar->var, 'offsetSet'),
new MethodCall(new TypeExpr($setVarType), 'offsetSet'),
$scope,
$storage,
new NoopNodeCallback(),
Expand Down
3 changes: 2 additions & 1 deletion src/Analyser/ExprHandler/IssetHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use PHPStan\Analyser\NodeScopeResolver;
use PHPStan\Analyser\NoopNodeCallback;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Node\Expr\TypeExpr;
use PHPStan\Type\BooleanType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\ObjectType;
Expand Down Expand Up @@ -101,7 +102,7 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex

$throwPoints = array_merge($throwPoints, $nodeScopeResolver->processExprNode(
$stmt,
new MethodCall($var->var, 'offsetExists'),
new MethodCall(new TypeExpr($varType), 'offsetExists'),
$scope,
$storage,
new NoopNodeCallback(),
Expand Down
2 changes: 1 addition & 1 deletion src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2054,7 +2054,7 @@ public function processStmtNode(
if (!$varType->isArray()->yes() && !(new ObjectType(ArrayAccess::class))->isSuperTypeOf($varType)->no()) {
$throwPoints = array_merge($throwPoints, $this->processExprNode(
$stmt,
new MethodCall($var->var, 'offsetUnset'),
new MethodCall(new TypeExpr($varType), 'offsetUnset'),
$scope,
$storage,
new NoopNodeCallback(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,26 @@ public function testRule(): void
$this->analyse([__DIR__ . '/data/missing-exception-method-throws.php'], $errors);
}

public function testBugArrayOffset(): void
{
$this->analyse([__DIR__ . '/data/bug-array-offset.php'], [
[
"Method BugArrayOffset\Foo::__construct() throws checked exception BugArrayOffset\ParameterNotFoundException but it's missing from the PHPDoc @throws tag.",
19,
],
[
"Method BugArrayOffset\Foo2::__construct() throws checked exception BugArrayOffset\ParameterNotFoundException but it's missing from the PHPDoc @throws tag.",
27,
],
[
"Method BugArrayOffset\Foo3::__construct() throws checked exception BugArrayOffset\ParameterNotFoundException but it's missing from the PHPDoc @throws tag.",
35,
],
[
"Method BugArrayOffset\Foo4::__construct() throws checked exception BugArrayOffset\ParameterNotFoundException but it's missing from the PHPDoc @throws tag.",
43,
],
]);
}

}
45 changes: 45 additions & 0 deletions tests/PHPStan/Rules/Exceptions/data/bug-array-offset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace BugArrayOffset;

class ParameterNotFoundException extends \Exception {}

interface Container
{
/**
* @throws ParameterNotFoundException
*/
public function getParameter(string $key): mixed;
}

class Foo
{
public function __construct(Container $container)
{
$container->getParameter('shipmonkDeadCode')['debug']['usagesOf'];
}
}

class Foo2
{
public function __construct(Container $container)
{
isset($container->getParameter('shipmonkDeadCode')['debug']['usagesOf']);
}
}

class Foo3
{
public function __construct(Container $container)
{
unset($container->getParameter('shipmonkDeadCode')['debug']['usagesOf']);
}
}

class Foo4
{
public function __construct(Container $container)
{
$container->getParameter('shipmonkDeadCode')['debug']['usagesOf'] = 42;
}
}
Loading