symfony icon indicating copy to clipboard operation
symfony copied to clipboard

[Cache] Autowiring type-hint for namespaced caches

Open IndraGunawan opened this issue 10 months ago • 0 comments

Symfony version(s) affected

7.3.0

Description

Symfony 7.3 has a great improvement on cache component https://symfony.com/blog/new-in-symfony-7-3-namespaced-caches

additional assert($this->cache instanceof NamespacedPoolInterface); required before calling the $this->cache->withSubNamespace('sub-ns'); even all caches adapters implement NamespacedPoolInterface, otherwise phpstan will complain about call to undefined method

How to reproduce

had tried to use intersection types

public function __construct(
        private CacheInterface&NamespacedPoolInterface $appCache
    )

but get this error

In DefinitionErrorExceptionPass.php line 48:
                                                                                                                                                                                                                      
  [Symfony\Component\DependencyInjection\Exception\RuntimeException]                                                                                                                                                  
  Cannot autowire service "App\Command\TestCommand": argument "$appCache" of method "__construct()" has type "Symfony\Contracts\Cache\CacheInterface&Symfony\Contracts\Cache\NamespacedPoolInterface" but this class  
   was not found.

Possible Solution

No response

Additional Context

No response

IndraGunawan avatar May 29 '25 12:05 IndraGunawan