Skip to content

[Symfony 7.3] Add RegexToSlugConstraintRector #723 #780

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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/symfony7/symfony73.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
return static function (RectorConfig $rectorConfig): void {
$rectorConfig->import(__DIR__ . '/symfony73/symfony73-console.php');
$rectorConfig->import(__DIR__ . '/symfony73/symfony73-twig-bundle.php');
$rectorConfig->import(__DIR__ . '/symfony73/symfony73-validator.php');
};
11 changes: 11 additions & 0 deletions config/sets/symfony/symfony7/symfony73/symfony73-validator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Symfony\Symfony73\Rector\New_\RegexToSlugConstraintRector;

return static function (RectorConfig $rectorConfig): void {
// @see https://symfony.com/blog/new-in-symfony-7-3-slug-and-twig-constraints#slug-constraint
$rectorConfig->rule(RegexToSlugConstraintRector::class);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Rector\Symfony\Tests\Symfony73\Rector\New_\RegexToSlugConstraintRector\Fixture;

use Symfony\Component\Validator\Constraints\Regex;

class Replace
{
public function createConstraint()
{
return new Regex('/[a-z0-9]+(?:-[a-z0-9]+)*/');
}
}

?>
-----
<?php

namespace Rector\Symfony\Tests\Symfony73\Rector\New_\RegexToSlugConstraintRector\Fixture;

use Symfony\Component\Validator\Constraints\Regex;

class Replace
{
public function createConstraint()
{
return new Symfony\Component\Validator\Constraints\Slug();
}
}

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

namespace Rector\Symfony\Tests\Symfony73\Rector\New_\RegexToSlugConstraintRector\Fixture;

use Symfony\Component\Validator\Constraints\Regex;

class ReplaceAllParams
{
public function createConstraint()
{
return new Regex(pattern: '/[a-z0-9]+(?:-[a-z0-9]+)*/', message: 'some message', htmlPattern: null, match: null, normalizer: null, groups: ['Default'], payload: null, options: []);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe i should skip if htmlPattern, match or normalizer are part of the params?

}
}

?>
-----
<?php

namespace Rector\Symfony\Tests\Symfony73\Rector\New_\RegexToSlugConstraintRector\Fixture;

use Symfony\Component\Validator\Constraints\Regex;

class ReplaceAllParams
{
public function createConstraint()
{
return new Symfony\Component\Validator\Constraints\Slug(message: 'some message', groups: ['Default'], payload: null, options: []);
}
}

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

namespace Rector\Symfony\Tests\Symfony73\Rector\New_\RegexToSlugConstraintRector\Fixture;

use Symfony\Component\Validator\Constraints\Regex;

class ReplaceParamArraySyntax
{
public function createConstraint()
{
return new Regex(['pattern' => '/^[a-z0-9]+(?:-[a-z0-9]+)*$/']);
}
}

?>
-----
<?php

namespace Rector\Symfony\Tests\Symfony73\Rector\New_\RegexToSlugConstraintRector\Fixture;

use Symfony\Component\Validator\Constraints\Regex;

class ReplaceParamArraySyntax
{
public function createConstraint()
{
return new Symfony\Component\Validator\Constraints\Slug();
}
}

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

namespace Rector\Symfony\Tests\Symfony73\Rector\New_\RegexToSlugConstraintRector\Fixture;

use Symfony\Component\Validator\Constraints\Regex;

class ReplaceStartAndEndTags
{
public function createConstraint()
{
return new Regex('/^[a-z0-9]+(?:-[a-z0-9]+)*$/');
}
}

?>
-----
<?php

namespace Rector\Symfony\Tests\Symfony73\Rector\New_\RegexToSlugConstraintRector\Fixture;

use Symfony\Component\Validator\Constraints\Regex;

class ReplaceStartAndEndTags
{
public function createConstraint()
{
return new Symfony\Component\Validator\Constraints\Slug();
}
}

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

namespace Rector\Symfony\Tests\Symfony73\Rector\New_\RegexToSlugConstraintRector\Fixture;

use Symfony\Component\Validator\Constraints as Assert;

class ReplaceWithAlias
{
public function createConstraint()
{
return new Assert\Regex('/^[a-z0-9]+(?:-[a-z0-9]+)*$/');
}
}

?>
-----
<?php

namespace Rector\Symfony\Tests\Symfony73\Rector\New_\RegexToSlugConstraintRector\Fixture;

use Symfony\Component\Validator\Constraints as Assert;

class ReplaceWithAlias
{
public function createConstraint()
{
return new Symfony\Component\Validator\Constraints\Slug();
}
}

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

namespace Rector\Symfony\Tests\Symfony73\Rector\New_\RegexToSlugConstraintRector\Fixture;

use Symfony\Component\Validator\Constraints\Regex;

class SkipDifferentRegex
{
public function createConstraint()
{
return new Regex('/^[a-z0-9]$/');
}
}

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

namespace Rector\Symfony\Tests\Symfony73\Rector\New_\RegexToSlugConstraintRector\Fixture;

use Symfony\Component\Validator\Constraints\Regex;

class SkipDifferentRegex
{
public function createConstraint(string $regex = '/^[a-z0-9]+(?:-[a-z0-9]+)*$/')
{
return new Regex($regex);
}
}

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

declare(strict_types=1);

namespace Rector\Symfony\Tests\Symfony73\Rector\New_\RegexToSlugConstraintRector;

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

final class RegexToSlugConstraintRectorTest 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,10 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Symfony\Symfony73\Rector\New_\RegexToSlugConstraintRector;

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