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

$environment->getContext triggers the Psalm issue UndefinedInterfaceMethod #141

Open
Kwadz opened this issue Sep 21, 2021 · 2 comments
Open

Comments

@Kwadz
Copy link

Kwadz commented Sep 21, 2021

The example about accessing contexts from each other triggers:

  • the Psalm issue UndefinedInterfaceMethod
  • the PHPStorm issue "Potentially polymorphic call. The code may be inoperable depending on the actual class instance passed as the argument"

Indeed, the method getContext() is not necessarily present in every possible Environment interface implementation since it is not declared in this interface. Actually it is declared in InitializedContextEnvironment only.

The piece of code:

use Behat\Behat\Context\Context;
use Behat\Behat\Hook\Scope\BeforeScenarioScope;

class FeatureContext implements Context
{
    /** @var \Behat\MinkExtension\Context\MinkContext */
    private $minkContext;

    /** @BeforeScenario */
    public function gatherContexts(BeforeScenarioScope $scope)
    {
        $environment = $scope->getEnvironment();

        $this->minkContext = $environment->getContext('Behat\MinkExtension\Context\MinkContext');
    }
}
@stof
Copy link
Member

stof commented Sep 22, 2021

This might be solved by improving types based on the different scopes. The BeforeScenarioScope is guaranteed to get an environment implementing Behat\Behat\Context\Environment\ContextEnvironment, and I think even an InitializedContextEnvironment for that one (for BeforeSuite, that might not be an InitializedContextEnvironment, but it would still be a ContextEnvironment)

@pamil
Copy link
Member

pamil commented Dec 13, 2021

getContext public method is available only in specific ContextEnvironment implementations as it's not listed on the interface. FriendsOfBehat/SymfonyExtension implements that interface as well and provides getContext method additionally, but there's still no guarantee any other implementation will have that method.

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

3 participants