Skip to content

Commit

Permalink
[DowngradePhp80] Add DowngradeNamedArgumentRector (#133)
Browse files Browse the repository at this point in the history
Co-authored-by: kaizen-ci <info@kaizen-ci.org>
  • Loading branch information
samsonasik and kaizen-ci committed Jun 1, 2021
1 parent 42ca188 commit 02f9524
Show file tree
Hide file tree
Showing 15 changed files with 504 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/build_scoped_rector.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ jobs:
# install only prod dependencies - do not use ramsey, it uses cache including "dev", we want to avoid it here
- run: composer install --no-dev --ansi

# early downgrade individual files of symfony Attribute classes
- run: bin/rector process vendor/symfony/dependency-injection/Attribute/Autoconfigure.php -c build/config/config-downgrade-php70.php --ansi
- run: bin/rector process vendor/symfony/dependency-injection/Attribute/AutoconfigureTag.php -c build/config/config-downgrade-php70.php --ansi

# 1. copy files to $NESTED_DIRECTORY directory Exclude the scoped/nested directories to prevent rsync from copying in a loop
- run: rsync --exclude rector-build -av * rector-build --quiet
- run: rm -rf rector-build/packages-tests rector-build/rules-tests rector-build/tests
Expand Down
12 changes: 12 additions & 0 deletions config/set/downgrade-php80.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Rector\Core\Configuration\Option;
use Rector\Core\ValueObject\PhpVersion;
use Rector\DowngradePhp80\Rector\Catch_\DowngradeNonCapturingCatchesRector;
use Rector\DowngradePhp80\Rector\Class_\DowngradeAttributeToAnnotationRector;
use Rector\DowngradePhp80\Rector\Class_\DowngradePropertyPromotionRector;
use Rector\DowngradePhp80\Rector\ClassConstFetch\DowngradeClassOnObjectToGetClassRector;
use Rector\DowngradePhp80\Rector\ClassMethod\DowngradeStaticTypeDeclarationRector;
Expand All @@ -13,10 +14,13 @@
use Rector\DowngradePhp80\Rector\FuncCall\DowngradeStrContainsRector;
use Rector\DowngradePhp80\Rector\FunctionLike\DowngradeMixedTypeDeclarationRector;
use Rector\DowngradePhp80\Rector\FunctionLike\DowngradeUnionTypeDeclarationRector;
use Rector\DowngradePhp80\Rector\MethodCall\DowngradeNamedArgumentRector;
use Rector\DowngradePhp80\Rector\NullsafeMethodCall\DowngradeNullsafeToTernaryOperatorRector;
use Rector\DowngradePhp80\Rector\Property\DowngradeUnionTypeTypedPropertyRector;
use Rector\DowngradePhp80\ValueObject\DowngradeAttributeToAnnotation;
use Rector\Removing\Rector\Class_\RemoveInterfacesRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symplify\SymfonyPhpConfig\ValueObjectInliner;

return static function (ContainerConfigurator $containerConfigurator): void {
$parameters = $containerConfigurator->parameters();
Expand All @@ -31,6 +35,13 @@
],
]]);

$services->set(DowngradeAttributeToAnnotationRector::class)
->call('configure', [[
DowngradeAttributeToAnnotationRector::ATTRIBUTE_TO_ANNOTATION => ValueObjectInliner::inline([
new DowngradeAttributeToAnnotation('Attribute', 'Attribute'),
]),
]]);

$services->set(DowngradeUnionTypeTypedPropertyRector::class);
$services->set(DowngradeUnionTypeDeclarationRector::class);
$services->set(DowngradeMixedTypeDeclarationRector::class);
Expand All @@ -44,4 +55,5 @@
$services->set(DowngradeTrailingCommasInParamUseRector::class);
$services->set(\Rector\DowngradePhp80\Rector\FuncCall\DowngradeStrStartsWithRector::class);
$services->set(\Rector\DowngradePhp80\Rector\FuncCall\DowngradeStrEndsWithRector::class);
$services->set(DowngradeNamedArgumentRector::class);
};
4 changes: 4 additions & 0 deletions full_build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ set -u

composer install --no-dev --ansi

# early downgrade individual files of symfony Attribute classes
bin/rector process vendor/symfony/dependency-injection/Attribute/Autoconfigure.php -c build/config/config-downgrade-php70.php --ansi
bin/rector process vendor/symfony/dependency-injection/Attribute/AutoconfigureTag.php -c build/config/config-downgrade-php70.php --ansi

rsync --exclude rector-build -av * rector-build --quiet
rm -rf rector-build/packages-tests rector-build/rules-tests rector-build/tests

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

namespace Rector\Tests\DowngradePhp80\Rector\Class_\DowngradeAttributeToAnnotationRector\Fixture;

#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
class AutoconfigureTag
{
public function __construct(string $name = null, array $attributes = [])
{
}
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp80\Rector\Class_\DowngradeAttributeToAnnotationRector\Fixture;

/**
* @Attribute
*/
class AutoconfigureTag
{
public function __construct(string $name = null, array $attributes = [])
{
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
'Symfony\Component\Routing\Annotation\Route'
),
new DowngradeAttributeToAnnotation('Symfony\Contracts\Service\Attribute\Required', 'required'),
new DowngradeAttributeToAnnotation(
'Attribute',
'Attribute'
),
]),
]]);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\DowngradePhp80\Rector\MethodCall\DowngradeNamedArgumentRector;

use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;

final class DowngradeNamedArgumentRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
* @requires PHP 8.0
*/
public function test(SmartFileInfo $fileInfo): void
{
$this->doTestFileInfo($fileInfo);
}

/**
* @return Iterator<SmartFileInfo>
*/
public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Rector\Tests\DowngradePhp80\Rector\MethodCall\DowngradeNamedArgumentRector\Fixture;

class Fixture
{
private function execute(?array $a = null, ?array $b = null)
{
}

public function run(string $name = null, array $attributes = [])
{
$this->execute(a: [[$name ?? 0 => $attributes]]);
}
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp80\Rector\MethodCall\DowngradeNamedArgumentRector\Fixture;

class Fixture
{
private function execute(?array $a = null, ?array $b = null)
{
}

public function run(string $name = null, array $attributes = [])
{
$this->execute([[$name ?? 0 => $attributes]]);
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Rector\Tests\DowngradePhp80\Rector\MethodCall\DowngradeNamedArgumentRector\Fixture;

class Fixture2
{
private function execute(?array $a = null, ?array $b = null)
{
}

public function run(string $name = null, array $attributes = [])
{
$this->execute(b: [[$name ?? 0 => $attributes]]);
}
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp80\Rector\MethodCall\DowngradeNamedArgumentRector\Fixture;

class Fixture2
{
private function execute(?array $a = null, ?array $b = null)
{
}

public function run(string $name = null, array $attributes = [])
{
$this->execute(null, [[$name ?? 0 => $attributes]]);
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Rector\Tests\DowngradePhp80\Rector\MethodCall\DowngradeNamedArgumentRector\Fixture;

class FlipOrder
{
private function execute(?array $a = null, ?array $b = null)
{
var_dump($b);
}

public function run(string $name = null, array $attributes = [])
{
$this->execute(b: [[$name ?? 0 => $attributes]], a: []);
}
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp80\Rector\MethodCall\DowngradeNamedArgumentRector\Fixture;

class FlipOrder
{
private function execute(?array $a = null, ?array $b = null)
{
var_dump($b);
}

public function run(string $name = null, array $attributes = [])
{
$this->execute([], [[$name ?? 0 => $attributes]]);
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Rector\Tests\DowngradePhp80\Rector\MethodCall\DowngradeNamedArgumentRector\Fixture;

use Rector\Tests\DowngradePhp80\Rector\MethodCall\DowngradeNamedArgumentRector\Source\Foo;

class SkipNoArg extends Foo
{
public function __construct(string $name = null, array $attributes = [])
{
parent::__construct();
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Rector\Tests\DowngradePhp80\Rector\MethodCall\DowngradeNamedArgumentRector\Fixture;

use Rector\Tests\DowngradePhp80\Rector\MethodCall\DowngradeNamedArgumentRector\Source\Foo;

class SkipNoNamedArg extends Foo
{
public function __construct(string $name = null, array $attributes = [])
{
parent::__construct([], []);
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Rector\Tests\DowngradePhp80\Rector\MethodCall\DowngradeNamedArgumentRector\Fixture;

use Rector\Tests\DowngradePhp80\Rector\MethodCall\DowngradeNamedArgumentRector\Source\Foo;

class StaticCall extends Foo
{
public function __construct(string $name = null, array $attributes = [])
{
parent::__construct(a: [[$name ?? 0 => $attributes]]);
}
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp80\Rector\MethodCall\DowngradeNamedArgumentRector\Fixture;

use Rector\Tests\DowngradePhp80\Rector\MethodCall\DowngradeNamedArgumentRector\Source\Foo;

class StaticCall extends Foo
{
public function __construct(string $name = null, array $attributes = [])
{
parent::__construct([[$name ?? 0 => $attributes]]);
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\DowngradePhp80\Rector\MethodCall\DowngradeNamedArgumentRector\Source;

class Foo
{
public function __construct(
public ?array $a = null,
public ?array $b = null
) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

use Rector\DowngradePhp80\Rector\MethodCall\DowngradeNamedArgumentRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(DowngradeNamedArgumentRector::class);
};

0 comments on commit 02f9524

Please sign in to comment.