Skip to content

Commit

Permalink
add failing test case for implicit exception in ArrayAccess
Browse files Browse the repository at this point in the history
  • Loading branch information
rajyan committed Mar 7, 2022
1 parent 4450e46 commit 4bec6cf
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,16 @@ public function testFirstClassCallables(): void
]);
}

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

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

0 comments on commit 4bec6cf

Please sign in to comment.