I’m using Slim/4.11.0 and PHP-DI/7.0.2 (this one perhaps incorrectly, because I rushed to install it when I couldn’t make Pimple work after a migration). My application works smoothly. But when I run PHPStan with level 5 I get this message from my routing file:
Parameter #1 $config of class Alvaro\Paginas\Error constructor expects Alvaro\Config\Config, Psr\Container\ContainerInterface|null given.
I have a custom class for my settings, which is what my constructor expects and uses:
public function __construct(Config $config)
{
$this->config = $config;
}
But PHPStan doesn’t know the specific type because it comes from the container:
/** @var \Alvaro\Config\Config $config */
$config = require __DIR__ . '/../bootstrap.php';
AppFactory::setContainer($config);
$app = AppFactory::create();
$errorMiddleware->setErrorHandler(HttpNotFoundException::class, function (Request $request) use ($app) {
return (new Paginas\Error($app->getContainer()))
->notFound404($request, $app->getResponseFactory()->createResponse());
});
public static function setContainer(ContainerInterface $container): void
{
static::$container = $container;
}
public function getContainer(): ?ContainerInterface
{
return $this->container;
}
I’ve a couple of workarounds, but they don’t serve any real purpose other than making PHPStan happy. For that, I could just ignore the error. After all, I’m using PHPStan to make my code better.
$errorMiddleware->setErrorHandler(HttpNotFoundException::class, function (Request $request) use ($app, $config) {
return (new Paginas\Error($config))
->notFound404($request, $app->getResponseFactory()->createResponse());
});
$errorMiddleware->setErrorHandler(HttpNotFoundException::class, function (Request $request) use ($app) {
/** @var Config $config */
$config = $app->getContainer();
return (new Paginas\Error($config))
->notFound404($request, $app->getResponseFactory()->createResponse());
});
I wonder if I’m missing something more straightforward, or if my app set up can be improved in some way.