Skip to content

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

@LordSimal

Description

@LordSimal

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions