Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[DependencyInjection] Add CheckAliasValidityPass to check interface compatibility #50745

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

Merged
merged 1 commit into from
Feb 6, 2024
Merged
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
1 change: 1 addition & 0 deletions src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ CHANGELOG
7.1
---

* Add `CheckAliasValidityPass` to `lint:container` command
* Add `private_ranges` as a shortcut for private IP address ranges to the `trusted_proxies` option
* Mark classes `ConfigBuilderCacheWarmer`, `Router`, `SerializerCacheWarmer`, `TranslationsCacheWarmer`, `Translator` and `ValidatorCacheWarmer` as `final`
* Move the Router `cache_dir` to `kernel.build_dir`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\DependencyInjection\Compiler\CheckAliasValidityPass;
use Symfony\Component\DependencyInjection\Compiler\CheckTypeDeclarationsPass;
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
use Symfony\Component\DependencyInjection\Compiler\ResolveFactoryClassPass;
Expand Down Expand Up @@ -107,6 +108,7 @@ private function getContainerBuilder(): ContainerBuilder
$container->setParameter('container.build_hash', 'lint_container');
$container->setParameter('container.build_id', 'lint_container');

$container->addCompilerPass(new CheckAliasValidityPass(), PassConfig::TYPE_BEFORE_REMOVING, -100);
$container->addCompilerPass(new CheckTypeDeclarationsPass(true), PassConfig::TYPE_AFTER_REMOVING, -100);

return $this->container = $container;
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/DependencyInjection/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ CHANGELOG
7.1
---

* Add `CheckAliasValidityPass` to check service compatibility with aliased interface
* Add argument `$prepend` to `ContainerConfigurator::extension()` to prepend the configuration instead of appending it
* Have `ServiceLocator` implement `ServiceCollectionInterface`
* Add `#[Lazy]` attribute as shortcut for `#[Autowire(lazy: [bool|string])]` and `#[Autoconfigure(lazy: [bool|string])]`
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;

/**
* This pass validates aliases, it provides the following checks:
*
* - An alias which happens to be an interface must resolve to a service implementing this interface. This ensures injecting the aliased interface won't cause a type error at runtime.
*/
class CheckAliasValidityPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container): void
{
foreach ($container->getAliases() as $id => $alias) {
try {
if (!$container->hasDefinition((string) $alias)) {
continue;
}

$target = $container->getDefinition((string) $alias);
if (null === $target->getClass() || null !== $target->getFactory()) {
continue;
}

$reflection = $container->getReflectionClass($id);
if (null === $reflection || !$reflection->isInterface()) {
continue;
}

$targetReflection = $container->getReflectionClass($target->getClass());
if (null !== $targetReflection && !$targetReflection->implementsInterface($id)) {
throw new RuntimeException(sprintf('Invalid alias definition: alias "%s" is referencing class "%s" but this class does not implement "%s". Because this alias is an interface, "%s" must implement "%s".', $id, $target->getClass(), $id, $target->getClass(), $id));
}
} catch (\ReflectionException) {
continue;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\DependencyInjection\Tests\Compiler;

use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\Compiler\CheckAliasValidityPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Tests\Fixtures\CheckAliasValidityPass\FooImplementing;
use Symfony\Component\DependencyInjection\Tests\Fixtures\CheckAliasValidityPass\FooInterface;
use Symfony\Component\DependencyInjection\Tests\Fixtures\CheckAliasValidityPass\FooNotImplementing;

class CheckAliasValidityPassTest extends TestCase
{
public function testProcessDetectsClassNotImplementingAliasedInterface()
{
$this->expectException(RuntimeException::class);
$container = new ContainerBuilder();
$container->register('a')->setClass(FooNotImplementing::class);
$container->setAlias(FooInterface::class, 'a');

$this->process($container);
}

public function testProcessAcceptsClassImplementingAliasedInterface()
{
$container = new ContainerBuilder();
$container->register('a')->setClass(FooImplementing::class);
$container->setAlias(FooInterface::class, 'a');

$this->process($container);
$this->addToAssertionCount(1);
}

public function testProcessIgnoresArbitraryAlias()
{
$container = new ContainerBuilder();
$container->register('a')->setClass(FooImplementing::class);
$container->setAlias('not_an_interface', 'a');

$this->process($container);
$this->addToAssertionCount(1);
}

public function testProcessIgnoresTargetWithFactory()
{
$container = new ContainerBuilder();
$container->register('a')->setFactory(new Reference('foo'));
$container->setAlias(FooInterface::class, 'a');

$this->process($container);
$this->addToAssertionCount(1);
}

public function testProcessIgnoresTargetWithoutClass()
{
$container = new ContainerBuilder();
$container->register('a');
$container->setAlias(FooInterface::class, 'a');

$this->process($container);
$this->addToAssertionCount(1);
}

protected function process(ContainerBuilder $container): void
{
$pass = new CheckAliasValidityPass();
$pass->process($container);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Symfony\Component\DependencyInjection\Tests\Fixtures\CheckAliasValidityPass;

class FooImplementing implements FooInterface
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Symfony\Component\DependencyInjection\Tests\Fixtures\CheckAliasValidityPass;

interface FooInterface
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Symfony\Component\DependencyInjection\Tests\Fixtures\CheckAliasValidityPass;

class FooNotImplementing
{

}