Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add failing test case for implicit exception in ArrayAccess #1054

Merged
merged 4 commits into from
Mar 9, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3286,6 +3286,16 @@ private function processAssignVar(
$nodeCallback(new PropertyAssignNode($var, $assignedPropertyExpr, $isAssignOp), $scope);
}
}

if (!(new ObjectType(ArrayAccess::class))->isSuperTypeOf($varType)->no()) {
$throwPoints = array_merge($throwPoints, $this->processExprNode(
new MethodCall($var, 'offsetSet'),
$scope,
static function (): void {
},
$context,
)->getThrowPoints());
}
} elseif ($var instanceof PropertyFetch) {
$objectResult = $this->processExprNode($var->var, $scope, $nodeCallback, $context);
$hasYield = $objectResult->hasYield();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,24 @@ public function testFirstClassCallables(): void
]);
}

public function testBug4852(): void
{
if (!self::$useStaticReflectionProvider) {
$this->markTestSkipped('This test needs static reflection');
}

$this->analyse([__DIR__ . '/data/bug-4852.php'], [
[
'Dead catch - Exception is never thrown in the try block.',
70,
],
[
'Dead catch - Exception is never thrown in the try block.',
77,
],
]);
}

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

namespace Bug4852;

use ArrayAccess;
use Exception;

final class DefinitelyThrows implements ArrayAccess
{
public function offsetExists ($offset) {}
public function offsetGet ($offset) {}

/**
* @throws Exception
*/
public function offsetSet ($offset , $value) {
throw new Exception();
}
public function offsetUnset ($offset) {}
}

final class MaybeThrows1 implements ArrayAccess
{
public function offsetExists ($offset) {}
public function offsetGet ($offset) {}
public function offsetSet ($offset , $value) {
throw new Exception();
}
public function offsetUnset ($offset) {}
}

class MaybeThrows2 {}

final class DefinitelyNoThrows implements ArrayAccess
{
public function offsetExists ($offset) {}
public function offsetGet ($offset) {}

/**
* @throws void
*/
public function offsetSet ($offset , $value) {}
public function offsetUnset ($offset) {}
}

$foo = new DefinitelyThrows();
try {
$foo[] = 'value';
} catch (Exception $e) {
// not dead
}

$bar = new MaybeThrows1();
try {
$bar[] = 'value';
} catch (Exception $e) {
// not dead
}

$buz = new MaybeThrows2();
try {
$buz[] = 'value';
} catch (Exception $e) {
// not dead
}

$baz = new DefinitelyNoThrows();
try {
$baz[] = 'value';
} catch (Exception $e) {
// dead
}

$array = [];
try {
$array[] = 'value';
} catch (Exception $e) {
// dead
}