diff --git a/rules-tests/CodeQuality/Rector/Class_/LoadValidatorMetadataToAttributeRector/Fixture/skip_constraint_without_own_constructor.php.inc b/rules-tests/CodeQuality/Rector/Class_/LoadValidatorMetadataToAttributeRector/Fixture/skip_constraint_without_own_constructor.php.inc new file mode 100644 index 00000000..e80c145c --- /dev/null +++ b/rules-tests/CodeQuality/Rector/Class_/LoadValidatorMetadataToAttributeRector/Fixture/skip_constraint_without_own_constructor.php.inc @@ -0,0 +1,19 @@ +addConstraint(new UniqueUserAlias([ + 'field' => 'alias', + 'message' => 'mautic.lead.list.alias.unique', + ])); + } +} diff --git a/rules-tests/CodeQuality/Rector/Class_/LoadValidatorMetadataToAttributeRector/Source/UniqueUserAlias.php b/rules-tests/CodeQuality/Rector/Class_/LoadValidatorMetadataToAttributeRector/Source/UniqueUserAlias.php new file mode 100644 index 00000000..74296c82 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/Class_/LoadValidatorMetadataToAttributeRector/Source/UniqueUserAlias.php @@ -0,0 +1,29 @@ + 'alias']) - the options have no matching constructor arguments + if ($new->args !== [] && ! $this->constraintConstructorAnalyzer->hasOwnConstructor($constraintClass)) { + return null; + } + return $constraintClass; } } diff --git a/src/NodeAnalyzer/ValidatorAssert/ConstraintConstructorAnalyzer.php b/src/NodeAnalyzer/ValidatorAssert/ConstraintConstructorAnalyzer.php new file mode 100644 index 00000000..bfa3badb --- /dev/null +++ b/src/NodeAnalyzer/ValidatorAssert/ConstraintConstructorAnalyzer.php @@ -0,0 +1,38 @@ +reflectionProvider->hasClass($constraintClass)) { + return false; + } + + $classReflection = $this->reflectionProvider->getClass($constraintClass); + if (! $classReflection->hasConstructor()) { + return false; + } + + $extendedMethodReflection = $classReflection->getConstructor(); + + return $extendedMethodReflection->getDeclaringClass() + ->getName() !== Constraint::class; + } +} diff --git a/tests/NodeAnalyzer/ValidatorAssert/ConstantExpressionAnalyzerTest.php b/tests/NodeAnalyzer/ValidatorAssert/ConstantExpressionAnalyzerTest.php index a9af9caa..4453e4c5 100644 --- a/tests/NodeAnalyzer/ValidatorAssert/ConstantExpressionAnalyzerTest.php +++ b/tests/NodeAnalyzer/ValidatorAssert/ConstantExpressionAnalyzerTest.php @@ -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('assertIsArray($stmts);