|
1 | | -# TYPO3 extension for PHPStan |
| 1 | +# PHPStan TYPO3 extensions and rules |
2 | 2 |
|
3 | | -TYPO3 CMS class reflection extension for PHPStan & framework-specific rules |
| 3 | +TYPO3 CMS class reflection extension for PHPStan & framework-specific rules. |
4 | 4 |
|
5 | | -[](https://travis-ci.org/sascha-egerer/phpstan-typo3) |
| 5 | +[](https://github.com/sascha-egerer/phpstan-typo3/actions) |
6 | 6 |
|
7 | | -## Configuration |
| 7 | +* [PHPStan](https://phpstan.org/) |
| 8 | + |
| 9 | +This extension provides the following features (!!! not an exhaustive list !!!): |
| 10 | + |
| 11 | +**Dynamic Return Type Extensions** |
| 12 | +* Provides correct return type for `\TYPO3\CMS\Core\Context\Context->getAspect()`. |
| 13 | +* Provides correct return type for `\TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance()`. |
| 14 | +* Provides correct return type for `\TYPO3\CMS\Extbase\Object\ObjectManagerInterface->get()`. |
| 15 | +* Provides correct return type for `\TYPO3\CMS\Extbase\Object\ObjectManager->get()`. |
| 16 | +* Provides correct return type for `\TYPO3\CMS\Extbase\Property\PropertyMapper->convert()`. |
| 17 | +* Provides correct return type for `\TYPO3\CMS\Core\Utility\MathUtility` methods like isIntegerInRange. |
| 18 | +* Provides correct return type for `\TYPO3\CMS\Extbase\Persistence\Generic\Query->execute()`. |
| 19 | +* Provides correct return type for `\TYPO3\CMS\Extbase\Persistence\QueryInterface->execute()`. |
| 20 | +* Provides correct return type for `\TYPO3\CMS\Core\Site\Entity\Site->getAttribute()`. |
| 21 | +* Provides correct return type for `\Psr\Http\Message\ServerRequestInterface->getAttribute()`. |
| 22 | +* Uses under the hood [bnf/phpstan-psr-container](https://github.com/bnf/phpstan-psr-container) |
| 23 | + |
| 24 | +All these dynamic return type extensions are necessary to teach PHPStan what type will be returned by the specific method call. |
| 25 | + |
| 26 | +<details> |
| 27 | +<summary>Show me a practical use case.</summary> |
| 28 | +For example PHPStan cannot know innately what type will be returned if you call `\TYPO3\CMS\Core\Utility\MathUtility->forceIntegerInRange(1000, 1, 10)`. |
| 29 | +It will be an int<10>. With the help of this library PHPStan also knows what´s going up. |
| 30 | + |
| 31 | +Imagine the following situation in your code: |
| 32 | + |
| 33 | +```php |
| 34 | + |
| 35 | +use TYPO3\CMS\Core\Utility\MathUtility; |
| 36 | + |
| 37 | +$integer = MathUtility::forceIntegerInRange(100, 1, 10); |
| 38 | + |
| 39 | +if($integer > 10) { |
| 40 | + throw new \UnexpectedValueException('The integer is too big') |
| 41 | +} |
| 42 | +``` |
| 43 | + |
| 44 | +PHPStan wil tell you that the if condition is superfluous, because the variable $integer will never be higher than 10. Right? |
| 45 | +</details> |
| 46 | + |
| 47 | +**Framework specific rules** |
| 48 | +* Provides rule for `\TYPO3\CMS\Core\Context\Context->getAspect()`. |
| 49 | +* Provides rule for `\Psr\Http\Message\ServerRequestInterface->getAttribute()`. |
| 50 | +* Provides rule for `\TYPO3\CMS\Core\Site\Entity\Site->getAttribute()`. |
| 51 | +* Provides rule for `\TYPO3\CMS\Extbase\Validation\ValidatorResolver->createValidator()`. |
| 52 | + |
| 53 | +<details> |
| 54 | +<summary>Show me a practical use case.</summary> |
| 55 | + |
| 56 | +For example PHPStan cannot know innately that calling `ValidatorResolver->createValidator(RegularExpressionValidator::class)` is invalid, because we miss to pass the required option `regularExpression`. |
| 57 | +With the help of this library PHPStan now complaints that we have missed to pass the required option. |
| 58 | +So go ahead and find bugs in your code without running it. |
| 59 | + |
| 60 | +</details> |
| 61 | + |
| 62 | + |
| 63 | +## Installation & Configuration |
8 | 64 |
|
9 | 65 | To use this extension, require it in [Composer](https://getcomposer.org/): |
10 | 66 |
|
|
0 commit comments