PHP code example of cakedc / cakephp-phpstan

1. Go to this page and download the library: Download cakedc/cakephp-phpstan library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

cakedc / cakephp-phpstan example snippets


  // PHPStan now knows the return type is array<int|string, string>
  $roles = $this->Roles->find('list')->toArray();

  // PHPStan now knows the return type is array<int|string, string> (chained)
  $roles = $this->Roles->find('list')->where(['active' => true])->orderBy(['name' => 'ASC'])->toArray();

  // PHPStan now knows the return type is array<int|string, array<int|string, string>> (grouped)
  $roles = $this->Roles->find('list', groupField: 'category_id')->toArray();

  //Now PHPStan know that \App\Models\Table\NotesTable::get returns \App\Model\Entity\Note
  $note = $this->Notes->get(1);
  $note->note = 'My new note';//No error

  //Now PHPStan know that \App\Models\Table\NotesTable::newEntity returns \App\Model\Entity\Note
  $note = $this->Notes->newEntity($data);
  $note->note = 'My new note new entity';//No error

  //Now PHPStan know that \App\Models\Table\NotesTable::newEmptyEntity returns \App\Model\Entity\Note
  $note = $this->Notes->newEmptyEntity($data);
  $note->note = 'My new note new empty entity';//No error

   //Now PHPStan know that \App\Models\Table\NotesTable::findOrCreate returns \App\Model\Entity\Note
  $note = $this->Notes->findOrCreate($data);
  $note->note = 'My entity found or created';//No error

  //Now PHPStan know that \App\Models\Table\NotesTable::newEntities returns \App\Model\Entity\Note[]
  $notes = $this->Notes->newEntities($data);
  foreach ($notes as $note) {
    $note->note = 'My new note';//No error
  }

  //Now PHPStan know that \App\Models\Table\NotesTable::get returns \App\Model\Entity\Note
  $note = $this->Notes->get(1);
  $notes = $this->Notes->newEntities($data);

  //Since PHPStan knows the type of $note, these methods call use the same type as return type:
  $note = $this->Notes->patchEntity($note, $data);
  $text = $note->note;//No error.

  $note = $this->Notes->save($note);
  $text = $note->note;//No error.

  $note = $this->Notes->saveOrFail($note);
  $text = $note->note;//No error.
  //Since PHPStan knows the type of $notes, these methods call use the same type as return type:
  $notes = $this->Notes->patchEntities($notes);
  $notes = $this->Notes->saveMany($notes);
  $notes = $this->Notes->saveManyOrFail($notes);
  $notes = $this->Notes->deleteMany($notes);
  $notes = $this->Notes->deleteManyOrFail($notes);

// Bad - code after render() is unreachable
public function myAction()
{
    $this->render('edit');
    $this->set('data', 'value'); // This will never execute
}

// Good - explicit return prevents confusion
public function myAction()
{
    return $this->render('edit');
}

// Also good - assignment is valid
public function myAction()
{
    $response = $this->render('edit');

    return $response;
}

// Bad - code after redirect() is unreachable
public function myAction()
{
    $this->redirect(['action' => 'index']);
    $this->Flash->success('Done'); // This will never execute
}

// Good - explicit return prevents confusion
public function myAction()
{
    return $this->redirect(['action' => 'index']);
}