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
17 changes: 17 additions & 0 deletions config/sets/symfony/symfony5/symfony52/symfony52-validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,27 @@

use Rector\Config\RectorConfig;
use Rector\Symfony\Symfony52\Rector\MethodCall\ValidatorBuilderEnableAnnotationMappingRector;
use Rector\Transform\Rector\Attribute\AttributeKeyToClassConstFetchRector;
use Rector\Transform\ValueObject\AttributeKeyToClassConstFetch;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rules([
# https://github.com/symfony/symfony/blob/5.x/UPGRADE-5.2.md#validator
ValidatorBuilderEnableAnnotationMappingRector::class,
]);

// Symfony\Component\Validator\Constraints\Email::VALIDATION_MODE_* added in Symfony 4.1
// When using PHP 8 attributes (available from Symfony 5.2 set), map string modes to constants
$rectorConfig->ruleWithConfiguration(AttributeKeyToClassConstFetchRector::class, [
new AttributeKeyToClassConstFetch(
'Symfony\\Component\\Validator\\Constraints\\Email',
'mode',
'Symfony\\Component\\Validator\\Constraints\\Email',
[
'strict' => 'VALIDATION_MODE_STRICT',
'loose' => 'VALIDATION_MODE_LOOSE',
'html5' => 'VALIDATION_MODE_HTML5',
]
),
]);
};
14 changes: 14 additions & 0 deletions config/sets/symfony/symfony6/symfony61/symfony61-validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use Rector\Config\RectorConfig;
use Rector\Renaming\Rector\Name\RenameClassRector;
use Rector\Symfony\Symfony61\Rector\StaticPropertyFetch\ErrorNamesPropertyToConstantRector;
use Rector\Transform\Rector\Attribute\AttributeKeyToClassConstFetchRector;
use Rector\Transform\ValueObject\AttributeKeyToClassConstFetch;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(ErrorNamesPropertyToConstantRector::class);
Expand All @@ -13,4 +15,16 @@
'Symfony\Component\Validator\Constraints\ExpressionLanguageSyntax' => 'Symfony\Component\Validator\Constraints\ExpressionSyntax',
'Symfony\Component\Validator\Constraints\ExpressionLanguageSyntaxValidator' => 'Symfony\Component\Validator\Constraints\ExpressionSyntaxValidator',
]);

// @see https://github.com/symfony/validator/blob/6.1/Constraints/Email.php
$rectorConfig->ruleWithConfiguration(AttributeKeyToClassConstFetchRector::class, [
new AttributeKeyToClassConstFetch(
'Symfony\\Component\\Validator\\Constraints\\Email',
'mode',
'Symfony\\Component\\Validator\\Constraints\\Email',
[
'html5-allow-no-tld' => 'VALIDATION_MODE_HTML5_ALLOW_NO_TLD',
]
),
]);
};
1 change: 1 addition & 0 deletions config/sets/symfony/symfony6/symfony62.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@
$rectorConfig->import(__DIR__ . '/symfony62/symfony62-doctrine-bridge.php');
$rectorConfig->import(__DIR__ . '/symfony62/symfony62-messenger.php');
$rectorConfig->import(__DIR__ . '/symfony62/symfony62-mail-pace-mailer.php');
$rectorConfig->import(__DIR__ . '/symfony62/symfony62-validator.php');
};
19 changes: 19 additions & 0 deletions config/sets/symfony/symfony6/symfony62/symfony62-validator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Renaming\Rector\ClassConstFetch\RenameClassConstFetchRector;
use Rector\Renaming\ValueObject\RenameClassConstFetch;

return static function (RectorConfig $rectorConfig): void {
// Symfony 6.2: Email::VALIDATION_MODE_LOOSE deprecated, use Email::VALIDATION_MODE_HTML5 instead
// @see https://github.com/symfony/validator/blob/6.2/Constraints/Email.php
$rectorConfig->ruleWithConfiguration(RenameClassConstFetchRector::class, [
new RenameClassConstFetch(
'Symfony\\Component\\Validator\\Constraints\\Email',
'VALIDATION_MODE_LOOSE',
'VALIDATION_MODE_HTML5'
),
]);
};
6 changes: 6 additions & 0 deletions src/Set/SetProvider/Symfony6SetProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,12 @@ public function provide(): array
'6.2',
__DIR__ . '/../../../config/sets/symfony/symfony6/symfony62/symfony62-mail-pace-mailer.php'
),
new ComposerTriggeredSet(
SetGroup::SYMFONY,
'symfony/validator',
'6.2',
__DIR__ . '/../../../config/sets/symfony/symfony6/symfony62/symfony62-validator.php'
),

new ComposerTriggeredSet(
SetGroup::SYMFONY,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

use Symfony\Component\Validator\Constraints\Email;

class EmailModeStrictToConst
{
#[Email(mode: 'strict')]
public function runStrict(): void
{
}
}

class EmailModeLooseToConst
{
#[Email(mode: 'loose')]
public function runLoose(): void
{
}
}

class EmailModeHtml5ToConst
{
#[Email(mode: 'html5')]
public function runHtml5(): void
{
}
}

?>
-----
<?php

use Symfony\Component\Validator\Constraints\Email;

class EmailModeStrictToConst
{
#[Email(mode: \Symfony\Component\Validator\Constraints\Email::VALIDATION_MODE_STRICT)]
public function runStrict(): void
{
}
}

class EmailModeLooseToConst
{
#[Email(mode: \Symfony\Component\Validator\Constraints\Email::VALIDATION_MODE_LOOSE)]
public function runLoose(): void
{
}
}

class EmailModeHtml5ToConst
{
#[Email(mode: \Symfony\Component\Validator\Constraints\Email::VALIDATION_MODE_HTML5)]
public function runHtml5(): void
{
}
}

?>
28 changes: 28 additions & 0 deletions tests/Set/Symfony52/Symfony52Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Symfony\Tests\Set\Symfony52;

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

final class Symfony52Test 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/symfony52.php';
}
}
10 changes: 10 additions & 0 deletions tests/Set/Symfony52/config/symfony52.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\Set\SymfonySetList;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->sets([SymfonySetList::SYMFONY_52]);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

use Symfony\Component\Validator\Constraints\Email;

class Html5AllowNoTld
{
#[Email(mode: 'html5-allow-no-tld')]
public function run(): void
{
}
}

?>
-----
<?php

use Symfony\Component\Validator\Constraints\Email;

class Html5AllowNoTld
{
#[Email(mode: \Symfony\Component\Validator\Constraints\Email::VALIDATION_MODE_HTML5_ALLOW_NO_TLD)]
public function run(): void
{
}
}

?>
27 changes: 27 additions & 0 deletions tests/Set/Symfony62/Fixture/email_loose_to_html5.php.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

use Symfony\Component\Validator\Constraints\Email;

class EmailModeLooseToHtml5
{
#[Email(mode: \Symfony\Component\Validator\Constraints\Email::VALIDATION_MODE_LOOSE)]
public function run(): void
{
}
}

?>
-----
<?php

use Symfony\Component\Validator\Constraints\Email;

class EmailModeLooseToHtml5
{
#[Email(mode: \Symfony\Component\Validator\Constraints\Email::VALIDATION_MODE_HTML5)]
public function run(): void
{
}
}

?>