From 968188a84bfd25a2773f87dac2222432688cd126 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20St=C3=BCcher?= Date: Fri, 5 Feb 2021 22:40:20 +0100 Subject: [PATCH 1/6] Use getHtmlPattern to be compliant with ECMA 262 --- .../Property/Restriction/PropertySchemaRegexRestriction.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Bridge/Symfony/Validator/Metadata/Property/Restriction/PropertySchemaRegexRestriction.php b/src/Bridge/Symfony/Validator/Metadata/Property/Restriction/PropertySchemaRegexRestriction.php index dccc81e9fbd..45a25fc2634 100644 --- a/src/Bridge/Symfony/Validator/Metadata/Property/Restriction/PropertySchemaRegexRestriction.php +++ b/src/Bridge/Symfony/Validator/Metadata/Property/Restriction/PropertySchemaRegexRestriction.php @@ -29,7 +29,7 @@ class PropertySchemaRegexRestriction implements PropertySchemaRestrictionMetadat */ public function create(Constraint $constraint, PropertyMetadata $propertyMetadata): array { - return isset($constraint->pattern) ? ['pattern' => $constraint->pattern] : []; + return $constraint->getHtmlPattern() ? ['pattern' => $constraint->getHtmlPattern()] : []; } /** From b209c7399d86797f65b12a8c373c09e8ca741503 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20St=C3=BCcher?= Date: Fri, 5 Feb 2021 23:05:57 +0100 Subject: [PATCH 2/6] Fix test: pattern for preg_match always needs delimiter --- tests/Fixtures/DummyValidatedEntity.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Fixtures/DummyValidatedEntity.php b/tests/Fixtures/DummyValidatedEntity.php index 81d3e4b852d..3668c7ff51b 100644 --- a/tests/Fixtures/DummyValidatedEntity.php +++ b/tests/Fixtures/DummyValidatedEntity.php @@ -32,7 +32,7 @@ class DummyValidatedEntity * * @Assert\NotBlank * @Assert\Length(max="4", min="10") - * @Assert\Regex(pattern="^dummy$") + * @Assert\Regex(pattern="/^dummy$/") */ public $dummy; From cae97d17a8748e9eb6d2b89a3112d78a68c1e586 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20St=C3=BCcher?= Date: Fri, 5 Feb 2021 23:12:56 +0100 Subject: [PATCH 3/6] Fix test: ECMA 262 complaint regex --- .../Metadata/Property/ValidatorPropertyMetadataFactoryTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Bridge/Symfony/Validator/Metadata/Property/ValidatorPropertyMetadataFactoryTest.php b/tests/Bridge/Symfony/Validator/Metadata/Property/ValidatorPropertyMetadataFactoryTest.php index 181b9ff14df..819493540f3 100644 --- a/tests/Bridge/Symfony/Validator/Metadata/Property/ValidatorPropertyMetadataFactoryTest.php +++ b/tests/Bridge/Symfony/Validator/Metadata/Property/ValidatorPropertyMetadataFactoryTest.php @@ -300,7 +300,7 @@ public function testCreateWithPropertyRegexRestriction(): void $schema = $validationPropertyMetadataFactory->create(DummyValidatedEntity::class, 'dummy')->getSchema(); $this->assertNotNull($schema); $this->assertArrayHasKey('pattern', $schema); - $this->assertEquals('^dummy$', $schema['pattern']); + $this->assertEquals('dummy', $schema['pattern']); } public function testCreateWithPropertyFormatRestriction(): void From 30ecadabcdc9d4553d1318ac12cc23f80c79bcaa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20St=C3=BCcher?= Date: Fri, 5 Feb 2021 23:51:56 +0100 Subject: [PATCH 4/6] PHPStan false positive --- phpstan.neon.dist | 3 +++ 1 file changed, 3 insertions(+) diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 131f24fe529..e5a04fcde23 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -50,6 +50,9 @@ parameters: - message: '#Call to an undefined method Doctrine\\Persistence\\ObjectManager::getConnection\(\)#' path: src/Bridge/Doctrine/Common/Util/IdentifierManagerTrait.php + - + message: '#Call to an undefined method Symfony\\Component\\Validator\\Constraint::getHtmlPattern\(\)\.#' + path: src/Bridge/Symfony/Validator/Metadata/Property/Restriction/PropertySchemaRegexRestriction.php # https://github.com/willdurand/Negotiation/issues/89#issuecomment-513283286 - message: '#Call to an undefined method Negotiation\\AcceptHeader::getType\(\)\.#' From 184e3ce3f414feb5274e3c6dfb6428eadf1abd7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20St=C3=BCcher?= Date: Sat, 6 Feb 2021 00:33:45 +0100 Subject: [PATCH 5/6] Check instanceof Regex to avoid PHPStan exception --- phpstan.neon.dist | 3 --- .../Property/Restriction/PropertySchemaRegexRestriction.php | 6 +++++- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/phpstan.neon.dist b/phpstan.neon.dist index e5a04fcde23..131f24fe529 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -50,9 +50,6 @@ parameters: - message: '#Call to an undefined method Doctrine\\Persistence\\ObjectManager::getConnection\(\)#' path: src/Bridge/Doctrine/Common/Util/IdentifierManagerTrait.php - - - message: '#Call to an undefined method Symfony\\Component\\Validator\\Constraint::getHtmlPattern\(\)\.#' - path: src/Bridge/Symfony/Validator/Metadata/Property/Restriction/PropertySchemaRegexRestriction.php # https://github.com/willdurand/Negotiation/issues/89#issuecomment-513283286 - message: '#Call to an undefined method Negotiation\\AcceptHeader::getType\(\)\.#' diff --git a/src/Bridge/Symfony/Validator/Metadata/Property/Restriction/PropertySchemaRegexRestriction.php b/src/Bridge/Symfony/Validator/Metadata/Property/Restriction/PropertySchemaRegexRestriction.php index 45a25fc2634..3ca7f93258b 100644 --- a/src/Bridge/Symfony/Validator/Metadata/Property/Restriction/PropertySchemaRegexRestriction.php +++ b/src/Bridge/Symfony/Validator/Metadata/Property/Restriction/PropertySchemaRegexRestriction.php @@ -29,7 +29,11 @@ class PropertySchemaRegexRestriction implements PropertySchemaRestrictionMetadat */ public function create(Constraint $constraint, PropertyMetadata $propertyMetadata): array { - return $constraint->getHtmlPattern() ? ['pattern' => $constraint->getHtmlPattern()] : []; + if ($constraint instanceof Regex && $constraint->getHtmlPattern()) { + return ['pattern' => $constraint->getHtmlPattern()]; + } + + return []; } /** From ac66aa262e4d54d6dba4d6225a45a315c497ebcb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20St=C3=BCcher?= Date: Sat, 6 Feb 2021 00:59:59 +0100 Subject: [PATCH 6/6] Update src/Bridge/Symfony/Validator/Metadata/Property/Restriction/PropertySchemaRegexRestriction.php MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Kévin Dunglas --- .../Property/Restriction/PropertySchemaRegexRestriction.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/Bridge/Symfony/Validator/Metadata/Property/Restriction/PropertySchemaRegexRestriction.php b/src/Bridge/Symfony/Validator/Metadata/Property/Restriction/PropertySchemaRegexRestriction.php index 3ca7f93258b..167152748c8 100644 --- a/src/Bridge/Symfony/Validator/Metadata/Property/Restriction/PropertySchemaRegexRestriction.php +++ b/src/Bridge/Symfony/Validator/Metadata/Property/Restriction/PropertySchemaRegexRestriction.php @@ -29,11 +29,7 @@ class PropertySchemaRegexRestriction implements PropertySchemaRestrictionMetadat */ public function create(Constraint $constraint, PropertyMetadata $propertyMetadata): array { - if ($constraint instanceof Regex && $constraint->getHtmlPattern()) { - return ['pattern' => $constraint->getHtmlPattern()]; - } - - return []; + return $constraint instanceof Regex && $constraint->getHtmlPattern() ? ['pattern' => $constraint->getHtmlPattern()] : []; } /**