Back to Slim

Slim 4.14.0 released

_posts/2024-06-13-slim-4.14.0-release.md

latest1.6 KB
Original Source

We are pleased to have released Slim 4.14.0. As a mature release, there aren't many changes and all are minor and can be seen here.

Type hinting with template generics

This release introduces of template generic docblocks into Slim.

As <tt>Slim\App</tt> has a <tt>getContainer(): ?ContainerInterface</tt> 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 and have this code:

$container = $this->app->getContainer();
$entries = $container->getKnownEntryNames();

Psalm or PHPStan have no way of knowing that <tt>$container</tt> is an instance of <tt>DI\Container</tt> which has a <tt>getKnownEntryNames()</tt> method and so will complain.

To inform the static analyzer that we created <tt>Slim\App</tt> with PHP-DI, we change: <tt>/** @var \Slim\App $app /</tt> to <tt>/* @var \Slim\App<DI\Container> $app */</tt> and now PHPStan knows that <tt>getKnownEntryNames()</tt> is a valid method call on <tt>$container</tt>.

Update your type hints

For your codebase, if you type hint <tt>Slim\App</tt> instance variables using <tt>/** @var \Slim\App $app */</tt>, then you will need to change it to either:

  • <tt>/** @var \Slim\App<null> $app */</tt> if you are not using a DI container, or
  • <tt>/** @var \Slim\App<\Psr\Container\ContainerInterface> $app */</tt> if you are.

As noted above, you can also type hint to the concrete instance of the container you are using too.