Skip to content
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
14 changes: 4 additions & 10 deletions config/sets/symfony/composer-based.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

declare(strict_types=1);

use PhpParser\Node\Scalar\String_;
use PHPStan\Type\ArrayType;
use PHPStan\Type\BooleanType;
use PHPStan\Type\Constant\ConstantBooleanType;
Expand Down Expand Up @@ -87,7 +86,7 @@
use Rector\Symfony\Symfony52\Rector\New_\PropertyPathMapperToDataMapperRector;
use Rector\Symfony\Symfony52\Rector\StaticCall\BinaryFileResponseCreateToNewInstanceRector;
use Rector\Symfony\Symfony53\Rector\StaticPropertyFetch\KernelTestCaseContainerPropertyDeprecationRector;
use Rector\Symfony\Symfony60\Rector\FuncCall\ReplaceServiceArgumentRector;
use Rector\Symfony\Symfony60\Rector\FuncCall\ContainerInterfaceServiceToServiceContainerRector;
use Rector\Symfony\Symfony60\Rector\MethodCall\GetHelperControllerToServiceRector;
use Rector\Symfony\Symfony61\Rector\Class_\CommandConfigureToAttributeRector;
use Rector\Symfony\Symfony61\Rector\Class_\CommandPropertyToAttributeRector;
Expand All @@ -113,7 +112,6 @@
use Rector\Symfony\Symfony81\Rector\MethodCall\RenameCopyOnWindowsOptionToFollowSymlinksRector;
use Rector\Symfony\Symfony81\Rector\New_\RemoveEraseCredentialsFromAuthenticatorManagerRector;
use Rector\Symfony\Symfony81\Rector\StaticCall\AddFormatArgumentToIsValidRector;
use Rector\Symfony\ValueObject\ReplaceServiceArgument;
use Rector\Transform\Rector\Attribute\AttributeKeyToClassConstFetchRector;
use Rector\Transform\Rector\ClassMethod\WrapReturnRector;
use Rector\Transform\Rector\StaticCall\StaticCallToNewRector;
Expand Down Expand Up @@ -253,6 +251,9 @@
// symfony/framework-bundle 5.3
KernelTestCaseContainerPropertyDeprecationRector::class,

// symfony/dependency-injection 6.0
ContainerInterfaceServiceToServiceContainerRector::class,

// symfony/framework-bundle 6.0
GetHelperControllerToServiceRector::class,

Expand Down Expand Up @@ -2016,13 +2017,6 @@
new UnionType($scalarTypes)
),
], 'symfony/dependency-injection', '>=6.0');
$rectorConfig->ruleWithConfigurationComposerVersionBound(ReplaceServiceArgumentRector::class, [
new ReplaceServiceArgument('Psr\Container\ContainerInterface', new String_('service_container')),
new ReplaceServiceArgument(
'Symfony\Component\DependencyInjection\ContainerInterface',
new String_('service_container')
),
], 'symfony/dependency-injection', '>=6.0');

// symfony/doctrine-bridge 6.0
$rectorConfig->ruleWithConfigurationComposerVersionBound(RenameMethodRector::class, [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

declare(strict_types=1);

use PhpParser\Node\Scalar\String_;
use PHPStan\Type\ArrayType;
use PHPStan\Type\BooleanType;
use PHPStan\Type\Constant\ConstantBooleanType;
Expand All @@ -15,19 +14,12 @@
use PHPStan\Type\StringType;
use PHPStan\Type\UnionType;
use Rector\Config\RectorConfig;
use Rector\Symfony\Symfony60\Rector\FuncCall\ReplaceServiceArgumentRector;
use Rector\Symfony\ValueObject\ReplaceServiceArgument;
use Rector\Symfony\Symfony60\Rector\FuncCall\ContainerInterfaceServiceToServiceContainerRector;
use Rector\TypeDeclaration\Rector\ClassMethod\AddReturnTypeDeclarationRector;
use Rector\TypeDeclaration\ValueObject\AddReturnTypeDeclaration;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->ruleWithConfiguration(ReplaceServiceArgumentRector::class, [
new ReplaceServiceArgument('Psr\Container\ContainerInterface', new String_('service_container')),
new ReplaceServiceArgument(
'Symfony\Component\DependencyInjection\ContainerInterface',
new String_('service_container')
),
]);
$rectorConfig->rule(ContainerInterfaceServiceToServiceContainerRector::class);

$configurationType = new ObjectType('Symfony\Component\Config\Definition\ConfigurationInterface');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

declare(strict_types=1);

namespace Rector\Symfony\Tests\Symfony60\Rector\FuncCall\ReplaceServiceArgumentRector;
namespace Rector\Symfony\Tests\Symfony60\Rector\FuncCall\ContainerInterfaceServiceToServiceContainerRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class ReplaceServiceArgumentRectorTest extends AbstractRectorTestCase
final class ContainerInterfaceServiceToServiceContainerRectorTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Rector\Symfony\Tests\Symfony60\Rector\FuncCall\ReplaceServiceArgumentRector\Fixture;
namespace Rector\Symfony\Tests\Symfony60\Rector\FuncCall\ContainerInterfaceServiceToServiceContainerRector\Fixture;

use Psr\Container\ContainerInterface;
use function Symfony\Component\DependencyInjection\Loader\Configurator\service;
Expand All @@ -11,7 +11,7 @@ return service(ContainerInterface::class);
-----
<?php

namespace Rector\Symfony\Tests\Symfony60\Rector\FuncCall\ReplaceServiceArgumentRector\Fixture;
namespace Rector\Symfony\Tests\Symfony60\Rector\FuncCall\ContainerInterfaceServiceToServiceContainerRector\Fixture;

use Psr\Container\ContainerInterface;
use function Symfony\Component\DependencyInjection\Loader\Configurator\service;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Rector\Symfony\Tests\Symfony60\Rector\FuncCall\ReplaceServiceArgumentRector\Fixture;
namespace Rector\Symfony\Tests\Symfony60\Rector\FuncCall\ContainerInterfaceServiceToServiceContainerRector\Fixture;

use function Symfony\Component\DependencyInjection\Loader\Configurator\service;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Symfony\Symfony60\Rector\FuncCall\ContainerInterfaceServiceToServiceContainerRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(ContainerInterfaceServiceToServiceContainerRector::class);
};

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,48 @@
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Scalar\String_;
use Rector\Contract\Rector\ConfigurableRectorInterface;
use Rector\PhpParser\Node\Value\ValueResolver;
use Rector\Rector\AbstractRector;
use Rector\Symfony\Enum\SymfonyClass;
use Rector\Symfony\Enum\SymfonyFunctionName;
use Rector\Symfony\ValueObject\ReplaceServiceArgument;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Rector\VersionBonding\Contract\ComposerPackageConstraintInterface;
use Rector\VersionBonding\ValueObject\ComposerPackageConstraint;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use Webmozart\Assert\Assert;

/**
* @see \Rector\Symfony\Tests\Symfony60\Rector\FuncCall\ReplaceServiceArgumentRector\ReplaceServiceArgumentRectorTest
* @changelog https://github.com/symfony/symfony/pull/42149
*
* @see \Rector\Symfony\Tests\Symfony60\Rector\FuncCall\ContainerInterfaceServiceToServiceContainerRector\ContainerInterfaceServiceToServiceContainerRectorTest
*/
final class ReplaceServiceArgumentRector extends AbstractRector implements ConfigurableRectorInterface
final class ContainerInterfaceServiceToServiceContainerRector extends AbstractRector implements ComposerPackageConstraintInterface
{
/**
* @var ReplaceServiceArgument[]
* @var string[]
*/
private array $replaceServiceArguments = [];
private const array CONTAINER_INTERFACES = [
SymfonyClass::PSR_CONTAINER_INTERFACE,
SymfonyClass::DEPENDENCY_INJECTION_CONTAINER_INTERFACE,
];

private const string SERVICE_CONTAINER = 'service_container';

public function __construct(
private readonly ValueResolver $valueResolver
) {
}

public function provideComposerPackageConstraint(): ComposerPackageConstraint
{
return new ComposerPackageConstraint('symfony/dependency-injection', '>=6.0');
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Replace defined service() argument in Symfony PHP config',
'Replace removed ContainerInterface alias with "service_container" service id in service() call',
[
new ConfiguredCodeSample(
new CodeSample(
<<<'CODE_SAMPLE'
use function Symfony\Component\DependencyInjection\Loader\Configurator\service;

Expand All @@ -49,8 +61,6 @@ public function getRuleDefinition(): RuleDefinition

return service('service_container');
CODE_SAMPLE
,
[new ReplaceServiceArgument('ContainerInterface', new String_('service_container'))]
),
]
);
Expand All @@ -73,29 +83,20 @@ public function refactor(Node $node): ?FuncCall
return null;
}

$firstArg = $node->args[0];
$firstArg = $node->args[0] ?? null;
if (! $firstArg instanceof Arg) {
return null;
}

foreach ($this->replaceServiceArguments as $replaceServiceArgument) {
if (! $this->valueResolver->isValue($firstArg->value, $replaceServiceArgument->getOldValue())) {
foreach (self::CONTAINER_INTERFACES as $containerInterface) {
if (! $this->valueResolver->isValue($firstArg->value, $containerInterface)) {
continue;
}

$node->args[0] = new Arg($replaceServiceArgument->getNewValueExpr());
$node->args[0] = new Arg(new String_(self::SERVICE_CONTAINER));
return $node;
}

return null;
}

/**
* @param mixed[] $configuration
*/
public function configure(array $configuration): void
{
Assert::allIsAOf($configuration, ReplaceServiceArgument::class);
$this->replaceServiceArguments = $configuration;
}
}
4 changes: 4 additions & 0 deletions src/Enum/SymfonyClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ final class SymfonyClass

public const string CONTAINER_AWARE_COMMAND = 'Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand';

public const string PSR_CONTAINER_INTERFACE = 'Psr\Container\ContainerInterface';

public const string DEPENDENCY_INJECTION_CONTAINER_INTERFACE = 'Symfony\Component\DependencyInjection\ContainerInterface';

public const string EVENT_DISPATCHER_INTERFACE = 'Symfony\Contracts\EventDispatcher\EventDispatcherInterface';

public const string VALIDATOR_INTERFACE = 'Symfony\Component\Validator\Validator\ValidatorInterface';
Expand Down
26 changes: 0 additions & 26 deletions src/ValueObject/ReplaceServiceArgument.php

This file was deleted.

Loading