/phpstan-typo3

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

Primary LanguagePHPMIT LicenseMIT

TYPO3 extension for PHPStan

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