schnitzler/phpstan-typo3-extbase

支持使用Extbase的PHPStan包

0.1.1 2021-10-07 08:49 UTC

This package is auto-updated.

Last update: 2024-09-07 16:00:11 UTC


README

此包提供了一些存根和服务,以使您在使用PHPStan和Extbase时更轻松。

示例

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);
    }
}

安装

composer require schnitzler/phpstan-typo3-extbase
# phpstan.neon

includes:
	- vendor/schnitzler/phpstan-typo3-extbase/extension.neon

为什么还要另一个包?

因为对我来说,按照自己的节奏工作更方便。此外,这个包的功能还不完整。我与其他包的维护者保持联系,并会继续这样做,也许有一天会有一个包满足所有TYPO3的需求。但至少现在,这是可行的途径。