Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Property $app with generic class Slim\App does not specify its types: TContainerInterface #3329

Closed
j0k3r opened this issue Jun 14, 2024 · 2 comments

Comments

@j0k3r
Copy link
Contributor

j0k3r commented Jun 14, 2024

Following this PR #3322, PHPStan is yelling on one of our project.

The error is:

Property Tests\OurProject\BaseTestCase::$app with generic class Slim\App does not specify its types: TContainerInterface

Here is the code:

<?php

namespace Tests\OurProject;

use PHPUnit\Framework\TestCase;

class BaseTestCase extends TestCase
{
    /** @var \Slim\App */
    protected $app;

    protected function setUpApp(bool $mockedClient = false): void
    {
        $this->app = require __DIR__ . '/../src/App.php';

PHPStan throw that error on the line protected $app;.
What type or change I need to apply to resolve it?

Poke @limarkxx

@akrabat
Copy link
Member

akrabat commented Jun 14, 2024

As App has a getContainer(): ?ContainerInterface method, the generic doc block enables you to specify what type is actually returned when this method is called. i.e. if you are using PHPDI and write:

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

PHPStan has no way of knowing that $container is an instance of PHPDI which has a getKnownEntryNames() method and so will complain.

To tell PHPStan this we change: /** @var \Slim\App $app */ to /** @var \Slim\App<DI\Container> $app */ and now PHPStan knows this.

If you want to use just PSR ContainerInterface methods, then you hint as /** @var \Slim\App<\Psr\Container\ContainerInterface> $app */

If you are not using a container at all, then hint with: /** @var \Slim\App<null> $app */

@j0k3r
Copy link
Contributor Author

j0k3r commented Jun 14, 2024

Awesome, thanks for that great explanation.
I'm not so familiar with template & generics.

@j0k3r j0k3r closed this as completed Jun 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants