Task #98097
closedAdd type hints to all GeneralUtility::makeInstance calls
0%
Description
Hi,
This is a change request to make it easier for PHPStan to check the code and IDEs to give helpful hints.
As an example
public/typo3/sysext/backend/Classes/Controller/EditDocumentController.php
Line 541
$tce = GeneralUtility::makeInstance(DataHandler::class);
this should be changed to
/** @var DataHandler $tce */
$tce = GeneralUtility::makeInstance(DataHandler::class);
And I would like to suggest a change for coding styles like this.
Same file line 1079
$file = GeneralUtility::makeInstance(ResourceFactory::class)->getFileObject($fileUid);
This should be change in general to something like
/** @var ResourceFactory $resourceFactory */
$resourceFactory = GeneralUtility::makeInstance(ResourceFactory::class);
$file = $resourceFactory->getFileObject($fileUid);
Only this way PHPStan and your IDE knows what to expect and navigating from one file to the next is just a click away.
Thanks
Updated by Gerrit Code Review over 2 years ago
- Status changed from New to Under Review
Patch set 1 for branch main of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/c/Packages/TYPO3.CMS/+/75395
Updated by Gerrit Code Review over 2 years ago
Patch set 2 for branch main of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/c/Packages/TYPO3.CMS/+/75395
Updated by Stefan Bürk over 2 years ago
Can this issue be closed ? As the patch is abandoned.
Generally, the core ships with phpstorm metadata, and also a phpstan extension is used (in core) which exactly helps with this. If you need this for phpstan in your project, you should also require the corresponding phpstan extension friendsoftypo3/phpstan-typo3
Maybe VS Code has some similar to PHPStorm meta files or the DynamicType plugin file .. if yes, we may add these along to the core. But type annotation comments has been removed actively, also to avoid eventually wrongly overriden or missinterpeted annotations.
Updated by Christian Kuhn over 2 years ago
- Status changed from Under Review to Resolved
Yeah.
I guess we can close here: The core monorepo .phpstorm.meta.php file should make phpstorm understand the generics. I assume other IDE's either understand that file as well (maybe with a plugin), or, if they want some other file, we could add them as well, of course.
For phpstan, the plugins should solve it.
Updated by JAKOTA Design Group GmbH over 2 years ago
Thanks a lot, PHPStorm meta file is exactly the right way to do this.
The naming is a bit misleading, but works as intended.
Although I had to add this line to the .phpstorm.meta.php to get it to work.
override(\TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(0, 1), map(['' => '@']));