Search code examples
cakephpcakephp-4.xphpstan

PHPstan: Cannot access property $work on array|EntityInterface|null


I am developing a web app, trying to keep PHPstan's suggestions in check.

I am having some difficulties with this method:

/**
 * AJAX: deletes a work file
 *
 * @return \Cake\Http\Response|false
 */
public function delete()
{
    $this->autoRender = false;
    $this->viewBuilder()->setLayout('ajax');
    $this->request->allowMethod(['post', 'delete']);

    $data = $this->request->getData();
    $data = is_array($data) ? $data : [$data];

    $workFile = $this->WorkFiles->find('all')
        ->where(['WorkFiles.id' => $data['id']])
        ->contain(['Works'])
        ->first();

    $res = [
        'status' => 'error',
        'message' => __('The file could not be deleted. Please, try again.'),
        'class' => 'alert-error',
    ];

    if ($workFile->work->anagraphic_id == $this->authAnagraphics['id']) { // error #1
        if ($this->WorkFiles->delete($workFile)) { // error #2
            $res = [
                'status' => 'success',
                'message' => __('File successfully deleted.'),
                'class' => 'alert-success',
            ];
        }
    }

    return $this->response->withStringBody((string)json_encode($res));
}

The code itself works, but I'm having two phpstan errors:

  1. [phpstan] Cannot access property $work on array|Cake\Datasource\EntityInterface|null.
  2. [phpstan] Parameter #1 $entity of method Cake\ORM\Table::delete() expects Cake\Datasource\EntityInterface, array|Cake\Datasource\EntityInterface|null given.

Am I doing something wrong?


Solution

  • Always use inline annotation then here:

    /** @var \App\Model\Entity\WorkFile|null $workFile */
    $workFile = $this->WorkFiles->find('all')
        ->where(['WorkFiles.id' => $data['id']])
        ->contain(['Works'])
        ->first();
    

    But the comments are right, you are blindly using a possible null value afterwards, as such your code is not written correctly.

    Use this instead:

    /** @var \App\Model\Entity\WorkFile $workFile */
    $workFile = $this->WorkFiles->find('all')
        ->where(['WorkFiles.id' => $data['id']])
        ->contain(['Works'])
        ->firstOrFail();