Skip to content

Commit

Permalink
Final Exception methods can never throw an exception
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jul 14, 2021
1 parent 1d4835e commit f5e88ae
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 0 deletions.
1 change: 1 addition & 0 deletions conf/config.stubFiles.neon
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ parameters:
- ../stubs/dom.stub
- ../stubs/spl.stub
- ../stubs/SplObjectStorage.stub
- ../stubs/Exception.stub
146 changes: 146 additions & 0 deletions stubs/Exception.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
<?php

interface Throwable
{
/**
* @return string
* @throws void
*/
public function getMessage();

/**
* @return mixed
* @throws void
*/
public function getCode();

/**
* @return string
* @throws void
*/
public function getFile();

/**
* @return int
* @throws void
*/
public function getLine();

/**
* @return mixed[]
* @throws void
*/
public function getTrace();

/**
* @return string
* @throws void
*/
public function getTraceAsString();

/**
* @return null|Throwable
* @throws void
*/
public function getPrevious();

/**
* @return string
* @throws void
*/
public function __toString();
}

class Exception implements Throwable
{

/**
* @return string
* @throws void
*/
final public function getMessage() {}

/**
* @return mixed
* @throws void
*/
final public function getCode() {}

/**
* @return string
* @throws void
*/
final public function getFile() {}

/**
* @return int
* @throws void
*/
final public function getLine() {}

/**
* @return mixed[]
* @throws void
*/
final public function getTrace() {}

/**
* @return null|Throwable
* @throws void
*/
final public function getPrevious() {}

/**
* @return string
* @throws void
*/
final public function getTraceAsString() {}

}

class Error implements Throwable
{

/**
* @return string
* @throws void
*/
final public function getMessage() {}

/**
* @return mixed
* @throws void
*/
final public function getCode() {}

/**
* @return string
* @throws void
*/
final public function getFile() {}

/**
* @return int
* @throws void
*/
final public function getLine() {}

/**
* @return mixed[]
* @throws void
*/
final public function getTrace() {}

/**
* @return null|Throwable
* @throws void
*/
final public function getPrevious() {}

/**
* @return string
* @throws void
*/
final public function getTraceAsString() {}

}
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ public function testRule(): void
'Dead catch - Exception is never thrown in the try block.',
485,
],
[
'Dead catch - Exception is never thrown in the try block.',
532,
],
]);
}

Expand Down
14 changes: 14 additions & 0 deletions tests/PHPStan/Rules/Exceptions/data/unthrown-exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -521,3 +521,17 @@ public function doBaz(string $string): void
}

}

class ExceptionGetMessage
{

public function doFoo(\Exception $e)
{
try {
echo $e->getMessage();
} catch (\Exception $t) {

}
}

}

0 comments on commit f5e88ae

Please sign in to comment.