diff --git a/_posts/2024-06-13-slim-4.14.0-release.md b/_posts/2024-06-13-slim-4.14.0-release.md index 53abbb28..ffe62dd2 100644 --- a/_posts/2024-06-13-slim-4.14.0-release.md +++ b/_posts/2024-06-13-slim-4.14.0-release.md @@ -9,9 +9,26 @@ can be seen [here](https://github.com/slimphp/Slim/releases/tag/4.14.0). ## Type hinting with template generics -This release introduces of [template generic docblocks](https://phpstan.org/blog/generics-in-php-using-phpdocs) into Slim. If you type-hint `Slim\App` instance variable using `/** @var \Slim\App $app */`, then you will need to change it to either: +This release introduces of [template generic docblocks](https://phpstan.org/blog/generics-in-php-using-phpdocs) into Slim. -* `/** @var \Slim\App $app */` if you are not using a DI container, or -* `/** @var \Slim\App<\Psr\Container\ContainerInterface> $app */` if you are. +As Slim\App has a getContainer(): ?ContainerInterface method, the generics docblock enables you to specify what type is actually returned when this method is called. +i.e. consider that you are using [PHP-DI](https://php-di.org) and have this code: -You can also type-hint to the concrete instance of the container you are using too. For example, if you are using [PHP-DI](https://php-di.org), then you can use: `/** @var \Slim\App $app */`. +``` +$container = $this->app->getContainer(); +$entries = $container->getKnownEntryNames(); +``` + +[PHPStan](https://phpstan.org/) has no way of knowing that $container is an instance ofDI\Container which has a getKnownEntryNames() method and so will complain. + +To inform PHPStan that we create Slim\App with PHP-DI, we change: /** @var \Slim\App $app */ to /** @var \Slim\App $app */ and now PHPStan knows that getKnownEntryNames() is a valid method call on $container. + + +### Update your type hints + +For your codebase, if you type hint Slim\App instance variable using /** @var \Slim\App $app */, then you will need to change it to either: + +* /** @var \Slim\App $app */ if you are not using a DI container, or +* /** @var \Slim\App<\Psr\Container\ContainerInterface> $app */ if you are. + +As noted above, you can also type hint to the concrete instance of the container you are using too.