PHP code example of schnitzler / phpstan-typo3-extbase

1. Go to this page and download the library: Download schnitzler/phpstan-typo3-extbase 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/ */

    

schnitzler / phpstan-typo3-extbase example snippets


class Item extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity {}

/**
 * @template TEntityClass of Item
 * @template-extends Repository<TEntityClass>
 */
class ItemRepository extends \TYPO3\CMS\Extbase\Persistence\Repository {}

class ItemController
{
    private ItemRepository $itemRepository;

    public function listAction()
    {
        # this call does not longer generate a "call getFirst() on array" error
        $this->itemRepository->findAll()->getFirst();

        # PHPStan does now understand that this is a collection of Item classes.
        # This is made possible with @template and @template-extends annotations on your repositories
        $items = $this->itemRepository->findAll();

        foreach($this->itemRepository->findAll() as $item) {
            # PHPStan does now know that $item is an instance of Item which does not have method getFoo() defined.
            $item->getFoo();
        }

        # PHPStan does now know that $item can be null
        $item = $this->itemRepository->findByUid(1);

        #PHPStan does no longer report an error for such comparisons due to the former detection
        if ($item !== null) {
        }

        # PHPStan understands that $query deals with Item objects
        $query = $this->itemRepository->createQuery();

        # PHPStan does now know that execute returns a QueryResult<Item> with the argument set to false.
        $queryResult = $query->execute(false);

        # PHPStan does now know that execute returns an array of arrays (array<int,array<string,mixed>>) with the argument set to true.
        $array = $query->execute(true);
    }
}