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
1 change: 1 addition & 0 deletions config/sets/symfony/symfony8/symfony81.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
// @see https://github.com/symfony/symfony/blob/8.1/UPGRADE-8.1.md
return static function (RectorConfig $rectorConfig): void {
$rectorConfig->import(__DIR__ . '/symfony81/symfony81-dependency-injection.php');
$rectorConfig->import(__DIR__ . '/symfony81/symfony81-uid.php');
};
10 changes: 10 additions & 0 deletions config/sets/symfony/symfony8/symfony81/symfony81-uid.php
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\Symfony81\Rector\StaticCall\AddFormatArgumentToIsValidRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rules([AddFormatArgumentToIsValidRector::class]);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Symfony\Tests\Symfony81\Rector\StaticCall\AddFormatArgumentToIsValidRector;

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

final class AddFormatArgumentToIsValidRectorTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::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,31 @@
<?php

namespace Rector\Symfony\Tests\Symfony81\Rector\StaticCall\AddFormatArgumentToIsValidRector\Fixture;

use Symfony\Component\Uid\Ulid;

final class Foo
{
public function bar(string $id): bool
{
return Ulid::isValid($id);
}
}

?>
-----
<?php

namespace Rector\Symfony\Tests\Symfony81\Rector\StaticCall\AddFormatArgumentToIsValidRector\Fixture;

use Symfony\Component\Uid\Ulid;

final class Foo
{
public function bar(string $id): bool
{
return Ulid::isValid($id, \Symfony\Component\Uid\Ulid::FORMAT_BASE_32);
}
}

?>
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\Symfony81\Rector\StaticCall\AddFormatArgumentToIsValidRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(AddFormatArgumentToIsValidRector::class);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

declare(strict_types=1);

namespace Rector\Symfony\Symfony81\Rector\StaticCall;

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Name\Relative;
use PhpParser\Node\Stmt\Class_;
use Rector\Rector\AbstractRector;
use Rector\Symfony\Enum\SymfonyClass;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see https://github.com/symfony/symfony/blob/8.1/UPGRADE-8.1.md#uid
*
* @see \Rector\Symfony\Tests\Symfony81\Rector\StaticCall\AddFormatArgumentToIsValidRector\AddFormatArgumentToIsValidRectorTest
*/
final class AddFormatArgumentToIsValidRector extends AbstractRector
Comment thread
samsonasik marked this conversation as resolved.
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Add $format argument to Ulid::isValid()',
[
new CodeSample(
<<<'CODE_SAMPLE'
Symfony\Component\Uid\Ulid;
final class Foo
{
public function bar(string $id): bool
{
return Ulid::isValid($id);
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
Symfony\Component\Uid\Ulid;
final class Foo
{
public function bar(string $id): bool
{
return Ulid::isValid($id, Symfony\Component\Uid\Ulid::FORMAT_BASE_32);
}
}
CODE_SAMPLE
,
),
],
);
}

public function getNodeTypes(): array
{
return [StaticCall::class];
}

/**
* @param StaticCall $node
*/
public function refactor(Node $node): ?Node
{
if (! $this->isName($node->class, 'Symfony\Component\Uid\Ulid')) {
return null;
}

if (! $this->isName($node->name, 'isValid')) {
return null;
}

if ($node->isFirstClassCallable()) {
return null;
}

if (\count($node->args) !==1) {
return null;
}

Comment thread
samsonasik marked this conversation as resolved.
$node->args[] = new Arg(
new ClassConstFetch(new FullyQualified(SymfonyClass::ULID_CLASS), 'FORMAT_BASE_32')
Comment thread
samsonasik marked this conversation as resolved.
);

return $node;
}
}
2 changes: 2 additions & 0 deletions src/Enum/SymfonyClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,6 @@ final class SymfonyClass
public const string ABSTRACT_TYPE_EXTENSION = 'Symfony\Component\Form\AbstractTypeExtension';

public const string ABSTRACT_TYPE = 'Symfony\Component\Form\AbstractType';

public const string ULID_CLASS = 'Symfony\Component\Uid\Ulid';
}
6 changes: 6 additions & 0 deletions src/Set/SetProvider/Symfony8SetProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ public function provide(): array
'8.1',
__DIR__ . '/../../../config/sets/symfony/symfony8/symfony81/symfony81-dependency-injection.php'
),
new ComposerTriggeredSet(
SetGroup::SYMFONY,
'symfony/uid',
'8.1',
__DIR__ . '/../../../config/sets/symfony/symfony8/symfony81/symfony81-uid.php'
),
];
}
}
5 changes: 5 additions & 0 deletions src/Set/SymfonySetList.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,11 @@ final class SymfonySetList
*/
public const string SYMFONY_80 = __DIR__ . '/../../config/sets/symfony/symfony8/symfony80.php';

/**
* @deprecated Set list are too generic and do not handle package differences. Use ->withComposerBased(symfony: true) instead
*/
public const string SYMFONY_81 = __DIR__ . '/../../config/sets/symfony/symfony8/symfony81.php';

public const string SYMFONY_CODE_QUALITY = __DIR__ . '/../../config/sets/symfony/symfony-code-quality.php';

public const string SYMFONY_CONSTRUCTOR_INJECTION = __DIR__ . '/../../config/sets/symfony/symfony-constructor-injection.php';
Expand Down
Loading