phpstan-symfony icon indicating copy to clipboard operation
phpstan-symfony copied to clipboard

ServiceSubscriberInterface, voters and private services issue

Open vandroids opened this issue 4 years ago • 3 comments

Support question

Hello! I'm trying to use ServiceSubscriberInterface after reading this article https://symfonycasts.com/screencast/symfony-doctrine/service-subscriber

They suggest to use service locator pattern for the sake of performance in voters. And the problem is this:

namespace App\Voters;

use Psr\Container\ContainerInterface;
use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Contracts\Service\ServiceSubscriberInterface;

class MyVoter extends Voter implements ServiceSubscriberInterface
{
    /**
     * @var ContainerInterface
     */
    private $container;

    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
    }

    protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
    {
        $this->container->get(AccessDecisionManagerInterface::class)->decide...
    }

    public static function getSubscribedServices()
    {
        return [
            AccessDecisionManagerInterface::class
        ];
    }
}

Phpstan tells me that Service "Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface" is private. But my code actually works without any problems.

Can you please help me to find out what to do in this situation?

vandroids avatar Jul 17 '20 13:07 vandroids

Just bumped into same issue, seems that https://github.com/phpstan/phpstan-symfony/commit/c340de842c656bdf5ea01eed07fce58ceb6eeb61 wasn't a full fix

malarzm avatar Jun 16 '21 15:06 malarzm

@malarzm there is old deprecated interface Symfony\Component\DependencyInjection\ServiceSubscriberInterface in the commit https://github.com/phpstan/phpstan-symfony/commit/c340de842c656bdf5ea01eed07fce58ceb6eeb61 . It has been removed in symfony 4, AFAIR. For now it is Symfony\Contracts\Service\ServiceSubscriberInterface

specdrum-agc avatar Jan 04 '23 10:01 specdrum-agc

@specdrum-agc thanks for the ping! From what I can see we were originally using the correct interface (god bless GitHub showing links in issues from my private repositories). But also nowadays we no longer have the error in our baseline so I reckon this was fixed somewhen in the past by the way :)

malarzm avatar Jan 05 '23 08:01 malarzm