cakephp-phpstan icon indicating copy to clipboard operation
cakephp-phpstan copied to clipboard

Dead catch - Cake\Datasource\Exception\RecordNotFoundException is never thrown in the try block.

Open LordSimal opened this issue 5 months ago • 5 comments

I have this code in my controller:

$optionsTable = $this->fetchTable('Options');
try {
    $ports = $optionsTable->get('mykey');
} catch (RecordNotFoundException) {
    $ports = new Option(['alfred_key' => 'mykey', 'value' => '']);
}

but phpstan gives me the following error:

 ------ -------------------------------------------------------------------------------------------------- 
  Line   plugins/AlfredTools/src/Controller/ToolsController.php                                            
 ------ -------------------------------------------------------------------------------------------------- 
  620    Dead catch - Cake\Datasource\Exception\RecordNotFoundException is never thrown in the try block.  
         🪪  catch.neverThrown                                                                             
 ------ -------------------------------------------------------------------------------------------------- 

I do have the following phpdoc at the top of my OptionsTable due to the ide helper plugin:

 * @method \App\Model\Entity\Option get(mixed $primaryKey, array|string $finder = 'all', \Psr\SimpleCache\CacheInterface|string|null $cache = null, \Closure|string|null $cacheKey = null, mixed ...$args)

I guess phpstan doesn't understand, that this extends the already existing \Cake\ORM\Table::get() method and therefore doesn't realize, that this method can throw either \Cake\Datasource\Exception\RecordNotFoundException or \Cake\Datasource\Exception\InvalidPrimaryKeyException

LordSimal avatar Jul 28 '25 07:07 LordSimal