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
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Rector\Symfony\Tests\CodeQuality\Rector\Class_\LoadValidatorMetadataToAttributeRector\Fixture;

use Rector\Symfony\Tests\CodeQuality\Rector\Class_\LoadValidatorMetadataToAttributeRector\Source\UniqueUserAlias;
use Symfony\Component\Validator\Mapping\ClassMetadata;

final class SkipConstraintWithoutOwnConstructor
{
private $alias;

public static function loadValidatorMetadata(ClassMetadata $metadata): void
{
$metadata->addConstraint(new UniqueUserAlias([
'field' => 'alias',
'message' => 'mautic.lead.list.alias.unique',
]));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace Rector\Symfony\Tests\CodeQuality\Rector\Class_\LoadValidatorMetadataToAttributeRector\Source;

use Attribute;
use Symfony\Component\Validator\Constraint;

/**
* Custom constraint that configures itself through properties, without a constructor of its own
*/
#[Attribute]
final class UniqueUserAlias extends Constraint
{
public $message = 'This alias is already in use.';

public $field = '';

public function getTargets(): string
{
return self::CLASS_CONSTRAINT;
}

public function getDefaultOption(): string
{
return 'field';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Rector\Rector\AbstractRector;
use Rector\Symfony\NodeAnalyzer\ValidatorAssert\ConstantExpressionAnalyzer;
use Rector\Symfony\NodeAnalyzer\ValidatorAssert\ConstraintAttributeTargetAnalyzer;
use Rector\Symfony\NodeAnalyzer\ValidatorAssert\ConstraintConstructorAnalyzer;
use Rector\Symfony\NodeAnalyzer\ValidatorAssert\MetadataConstraintResolver;
use Rector\Symfony\NodeFactory\ValidatorAssert\ConstraintAttributeGroupFactory;
use Rector\Symfony\ValueObject\ValidatorAssert\ClassMethodAndConstraint;
Expand All @@ -37,6 +38,7 @@ public function __construct(
private readonly ConstraintAttributeTargetAnalyzer $constraintAttributeTargetAnalyzer,
private readonly ConstraintAttributeGroupFactory $constraintAttributeGroupFactory,
private readonly ConstantExpressionAnalyzer $constantExpressionAnalyzer,
private readonly ConstraintConstructorAnalyzer $constraintConstructorAnalyzer,
) {
}

Expand Down Expand Up @@ -247,6 +249,11 @@ private function resolveConstraintClass(New_ $new): ?string
return null;
}

// e.g. new UniqueUserAlias(['field' => 'alias']) - the options have no matching constructor arguments
if ($new->args !== [] && ! $this->constraintConstructorAnalyzer->hasOwnConstructor($constraintClass)) {
return null;
}

return $constraintClass;
}
}
38 changes: 38 additions & 0 deletions src/NodeAnalyzer/ValidatorAssert/ConstraintConstructorAnalyzer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Rector\Symfony\NodeAnalyzer\ValidatorAssert;

use PHPStan\Reflection\ReflectionProvider;
use Symfony\Component\Validator\Constraint;

/**
* A constraint that adds no constructor of its own inherits the one from Symfony\Component\Validator\Constraint,
* which takes the options array as a whole. Its options are plain properties, so there are no named arguments to
* move them to.
*/
final readonly class ConstraintConstructorAnalyzer
{
public function __construct(
private ReflectionProvider $reflectionProvider,
) {
}

public function hasOwnConstructor(string $constraintClass): bool
{
if (! $this->reflectionProvider->hasClass($constraintClass)) {
return false;
}

$classReflection = $this->reflectionProvider->getClass($constraintClass);
if (! $classReflection->hasConstructor()) {
return false;
}

$extendedMethodReflection = $classReflection->getConstructor();

return $extendedMethodReflection->getDeclaringClass()
->getName() !== Constraint::class;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ public static function provideData(): iterable

private function parseNew(string $newExpression): New_
{
$parser = new ParserFactory()->createForNewestSupportedVersion();
$parser = new ParserFactory()
->createForNewestSupportedVersion();

$stmts = $parser->parse('<?php ' . $newExpression . ';');
$this->assertIsArray($stmts);
Expand Down
Loading