saschaegerer / phpstan-typo3
TYPO3 CMS class reflection extension for PHPStan
Fund package maintenance!
sascha-egerer
Liberapay
Installs: 2 057 333
Dependents: 258
Suggesters: 0
Security: 0
Stars: 42
Watchers: 6
Forks: 22
Open Issues: 8
Type:phpstan-extension
Requires
- php: ^7.4 || ^8.0
- ext-simplexml: *
- bnf/phpstan-psr-container: ^1.0
- composer/semver: ^3.3
- phpstan/phpstan: ^1.10.9
- ssch/typo3-debug-dump-pass: ^0.0.2
- typo3/cms-core: ^11.5 || ^12.4 || ^13.0
- typo3/cms-extbase: ^11.5 || ^12.4 || ^13.0
Requires (Dev)
- consistence-community/coding-standard: ^3.10
- dealerdirect/phpcodesniffer-composer-installer: ^1.0
- nikic/php-parser: ^v4.19.1
- phing/phing: ^2.17
- php-parallel-lint/php-parallel-lint: ^1.4
- phpstan/phpstan-phpunit: ^1.3
- phpstan/phpstan-strict-rules: ^1.5
- phpunit/phpunit: ^9.6
- symfony/polyfill-php80: ^1.29
- dev-master
- 1.10.2
- 1.10.1
- 1.10.0
- 1.9.1
- 1.9.0
- 1.8.9
- 1.8.5
- 1.8.4
- 1.8.3
- 1.8.2
- 1.8.1
- 1.8.0
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0.0
- 0.13.3
- 0.13.1
- 0.13.0
- 0.12.3
- 0.12.2
- 0.12.1
- 0.12.0
- 0.11.0
- 0.10.1
- 0.10.0
- dev-bugfix/fix-query-execute-return-type
- dev-phpstan-1.11
- dev-issue-156
- dev-add-typo3-13_1-support
- dev-remove-object-manager-type-extension
- dev-add-type-declarations
- dev-feature/do-not-try-to-find-model-for-abstract-repository
- dev-issue-138
- dev-remove-branch-alias
- dev-issue-111
- dev-feature/drop-php-7-support
- dev-issue-125
- dev-bugfix/fix-fatal-in-queryinterface-d-r-t
- dev-feauture/114
- dev-issue/103-2
- dev-issue/103
- dev-feature/fix-condition
- dev-feature/fix-configurations
- dev-fixDynamicReturnTypeOfRepositoryFindAll
This package is auto-updated.
Last update: 2024-10-26 21:03:59 UTC
README
TYPO3 CMS class reflection extension for PHPStan & framework-specific rules.
This extension provides the following features (!!! not an exhaustive list !!!):
Dynamic Return Type Extensions
- Provides correct return type for
\TYPO3\CMS\Core\Context\Context->getAspect()
. - Provides correct return type for
\TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance()
. - Provides correct return type for
\TYPO3\CMS\Extbase\Object\ObjectManagerInterface->get()
. - Provides correct return type for
\TYPO3\CMS\Extbase\Object\ObjectManager->get()
. - Provides correct return type for
\TYPO3\CMS\Extbase\Property\PropertyMapper->convert()
. - Provides correct return type for
\TYPO3\CMS\Core\Utility\MathUtility
methods like isIntegerInRange. - Provides correct return type for
\TYPO3\CMS\Extbase\Persistence\Generic\Query->execute()
. - Provides correct return type for
\TYPO3\CMS\Extbase\Persistence\QueryInterface->execute()
. - Provides correct return type for
\TYPO3\CMS\Core\Site\Entity\Site->getAttribute()
. - Provides correct return type for
\Psr\Http\Message\ServerRequestInterface->getAttribute()
. - Uses under the hood bnf/phpstan-psr-container
All these dynamic return type extensions are necessary to teach PHPStan what type will be returned by the specific method call.
Show me a practical use case.
For example PHPStan cannot know innately what type will be returned if you call `\TYPO3\CMS\Core\Utility\MathUtility->forceIntegerInRange(1000, 1, 10)`. It will be an int<10>. With the help of this library PHPStan also knows what´s going up.Imagine the following situation in your code:
use TYPO3\CMS\Core\Utility\MathUtility; $integer = MathUtility::forceIntegerInRange(100, 1, 10); if($integer > 10) { throw new \UnexpectedValueException('The integer is too big') }
PHPStan will tell you that the if condition is superfluous, because the variable $integer will never be higher than 10. Right?
Framework specific rules
- Provides rule for
\TYPO3\CMS\Core\Context\Context->getAspect()
. - Provides rule for
\Psr\Http\Message\ServerRequestInterface->getAttribute()
. - Provides rule for
\TYPO3\CMS\Core\Site\Entity\Site->getAttribute()
. - Provides rule for
\TYPO3\CMS\Extbase\Validation\ValidatorResolver->createValidator()
.
Show me a practical use case.
For example PHPStan cannot know innately that calling ValidatorResolver->createValidator(RegularExpressionValidator::class)
is invalid, because we miss to pass the required option regularExpression
.
With the help of this library PHPStan now complaints that we have missed to pass the required option.
So go ahead and find bugs in your code without running it.
Installation & 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');
Check for private Services
You have to provide a path to App_KernelDevelopmentDebugContainer.xml or similar XML file describing your container. This is generated by ssch/typo3-debug-dump-pass in your /var/cache/{TYPO3_CONTEXT}/ folder.
parameters: typo3: containerXmlPath: var/cache/development/App_KernelDevelopmentDebugContainer.xml