saschaegerer/phpstan-typo3

TYPO3 CMS class reflection extension for PHPStan

Fund package maintenance!
sascha-egerer
Liberapay

Installs: 757 775

Dependents: 120

Suggesters: 0

Security: 0

Stars: 34

Watchers: 6

Forks: 17

Open Issues: 3

Type:phpstan-extension

1.8.2 2022-11-06 09:11 UTC

README

TYPO3 CMS class reflection extension for PHPStan & framework-specific rules

Build Status

Configuration

To use this extension, require it in Composer:

composer require --dev saschaegerer/phpstan-typo3

If you also install phpstan/extension-installer then you're all set!

Manual installation

If you don't want to use phpstan/extension-installer, put this into your phpstan.neon config:

includes:
    - vendor/saschaegerer/phpstan-typo3/extension.neon

Custom Context API Aspects

If you use custom aspects for the TYPO3 Context API you can add a mapping so PHPStan knows what type of aspect class is returned by the context API

parameters:
    typo3:
        contextApiGetAspectMapping:
            myCustomAspect: FlowdGmbh\MyProject\Context\MyCustomAspect
// PHPStan will now know that $myCustomAspect is of type FlowdGmbh\MyProject\Context\MyCustomAspect
$myCustomAspect = GeneralUtility::makeInstance(Context::class)->getAspect('myCustomAspect');

Custom Request Attribute

If you use custom PSR-7 request attribute you can add a mapping so PHPStan knows what type of class is returned by Request::getAttribute()

parameters:
    typo3:
        requestGetAttributeMapping:
            myAttribute: FlowdGmbh\MyProject\Http\MyAttribute
            myNullableAttribute: FlowdGmbh\MyProject\Http\MyAttribute|null
// PHPStan will now know that $myAttribute is of type FlowdGmbh\MyProject\Http\MyAttribute
$myAttribute = $request->getAttribute('myAttribute');

Custom Site Attribute

If you use custom attributes for the TYPO3 Site API you can add a mapping so PHPStan knows what type is returned by the site API

parameters:
    typo3:
        siteGetAttributeMapping:
            myArrayAttribute: array
            myIntAttribute: int
            myStringAttribute: string
$site = $this->request->getAttribute('site');

// PHPStan will now know that $myArrayAttribute is of type array<mixed, mixed>
$myArrayAttribute = $site->getAttribute('myArrayAttribute');

// PHPStan will now know that $myIntAttribute is of type int
$myIntAttribute = $site->getAttribute('myIntAttribute');

// PHPStan will now know that $myStringAttribute is of type string
$myStringAttribute = $site->getAttribute('myStringAttribute');