Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/Stubs/5/Component/DependencyInjection/ServiceLocator.stubphp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Symfony\Component\DependencyInjection;

use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
use Symfony\Contracts\Service\ServiceLocatorTrait;
use Symfony\Contracts\Service\ServiceProviderInterface;
use Symfony\Contracts\Service\ServiceSubscriberInterface;

/**
* @template T
*/
class ServiceLocator implements ServiceProviderInterface
{

/**
* @return T
*/
public function get($id);

/**
* @return array<string, string>
*/
public function getProvidedServices(): array;
}
30 changes: 30 additions & 0 deletions src/Stubs/6/Component/DependencyInjection/ServiceLocator.stubphp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Symfony\Component\DependencyInjection;

use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
use Symfony\Contracts\Service\ServiceLocatorTrait;
use Symfony\Contracts\Service\ServiceProviderInterface;
use Symfony\Contracts\Service\ServiceSubscriberInterface;

/**
* @template T
*/
class ServiceLocator implements ServiceProviderInterface
{

/**
* @return T
*/
public function get($id)
{}

/**
* @return array<string, string>
*/
public function getProvidedServices(): array;
}
71 changes: 71 additions & 0 deletions tests/acceptance/acceptance/ServiceLocator.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
@symfony-5 @symfony-6
Feature: ServiceLocator

Background:
Given I have Symfony plugin enabled

Scenario: ServiceLocator will return tagged service
Given I have the following code
"""
<?php

interface StrategyInterface{}

use Symfony\Component\DependencyInjection\ServiceLocator;

class MyService
{
/** @var ServiceLocator<StrategyInterface> $strategies */
private ServiceLocator $strategies;

/** @param ServiceLocator<StrategyInterface> $strategies */
public function __construct(ServiceLocator $strategies)
{
$this->strategies = $strategies;
}

public function doSomethingWithStrategy(): void
{
$strategy = $this->strategies->get('random_string');
/** @psalm-trace $strategy */
}
}
"""
When I run Psalm
Then I see these errors
| Type | Message |
| Trace | $strategy: StrategyInterface |
And I see no other errors

Scenario: Fixed the return type of getProvidedServices()
Given I have the following code
"""
<?php

interface StrategyInterface{}

use Symfony\Component\DependencyInjection\ServiceLocator;

class MyService
{
/** @var ServiceLocator<StrategyInterface> $strategies */
private ServiceLocator $strategies;

/** @param ServiceLocator<StrategyInterface> $strategies */
public function __construct(ServiceLocator $strategies)
{
$this->strategies = $strategies;
}

public function getAll(): void
{
$names = $this->strategies->getProvidedServices();
/** @psalm-trace $names */
}
}
"""
When I run Psalm
Then I see these errors
| Type | Message |
| Trace | $names: array<string, string> |
And I see no other errors