From f1b87f9feaa570ea7b09e5f34690b125b784af86 Mon Sep 17 00:00:00 2001 From: Yoanm Date: Sat, 20 Apr 2019 18:09:07 +0200 Subject: [PATCH 1/8] Improve --- src/App/Helper/StringDocHelper.php | 130 ++++++++++-------- .../ConstraintToParamsDocTransformer.php | 64 ++++++--- 2 files changed, 114 insertions(+), 80 deletions(-) diff --git a/src/App/Helper/StringDocHelper.php b/src/App/Helper/StringDocHelper.php index 6577874..22417ef 100644 --- a/src/App/Helper/StringDocHelper.php +++ b/src/App/Helper/StringDocHelper.php @@ -11,6 +11,33 @@ */ class StringDocHelper { + const CONSTRAINT_WITH_FORMAT_FROM_CLASSNAME = [ + Assert\Bic::class, + Assert\CardScheme::class, + Assert\Country::class, + Assert\Currency::class, + Assert\Date::class, + Assert\DateTime::class, + Assert\Range::class, + Assert\Email::class, + Assert\File::class, + Assert\Iban::class, + Assert\Ip::class, + Assert\Isbn::class, + Assert\Issn::class, + Assert\Language::class, + Assert\Locale::class, + Assert\Luhn::class, + Assert\Time::class, + Assert\Url::class, + Assert\Uuid::class, + ]; + + const CONSTRAINT_WITH_FORMAT_FROM_PROPERTY = [ + Assert\Regex::class => 'pattern', + Assert\Expression::class => 'expression', + ]; + /** * @param TypeDoc $doc * @param Constraint $constraint @@ -19,72 +46,59 @@ class StringDocHelper */ public function append(TypeDoc $doc, Constraint $constraint) : void { - // If format already defined or type is defined and is not a string nor scalar => give up + // If not a string nor scalar => give up if (!$doc instanceof StringDoc) { return; } $constraintClass = get_class($constraint); - $constraintForFormatList = [ - Assert\Bic::class, - Assert\CardScheme::class, - Assert\Country::class, - Assert\Currency::class, - Assert\Date::class, - Assert\DateTime::class, - Assert\Email::class, - Assert\File::class, - Assert\Iban::class, - Assert\Ip::class, - Assert\Isbn::class, - Assert\Issn::class, - Assert\Language::class, - Assert\Locale::class, - Assert\Luhn::class, - Assert\Time::class, - Assert\Url::class, - Assert\Uuid::class, - ]; - if (in_array($constraintClass, $constraintForFormatList)) { - if (Assert\DateTime::class === $constraintClass) { - $format = 'datetime'; - } else { - $format = lcfirst((new \ReflectionClass($constraint))->getShortName()); - } - $doc->setFormat($format); + if (in_array($constraintClass, self::CONSTRAINT_WITH_FORMAT_FROM_CLASSNAME)) { + $this->enhanceFromClassName($doc, $constraint); + } elseif (in_array($constraintClass, self::CONSTRAINT_WITH_FORMAT_FROM_PROPERTY)) { + $propertyName = self::CONSTRAINT_WITH_FORMAT_FROM_PROPERTY[$constraintClass]; + $doc->setFormat($constraint->{$propertyName}); + } + } - if ($constraint instanceof Assert\Uuid) { - $formatDescription = sprintf( - '%s (%s)', - ucfirst($format), - implode( - ', ', - array_map( - function ($version) { - return sprintf('v%s', $version); - }, - $constraint->versions - ) - ) - ); - $doc->setDescription( - sprintf( - '%s%s%s', - $doc->getDescription(), - strlen($doc->getDescription()) ? ' ' : '', - $formatDescription - ) - ); - } - } elseif ($constraint instanceof Assert\Regex) { - $doc->setFormat($constraint->pattern); - } elseif ($constraint instanceof Assert\Range) { - // If it's a string range it must be a date range check (either it must be an integer or float value) - $doc->setFormat('datetime'); - } elseif ($constraint instanceof Assert\Expression) { + /** + * @param StringDoc $doc + * @param Constraint $constraint + * + * @throws \ReflectionException + */ + private function enhanceFromClassName(StringDoc $doc, Constraint $constraint): void + { + if ($constraint instanceof Assert\DateTime || $constraint instanceof Assert\Range) { // If it's a string range it must be a date range check (either it must be an integer or float value) - $doc->setFormat($constraint->expression); + $format = 'datetime'; + } else { + $format = lcfirst((new \ReflectionClass($constraint))->getShortName()); + } + $doc->setFormat($format); + + if ($constraint instanceof Assert\Uuid) { + $formatDescription = sprintf( + '%s (%s)', + ucfirst($format), + implode( + ', ', + array_map( + function ($version) { + return sprintf('v%s', $version); + }, + $constraint->versions + ) + ) + ); + $doc->setDescription( + sprintf( + '%s%s%s', + $doc->getDescription(), + strlen($doc->getDescription()) ? ' ' : '', + $formatDescription + ) + ); } } } diff --git a/src/Infra/Transformer/ConstraintToParamsDocTransformer.php b/src/Infra/Transformer/ConstraintToParamsDocTransformer.php index c535afa..57a0bc1 100644 --- a/src/Infra/Transformer/ConstraintToParamsDocTransformer.php +++ b/src/Infra/Transformer/ConstraintToParamsDocTransformer.php @@ -25,6 +25,17 @@ class ConstraintToParamsDocTransformer /** @var ConstraintPayloadDocHelper */ private $constraintPayloadDocHelper; + const CONSTRAINT_WITH_ALLOWED_VALUE_LIST = [ + Assert\IsTrue::class => [true, 1, '1'], + Assert\IsFalse::class => [false, 0, '0'], + Assert\IsNull::class => [null], + ]; + + const CONSTRAINT_WITH_ALLOWED_VALUE_LIST_FROM_PROPERTY = [ + Assert\IdenticalTo::class => 'value', + Assert\EqualTo::class => 'value', + ]; + /** * @param DocTypeHelper $docTypeHelper * @param StringDocHelper $stringDocHelper @@ -128,29 +139,17 @@ private function appendCollectionDoc(TypeDoc $doc, Constraint $constraint) : voi */ private function appendValidItemListDoc(TypeDoc $doc, Constraint $constraint) : void { + $constraintClass = get_class($constraint); if ($constraint instanceof Assert\Choice) { - if ($constraint->callback && is_callable($constraint->callback)) { - $choiceList = call_user_func($constraint->callback); - } else { - $choiceList = $constraint->choices ?? []; - } - foreach ($choiceList as $choice) { - $this->addToAllowedValueListIfNotExist($doc, $choice); - } - } elseif ($constraint instanceof Assert\IsNull) { - $this->addToAllowedValueListIfNotExist($doc, null); - } elseif ($constraint instanceof Assert\IdenticalTo) { - $this->addToAllowedValueListIfNotExist($doc, $constraint->value); - } elseif ($constraint instanceof Assert\IsTrue) { - $this->addToAllowedValueListIfNotExist($doc, true); - $this->addToAllowedValueListIfNotExist($doc, 1); - $this->addToAllowedValueListIfNotExist($doc, '1'); - } elseif ($constraint instanceof Assert\IsFalse) { - $this->addToAllowedValueListIfNotExist($doc, false); - $this->addToAllowedValueListIfNotExist($doc, 0); - $this->addToAllowedValueListIfNotExist($doc, '0'); - } elseif ($constraint instanceof Assert\EqualTo) { - $this->addToAllowedValueListIfNotExist($doc, $constraint->value); + $this->appendChoiceAllowedValue($doc, $constraint); + } elseif (in_array($constraintClass, self::CONSTRAINT_WITH_ALLOWED_VALUE_LIST_FROM_PROPERTY)) { + $propertyName = self::CONSTRAINT_WITH_ALLOWED_VALUE_LIST_FROM_PROPERTY[$constraintClass]; + $this->addToAllowedValueListIfNotExist($doc, $constraint->{$propertyName}); + } elseif (in_array($constraintClass, self::CONSTRAINT_WITH_ALLOWED_VALUE_LIST)) { + $this->addListToAllowedValueListIfNotExist( + $doc, + self::CONSTRAINT_WITH_ALLOWED_VALUE_LIST[$constraintClass] + ); } } @@ -192,6 +191,13 @@ class_uses($object) return count(array_intersect($actualClassList, $classList)) > 0; } + private function addListToAllowedValueListIfNotExist(TypeDoc $doc, array $valueList) : void + { + foreach ($valueList as $value) { + $this->addToAllowedValueListIfNotExist($doc, $value); + } + } + private function addToAllowedValueListIfNotExist(TypeDoc $doc, $value) : void { if (!in_array($value, $doc->getAllowedValueList(), true)) { @@ -248,4 +254,18 @@ private function basicAppendToDoc(TypeDoc $doc, Constraint $constraint): void $doc->setExample($doc->getExample() ?? $exampleValue); } } + + /** + * @param TypeDoc $doc + * @param Assert\Choice $constraint + */ + private function appendChoiceAllowedValue(TypeDoc $doc, Assert\Choice $constraint): void + { + if ($constraint->callback && is_callable($constraint->callback)) { + $choiceList = call_user_func($constraint->callback); + } else { + $choiceList = $constraint->choices ?? []; + } + $this->addListToAllowedValueListIfNotExist($doc, $choiceList); + } } From 4e88ec88656e2346dc0558f3629f890df86f40f3 Mon Sep 17 00:00:00 2001 From: Yoanm Date: Sat, 20 Apr 2019 18:27:55 +0200 Subject: [PATCH 2/8] Improve --- src/App/Helper/StringDocHelper.php | 52 +++++++++---------- .../ConstraintToParamsDocTransformer.php | 12 ++++- 2 files changed, 36 insertions(+), 28 deletions(-) diff --git a/src/App/Helper/StringDocHelper.php b/src/App/Helper/StringDocHelper.php index 22417ef..f3d2246 100644 --- a/src/App/Helper/StringDocHelper.php +++ b/src/App/Helper/StringDocHelper.php @@ -2,7 +2,7 @@ namespace Yoanm\JsonRpcParamsSymfonyConstraintDoc\App\Helper; use Symfony\Component\Validator\Constraint; -use Symfony\Component\Validator\Constraints as Assert; +use Symfony\Component\Validator\Constraints; use Yoanm\JsonRpcServerDoc\Domain\Model\Type\StringDoc; use Yoanm\JsonRpcServerDoc\Domain\Model\Type\TypeDoc; @@ -12,30 +12,30 @@ class StringDocHelper { const CONSTRAINT_WITH_FORMAT_FROM_CLASSNAME = [ - Assert\Bic::class, - Assert\CardScheme::class, - Assert\Country::class, - Assert\Currency::class, - Assert\Date::class, - Assert\DateTime::class, - Assert\Range::class, - Assert\Email::class, - Assert\File::class, - Assert\Iban::class, - Assert\Ip::class, - Assert\Isbn::class, - Assert\Issn::class, - Assert\Language::class, - Assert\Locale::class, - Assert\Luhn::class, - Assert\Time::class, - Assert\Url::class, - Assert\Uuid::class, + Constraints\Bic::class => true, + Constraints\CardScheme::class => true, + Constraints\Country::class => true, + Constraints\Currency::class => true, + Constraints\Date::class => true, + Constraints\DateTime::class => true, + Constraints\Range::class => true, + Constraints\Email::class => true, + Constraints\File::class => true, + Constraints\Iban::class => true, + Constraints\Ip::class => true, + Constraints\Isbn::class => true, + Constraints\Issn::class => true, + Constraints\Language::class => true, + Constraints\Locale::class => true, + Constraints\Luhn::class => true, + Constraints\Time::class => true, + Constraints\Url::class => true, + Constraints\Uuid::class => true, ]; const CONSTRAINT_WITH_FORMAT_FROM_PROPERTY = [ - Assert\Regex::class => 'pattern', - Assert\Expression::class => 'expression', + Constraints\Regex::class => 'pattern', + Constraints\Expression::class => 'expression', ]; /** @@ -53,9 +53,9 @@ public function append(TypeDoc $doc, Constraint $constraint) : void $constraintClass = get_class($constraint); - if (in_array($constraintClass, self::CONSTRAINT_WITH_FORMAT_FROM_CLASSNAME)) { + if (array_key_exists($constraintClass, self::CONSTRAINT_WITH_FORMAT_FROM_CLASSNAME)) { $this->enhanceFromClassName($doc, $constraint); - } elseif (in_array($constraintClass, self::CONSTRAINT_WITH_FORMAT_FROM_PROPERTY)) { + } elseif (array_key_exists($constraintClass, self::CONSTRAINT_WITH_FORMAT_FROM_PROPERTY)) { $propertyName = self::CONSTRAINT_WITH_FORMAT_FROM_PROPERTY[$constraintClass]; $doc->setFormat($constraint->{$propertyName}); } @@ -69,7 +69,7 @@ public function append(TypeDoc $doc, Constraint $constraint) : void */ private function enhanceFromClassName(StringDoc $doc, Constraint $constraint): void { - if ($constraint instanceof Assert\DateTime || $constraint instanceof Assert\Range) { + if ($constraint instanceof Constraints\DateTime || $constraint instanceof Constraints\Range) { // If it's a string range it must be a date range check (either it must be an integer or float value) $format = 'datetime'; } else { @@ -77,7 +77,7 @@ private function enhanceFromClassName(StringDoc $doc, Constraint $constraint): v } $doc->setFormat($format); - if ($constraint instanceof Assert\Uuid) { + if ($constraint instanceof Constraints\Uuid) { $formatDescription = sprintf( '%s (%s)', ucfirst($format), diff --git a/src/Infra/Transformer/ConstraintToParamsDocTransformer.php b/src/Infra/Transformer/ConstraintToParamsDocTransformer.php index 57a0bc1..d59131b 100644 --- a/src/Infra/Transformer/ConstraintToParamsDocTransformer.php +++ b/src/Infra/Transformer/ConstraintToParamsDocTransformer.php @@ -142,10 +142,10 @@ private function appendValidItemListDoc(TypeDoc $doc, Constraint $constraint) : $constraintClass = get_class($constraint); if ($constraint instanceof Assert\Choice) { $this->appendChoiceAllowedValue($doc, $constraint); - } elseif (in_array($constraintClass, self::CONSTRAINT_WITH_ALLOWED_VALUE_LIST_FROM_PROPERTY)) { + } elseif (array_key_exists($constraintClass, self::CONSTRAINT_WITH_ALLOWED_VALUE_LIST_FROM_PROPERTY)) { $propertyName = self::CONSTRAINT_WITH_ALLOWED_VALUE_LIST_FROM_PROPERTY[$constraintClass]; $this->addToAllowedValueListIfNotExist($doc, $constraint->{$propertyName}); - } elseif (in_array($constraintClass, self::CONSTRAINT_WITH_ALLOWED_VALUE_LIST)) { + } elseif (array_key_exists($constraintClass, self::CONSTRAINT_WITH_ALLOWED_VALUE_LIST)) { $this->addListToAllowedValueListIfNotExist( $doc, self::CONSTRAINT_WITH_ALLOWED_VALUE_LIST[$constraintClass] @@ -191,6 +191,10 @@ class_uses($object) return count(array_intersect($actualClassList, $classList)) > 0; } + /** + * @param TypeDoc $doc + * @param mixed[] $valueList + */ private function addListToAllowedValueListIfNotExist(TypeDoc $doc, array $valueList) : void { foreach ($valueList as $value) { @@ -198,6 +202,10 @@ private function addListToAllowedValueListIfNotExist(TypeDoc $doc, array $valueL } } + /** + * @param TypeDoc $doc + * @param mixed $value + */ private function addToAllowedValueListIfNotExist(TypeDoc $doc, $value) : void { if (!in_array($value, $doc->getAllowedValueList(), true)) { From 878c4b8fb0218b15c8eb02fcd9f8aef9604da560 Mon Sep 17 00:00:00 2001 From: Yoanm Date: Sat, 20 Apr 2019 19:43:01 +0200 Subject: [PATCH 3/8] Polish --- src/App/Helper/ClassComparatorTrait.php | 32 +++++ src/App/Helper/DocTypeHelper.php | 83 +++++++------ src/App/Helper/MinMaxHelper.php | 110 ++++++++++++------ src/App/Helper/StringDocHelper.php | 55 ++++----- src/App/Helper/TypeGuesser.php | 78 +++++-------- .../ConstraintToParamsDocTransformer.php | 72 +++++------- 6 files changed, 244 insertions(+), 186 deletions(-) create mode 100644 src/App/Helper/ClassComparatorTrait.php diff --git a/src/App/Helper/ClassComparatorTrait.php b/src/App/Helper/ClassComparatorTrait.php new file mode 100644 index 0000000..7430e35 --- /dev/null +++ b/src/App/Helper/ClassComparatorTrait.php @@ -0,0 +1,32 @@ + ScalarDoc::class, + 'string' => StringDoc::class, + 'bool' => BooleanDoc::class, + 'boolean' => BooleanDoc::class, + 'int' => IntegerDoc::class, + 'integer' => IntegerDoc::class, + 'float' => FloatDoc::class, + 'long' => FloatDoc::class, + 'double' => FloatDoc::class, + 'real' => FloatDoc::class, + 'numeric' => NumberDoc::class, + 'array' => ArrayDoc::class, + 'object' => ObjectDoc::class, + ]; + /** * @param ConstraintPayloadDocHelper $constraintPayloadDocHelper * @param TypeGuesser $typeGuesser @@ -56,23 +72,7 @@ protected function getDocFromTypeConstraintOrPayloadDocIfExist(array $constraint $doc = null; // Check if a Type constraint exist or if a constraint have a type documentation foreach ($constraintList as $constraint) { - if (null !== ($typeFromPayload = $this->constraintPayloadDocHelper->getTypeIfExist($constraint))) { - $doc = $this->normalizeType($typeFromPayload); - } elseif ($constraint instanceof Assert\Type) { - $doc = $this->normalizeType(strtolower($constraint->type)); - } elseif ($constraint instanceof Assert\Existence && count($constraint->constraints) > 0) { - $doc = $this->guess($constraint->constraints); - } elseif ($constraint instanceof Assert\IdenticalTo) { - // Strict comparison so value define the type - $doc = $this->normalizeType(gettype($constraint->value)); - } elseif ($constraint instanceof Assert\Callback) { - $callbackResult = call_user_func($constraint->callback); - $doc = $this->guess( - is_array($callbackResult) - ? $callbackResult - : [$callbackResult] - ); - } + $doc = $this->createDocFromConstraint($constraint); if (null !== $doc) { break; @@ -89,24 +89,41 @@ protected function getDocFromTypeConstraintOrPayloadDocIfExist(array $constraint */ private function normalizeType(string $type) : ?TypeDoc { - if ('scalar' === $type) { - return new ScalarDoc(); - } elseif ('string' === $type) { - return new StringDoc(); - } elseif (in_array($type, ['bool', 'boolean'])) { - return new BooleanDoc(); - } elseif (in_array($type, ['int', 'integer'])) { - return new IntegerDoc(); - } elseif (in_array($type, ['float', 'long', 'double', 'real'])) { - return new FloatDoc(); - } elseif ('numeric' === $type) { - return new NumberDoc(); - } elseif ('array' === $type) { - return new ArrayDoc(); - } elseif ('object' === $type) { - return new ObjectDoc(); + if (array_key_exists($type, self::MANAGED_TYPE_CLASS_LIST)) { + $class = self::MANAGED_TYPE_CLASS_LIST[$type]; + + return new $class(); } return null; } + + /** + * @param Constraint $constraint + * + * @return TypeDoc|null + */ + private function createDocFromConstraint(Constraint $constraint) : ?TypeDoc + { + $doc = null; + + if (null !== ($typeFromPayload = $this->constraintPayloadDocHelper->getTypeIfExist($constraint))) { + $doc = $this->normalizeType($typeFromPayload); + } elseif ($constraint instanceof Assert\Type) { + $doc = $this->normalizeType(strtolower($constraint->type)); + } elseif ($constraint instanceof Assert\IdenticalTo) {// Strict comparison so value define the type + $doc = $this->normalizeType(gettype($constraint->value)); + } elseif ($constraint instanceof Assert\Existence && count($constraint->constraints) > 0) { + $doc = $this->guess($constraint->constraints); + } elseif ($constraint instanceof Assert\Callback) { + $callbackResult = call_user_func($constraint->callback); + $doc = $this->guess( + is_array($callbackResult) + ? $callbackResult + : [$callbackResult] + ); + } + + return $doc; + } } diff --git a/src/App/Helper/MinMaxHelper.php b/src/App/Helper/MinMaxHelper.php index 3dfe19c..fc00a32 100644 --- a/src/App/Helper/MinMaxHelper.php +++ b/src/App/Helper/MinMaxHelper.php @@ -34,20 +34,19 @@ public function append(TypeDoc $doc, Constraint $constraint) : void */ private function appendStringDoc(StringDoc $doc, Constraint $constraint) : void { + $min = $max = null; if ($constraint instanceof Assert\Length) { - if (null !== $constraint->min) { - $doc->setMinLength((int) $constraint->min); - } - if (null !== $constraint->max) { - $doc->setMaxLength((int) $constraint->max); - } + $min = $constraint->min; + $max = $constraint->max; } elseif ($constraint instanceof Assert\NotBlank && null === $doc->getMinLength()) { // Not blank so minimum 1 character - $doc->setMinLength(1); + $min = 1; } elseif ($constraint instanceof Assert\Blank && null === $doc->getMaxLength()) { // Blank so maximum 0 character - $doc->setMaxLength(0); + $max = 0; } + + $this->setMinMaxLengthIfNotNull($doc, $min, $max); } /** @@ -71,21 +70,19 @@ private function appendNumberDoc(NumberDoc $doc, Constraint $constraint) : void */ private function appendCollectionDoc(CollectionDoc $doc, Constraint $constraint) : void { + $min = $max = null; if ($constraint instanceof Assert\Choice || $constraint instanceof Assert\Count) { - if (null !== $constraint->min) { - $doc->setMinItem((int) $constraint->min); - } - if (null !== $constraint->max) { - $doc->setMaxItem((int) $constraint->max); - } + $min = $constraint->min; + $max = $constraint->max; } elseif ($constraint instanceof Assert\NotBlank && null === $doc->getMinItem()) { // Not blank so minimum 1 item - $doc->setMinItem(1); + $min = 1; } /* Documentation does not mention array, counter to NotBlank constraint elseif ($constraint instanceof Assert\Blank && null === $doc->getMaxItem()) { // Blank so maximum 0 item - $doc->setMaxItem(0); + $max = 0; }*/ + $this->setMinMaxItemIfNotNull($doc, $min, $max); $this->appendLessGreaterThanMinMaxItem($doc, $constraint); } @@ -95,22 +92,21 @@ private function appendCollectionDoc(CollectionDoc $doc, Constraint $constraint) */ private function appendNumberMinMax(NumberDoc $doc, Constraint $constraint) : void { + $min = $max = null; if ($constraint instanceof Assert\Range) { - if (null !== $constraint->min) { - $doc->setMin($constraint->min); - } - if (null !== $constraint->max) { - $doc->setMax($constraint->max); - } + $min = $constraint->min; + $max = $constraint->max; } elseif ($constraint instanceof Assert\LessThanOrEqual || $constraint instanceof Assert\LessThan ) { - $doc->setMax($constraint->value); + $max = $constraint->value; } elseif ($constraint instanceof Assert\GreaterThanOrEqual || $constraint instanceof Assert\GreaterThan ) { - $doc->setMin($constraint->value); + $min = $constraint->value; } + + $this->setMinMaxIfNotNull($doc, $min, $max); } /** @@ -119,18 +115,64 @@ private function appendNumberMinMax(NumberDoc $doc, Constraint $constraint) : vo */ private function appendLessGreaterThanMinMaxItem(CollectionDoc $doc, Constraint $constraint): void { + $min = $max = null; if ($constraint instanceof Assert\GreaterThan || $constraint instanceof Assert\GreaterThanOrEqual) { - $doc->setMinItem( - $constraint instanceof Assert\GreaterThanOrEqual - ? $constraint->value - : $constraint->value + 1 - ); + $min = $constraint instanceof Assert\GreaterThanOrEqual + ? $constraint->value + : ($constraint->value + 1) + ; } elseif ($constraint instanceof Assert\LessThan || $constraint instanceof Assert\LessThanOrEqual) { - $doc->setMaxItem( - $constraint instanceof Assert\LessThanOrEqual - ? $constraint->value - : $constraint->value - 1 - ); + $max = $constraint instanceof Assert\LessThanOrEqual + ? $constraint->value + : $constraint->value - 1 + ; + } + + $this->setMinMaxItemIfNotNull($doc, $min, $max); + } + + /** + * @param StringDoc $doc + * @param null|int $min + * @param null|int $max + */ + private function setMinMaxLengthIfNotNull(StringDoc $doc, $min, $max): void + { + if (null !== $min) { + $doc->setMinLength((int)$min); + } + if (null !== $max) { + $doc->setMaxLength((int)$max); + } + } + + /** + * @param CollectionDoc $doc + * @param null|int $min + * @param null|int $max + */ + private function setMinMaxItemIfNotNull(CollectionDoc $doc, $min, $max): void + { + if (null !== $min) { + $doc->setMinItem((int) $min); + } + if (null !== $max) { + $doc->setMaxItem((int) $max); + } + } + + /** + * @param NumberDoc $doc + * @param null|int|float $min + * @param null|int|float $max + */ + private function setMinMaxIfNotNull(NumberDoc $doc, $min, $max): void + { + if (null !== $min) { + $doc->setMin($min); + } + if (null !== $max) { + $doc->setMax($max); } } } diff --git a/src/App/Helper/StringDocHelper.php b/src/App/Helper/StringDocHelper.php index f3d2246..6edfa87 100644 --- a/src/App/Helper/StringDocHelper.php +++ b/src/App/Helper/StringDocHelper.php @@ -11,26 +11,28 @@ */ class StringDocHelper { + use ClassComparatorTrait; + const CONSTRAINT_WITH_FORMAT_FROM_CLASSNAME = [ - Constraints\Bic::class => true, - Constraints\CardScheme::class => true, - Constraints\Country::class => true, - Constraints\Currency::class => true, - Constraints\Date::class => true, - Constraints\DateTime::class => true, - Constraints\Range::class => true, - Constraints\Email::class => true, - Constraints\File::class => true, - Constraints\Iban::class => true, - Constraints\Ip::class => true, - Constraints\Isbn::class => true, - Constraints\Issn::class => true, - Constraints\Language::class => true, - Constraints\Locale::class => true, - Constraints\Luhn::class => true, - Constraints\Time::class => true, - Constraints\Url::class => true, - Constraints\Uuid::class => true, + Constraints\Bic::class, + Constraints\CardScheme::class, + Constraints\Country::class, + Constraints\Currency::class, + Constraints\Date::class, + Constraints\DateTime::class, + Constraints\Range::class, + Constraints\Email::class, + Constraints\File::class, + Constraints\Iban::class, + Constraints\Ip::class, + Constraints\Isbn::class, + Constraints\Issn::class, + Constraints\Language::class, + Constraints\Locale::class, + Constraints\Luhn::class, + Constraints\Time::class, + Constraints\Url::class, + Constraints\Uuid::class, ]; const CONSTRAINT_WITH_FORMAT_FROM_PROPERTY = [ @@ -51,13 +53,13 @@ public function append(TypeDoc $doc, Constraint $constraint) : void return; } - $constraintClass = get_class($constraint); - - if (array_key_exists($constraintClass, self::CONSTRAINT_WITH_FORMAT_FROM_CLASSNAME)) { + if (null !== $this->getMatchingClassNameIn($constraint, self::CONSTRAINT_WITH_FORMAT_FROM_CLASSNAME)) { $this->enhanceFromClassName($doc, $constraint); - } elseif (array_key_exists($constraintClass, self::CONSTRAINT_WITH_FORMAT_FROM_PROPERTY)) { - $propertyName = self::CONSTRAINT_WITH_FORMAT_FROM_PROPERTY[$constraintClass]; - $doc->setFormat($constraint->{$propertyName}); + } elseif (null !== ($match = $this->getMatchingClassNameIn( + $constraint, + array_keys(self::CONSTRAINT_WITH_FORMAT_FROM_PROPERTY) + ))) { + $doc->setFormat($constraint->{self::CONSTRAINT_WITH_FORMAT_FROM_PROPERTY[$match]}); } } @@ -69,7 +71,8 @@ public function append(TypeDoc $doc, Constraint $constraint) : void */ private function enhanceFromClassName(StringDoc $doc, Constraint $constraint): void { - if ($constraint instanceof Constraints\DateTime || $constraint instanceof Constraints\Range) { + static $dateTimeClassList = [Constraints\DateTime::class, Constraints\Range::class]; + if (null !== $this->getMatchingClassNameIn($constraint, $dateTimeClassList)) { // If it's a string range it must be a date range check (either it must be an integer or float value) $format = 'datetime'; } else { diff --git a/src/App/Helper/TypeGuesser.php b/src/App/Helper/TypeGuesser.php index 4d7969f..034fafa 100644 --- a/src/App/Helper/TypeGuesser.php +++ b/src/App/Helper/TypeGuesser.php @@ -17,6 +17,33 @@ */ class TypeGuesser { + use ClassComparatorTrait; + + const STRING_CONSTRAINT_CLASS_LIST = [ + Assert\Length::class, // << Applied on string only + Assert\Date::class, // << validator expect a string with specific format + Assert\Time::class, // << validator expect a string with specific format + Assert\Bic::class, + Assert\CardScheme::class, + Assert\Country::class, + Assert\Currency::class, + Assert\Email::class, + Assert\File::class, + Assert\Iban::class, + Assert\Ip::class, + Assert\Isbn::class, + Assert\Issn::class, + Assert\Language::class, + Assert\Locale::class, + Assert\Luhn::class, + Assert\Url::class, + Assert\Uuid::class, + ]; + const BOOLEAN_CONSTRAINT_CLASS_LIST = [ + Assert\IsTrue::class, + Assert\IsFalse::class, + ]; + /** * @param array $constraintList * @@ -88,35 +115,10 @@ private function isAbstractType(TypeDoc $doc) : bool */ private function guessPrimaryTypeFromConstraint(Constraint $constraint) : ?TypeDoc { - static $stringConstraintClassList = [ - Assert\Length::class, // << Applied on string only - Assert\Date::class, // << validator expect a string with specific format - Assert\Time::class, // << validator expect a string with specific format - Assert\Bic::class, - Assert\CardScheme::class, - Assert\Country::class, - Assert\Currency::class, - Assert\Email::class, - Assert\File::class, - Assert\Iban::class, - Assert\Ip::class, - Assert\Isbn::class, - Assert\Issn::class, - Assert\Language::class, - Assert\Locale::class, - Assert\Luhn::class, - Assert\Url::class, - Assert\Uuid::class, - ]; - static $booleanConstraintClassList = [ - Assert\IsTrue::class, - Assert\IsFalse::class, - ]; - // Try to guess primary types - if ($this->isInstanceOfOneClassIn($constraint, $stringConstraintClassList)) { + if (null !== $this->getMatchingClassNameIn($constraint, self::STRING_CONSTRAINT_CLASS_LIST)) { return new StringDoc(); - } elseif ($this->isInstanceOfOneClassIn($constraint, $booleanConstraintClassList)) { + } elseif (null !== $this->getMatchingClassNameIn($constraint, self::BOOLEAN_CONSTRAINT_CLASS_LIST)) { return new BooleanDoc(); } elseif ($constraint instanceof Assert\DateTime) { return $this->guessDateTimeType($constraint); @@ -164,26 +166,4 @@ private function guestCollectionType(Assert\Collection $constraint) : TypeDoc return new ObjectDoc(); } - - /** - * @param $object - * @param array $classList - * - * @return bool - */ - private function isInstanceOfOneClassIn($object, array $classList) : bool - { - $actualClassList = array_merge( - [get_class($object)], - class_implements($object), - class_uses($object) - ); - $parentClass = get_parent_class($object); - while (false !== $parentClass) { - $actualClassList[] = $parentClass; - $parentClass = get_parent_class($parentClass); - } - - return count(array_intersect($actualClassList, $classList)) > 0; - } } diff --git a/src/Infra/Transformer/ConstraintToParamsDocTransformer.php b/src/Infra/Transformer/ConstraintToParamsDocTransformer.php index d59131b..921659e 100644 --- a/src/Infra/Transformer/ConstraintToParamsDocTransformer.php +++ b/src/Infra/Transformer/ConstraintToParamsDocTransformer.php @@ -3,6 +3,7 @@ use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\Constraints as Assert; +use Yoanm\JsonRpcParamsSymfonyConstraintDoc\App\Helper\ClassComparatorTrait; use Yoanm\JsonRpcParamsSymfonyConstraintDoc\App\Helper\ConstraintPayloadDocHelper; use Yoanm\JsonRpcParamsSymfonyConstraintDoc\App\Helper\DocTypeHelper; use Yoanm\JsonRpcParamsSymfonyConstraintDoc\App\Helper\MinMaxHelper; @@ -16,6 +17,8 @@ */ class ConstraintToParamsDocTransformer { + use ClassComparatorTrait; + /** @var DocTypeHelper */ private $docTypeHelper; /** @var StringDocHelper */ @@ -36,6 +39,16 @@ class ConstraintToParamsDocTransformer Assert\EqualTo::class => 'value', ]; + const NOT_NULL_CONSTRAINT_LIST = [ + Assert\NotNull::class, + Assert\IsTrue::class, // If it is true, it cannot be null ... + Assert\IsFalse::class, // If it is false, it cannot be null ... + // If should be identical to something, it cannot be null (but can be identical to null) + Assert\IdenticalTo::class, + ]; + + + /** * @param DocTypeHelper $docTypeHelper * @param StringDocHelper $stringDocHelper @@ -139,17 +152,21 @@ private function appendCollectionDoc(TypeDoc $doc, Constraint $constraint) : voi */ private function appendValidItemListDoc(TypeDoc $doc, Constraint $constraint) : void { - $constraintClass = get_class($constraint); if ($constraint instanceof Assert\Choice) { $this->appendChoiceAllowedValue($doc, $constraint); - } elseif (array_key_exists($constraintClass, self::CONSTRAINT_WITH_ALLOWED_VALUE_LIST_FROM_PROPERTY)) { - $propertyName = self::CONSTRAINT_WITH_ALLOWED_VALUE_LIST_FROM_PROPERTY[$constraintClass]; - $this->addToAllowedValueListIfNotExist($doc, $constraint->{$propertyName}); - } elseif (array_key_exists($constraintClass, self::CONSTRAINT_WITH_ALLOWED_VALUE_LIST)) { - $this->addListToAllowedValueListIfNotExist( + } elseif (null !== ($match = $this->getMatchingClassNameIn( + $constraint, + array_keys(self::CONSTRAINT_WITH_ALLOWED_VALUE_LIST_FROM_PROPERTY) + ))) { + $this->addToAllowedValueListIfNotExist( $doc, - self::CONSTRAINT_WITH_ALLOWED_VALUE_LIST[$constraintClass] + $constraint->{self::CONSTRAINT_WITH_ALLOWED_VALUE_LIST_FROM_PROPERTY[$match]} ); + } elseif (null !== ($match = $this->getMatchingClassNameIn( + $constraint, + array_keys(self::CONSTRAINT_WITH_ALLOWED_VALUE_LIST) + ))) { + $this->addListToAllowedValueListIfNotExist($doc, self::CONSTRAINT_WITH_ALLOWED_VALUE_LIST[$match]); } } @@ -169,28 +186,6 @@ private function appendAllConstraintToDoc(ArrayDoc $doc, Assert\All $constraint) $doc->setItemValidation($itemDoc); } - /** - * @param $object - * @param array $classList - * - * @return bool - */ - private function isInstanceOfOneClassIn($object, array $classList) : bool - { - $actualClassList = array_merge( - [get_class($object)], - class_implements($object), - class_uses($object) - ); - $parentClass = get_parent_class($object); - while (false !== $parentClass) { - $actualClassList[] = $parentClass; - $parentClass = get_parent_class($parentClass); - } - - return count(array_intersect($actualClassList, $classList)) > 0; - } - /** * @param TypeDoc $doc * @param mixed[] $valueList @@ -221,14 +216,6 @@ private function addToAllowedValueListIfNotExist(TypeDoc $doc, $value) : void */ private function basicAppendToDoc(TypeDoc $doc, Constraint $constraint): void { - static $notNullConstraintList = [ - Assert\NotNull::class, - Assert\IsTrue::class, // If it is true, it cannot be null ... - Assert\IsFalse::class, // If it is false, it cannot be null ... - // If should be identical to something, it cannot be null (but can be identical to null) - Assert\IdenticalTo::class, - ]; - $this->stringDocHelper->append($doc, $constraint); $this->appendCollectionDoc($doc, $constraint); @@ -240,12 +227,9 @@ private function basicAppendToDoc(TypeDoc $doc, Constraint $constraint): void foreach ($constraint->constraints as $subConstraint) { $this->appendToDoc($doc, $subConstraint); } - } elseif ($this->isInstanceOfOneClassIn($constraint, $notNullConstraintList)) { - $doc->setNullable( - ($constraint instanceof Assert\IdenticalTo) - ? is_null($constraint->value) - : false - ); + } elseif (null !== $this->getMatchingClassNameIn($constraint, self::NOT_NULL_CONSTRAINT_LIST)) { + $isIdenticalTo = $constraint instanceof Assert\IdenticalTo; + $doc->setNullable($isIdenticalTo ? is_null($constraint->value) : false); $defaultValue = $exampleValue = null; switch (true) { case $constraint instanceof Assert\IsTrue: @@ -254,7 +238,7 @@ private function basicAppendToDoc(TypeDoc $doc, Constraint $constraint): void case $constraint instanceof Assert\IsFalse: $defaultValue = $exampleValue = false; break; - case $constraint instanceof Assert\IdenticalTo: + case $isIdenticalTo: $defaultValue = $exampleValue = $constraint->value; break; } From fa757477522c1921378b751a74373129c6a6868a Mon Sep 17 00:00:00 2001 From: Yoanm Date: Sat, 20 Apr 2019 19:48:57 +0200 Subject: [PATCH 4/8] Fix scrutinizer issue --- src/App/Helper/MinMaxHelper.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/App/Helper/MinMaxHelper.php b/src/App/Helper/MinMaxHelper.php index fc00a32..7f5417f 100644 --- a/src/App/Helper/MinMaxHelper.php +++ b/src/App/Helper/MinMaxHelper.php @@ -132,9 +132,9 @@ private function appendLessGreaterThanMinMaxItem(CollectionDoc $doc, Constraint } /** - * @param StringDoc $doc - * @param null|int $min - * @param null|int $max + * @param StringDoc $doc + * @param null|int|mixed $min + * @param null|int|mixed $max */ private function setMinMaxLengthIfNotNull(StringDoc $doc, $min, $max): void { @@ -147,9 +147,9 @@ private function setMinMaxLengthIfNotNull(StringDoc $doc, $min, $max): void } /** - * @param CollectionDoc $doc - * @param null|int $min - * @param null|int $max + * @param CollectionDoc $doc + * @param null|int|mixed $min + * @param null|int|mixed $max */ private function setMinMaxItemIfNotNull(CollectionDoc $doc, $min, $max): void { @@ -162,9 +162,9 @@ private function setMinMaxItemIfNotNull(CollectionDoc $doc, $min, $max): void } /** - * @param NumberDoc $doc - * @param null|int|float $min - * @param null|int|float $max + * @param NumberDoc $doc + * @param null|int||mixed $min + * @param null|int||mixed $max */ private function setMinMaxIfNotNull(NumberDoc $doc, $min, $max): void { From 5388abb0d1a78f97c07556bf5124d239ffa6c669 Mon Sep 17 00:00:00 2001 From: Yoanm Date: Sat, 20 Apr 2019 20:03:52 +0200 Subject: [PATCH 5/8] Improve --- src/App/Helper/DocTypeHelper.php | 23 +++++-- src/App/Helper/MinMaxHelper.php | 15 +++-- src/App/Helper/TypeGuesser.php | 19 ++++-- .../ConstraintToParamsDocTransformer.php | 65 ++++++++++++------- 4 files changed, 83 insertions(+), 39 deletions(-) diff --git a/src/App/Helper/DocTypeHelper.php b/src/App/Helper/DocTypeHelper.php index ffdc60b..f7aab73 100644 --- a/src/App/Helper/DocTypeHelper.php +++ b/src/App/Helper/DocTypeHelper.php @@ -116,14 +116,25 @@ private function createDocFromConstraint(Constraint $constraint) : ?TypeDoc } elseif ($constraint instanceof Assert\Existence && count($constraint->constraints) > 0) { $doc = $this->guess($constraint->constraints); } elseif ($constraint instanceof Assert\Callback) { - $callbackResult = call_user_func($constraint->callback); - $doc = $this->guess( - is_array($callbackResult) - ? $callbackResult - : [$callbackResult] - ); + $doc = $this->getTypeFromCallbackConstraint($constraint); } return $doc; } + + /** + * @param Assert\Callback $constraint + * + * @return TypeDoc + */ + private function getTypeFromCallbackConstraint(Assert\Callback $constraint): TypeDoc + { + $callbackResult = call_user_func($constraint->callback); + $doc = $this->guess( + is_array($callbackResult) + ? $callbackResult + : [$callbackResult] + ); + return $doc; +} } diff --git a/src/App/Helper/MinMaxHelper.php b/src/App/Helper/MinMaxHelper.php index 7f5417f..3531142 100644 --- a/src/App/Helper/MinMaxHelper.php +++ b/src/App/Helper/MinMaxHelper.php @@ -13,6 +13,8 @@ */ class MinMaxHelper { + use ClassComparatorTrait; + /** * @param TypeDoc $doc * @param Constraint $constraint @@ -116,13 +118,14 @@ private function appendNumberMinMax(NumberDoc $doc, Constraint $constraint) : vo private function appendLessGreaterThanMinMaxItem(CollectionDoc $doc, Constraint $constraint): void { $min = $max = null; - if ($constraint instanceof Assert\GreaterThan || $constraint instanceof Assert\GreaterThanOrEqual) { - $min = $constraint instanceof Assert\GreaterThanOrEqual + $gtConstraintList = [Assert\GreaterThan::class, Assert\GreaterThanOrEqual::class]; + $ltConstraintList = [Assert\LessThan::class, Assert\LessThanOrEqual::class]; + if (null !== ($match = $this->getMatchingClassNameIn($constraint, $gtConstraintList))) { + $min = ($match === Assert\GreaterThanOrEqual::class) ? $constraint->value - : ($constraint->value + 1) - ; - } elseif ($constraint instanceof Assert\LessThan || $constraint instanceof Assert\LessThanOrEqual) { - $max = $constraint instanceof Assert\LessThanOrEqual + : $constraint->value + 1; + } elseif (null !== ($match = $this->getMatchingClassNameIn($constraint, $ltConstraintList))) { + $max = ($match === Assert\LessThanOrEqual::class) ? $constraint->value : $constraint->value - 1 ; diff --git a/src/App/Helper/TypeGuesser.php b/src/App/Helper/TypeGuesser.php index 034fafa..3cf33d5 100644 --- a/src/App/Helper/TypeGuesser.php +++ b/src/App/Helper/TypeGuesser.php @@ -126,11 +126,7 @@ private function guessPrimaryTypeFromConstraint(Constraint $constraint) : ?TypeD return $this->guestCollectionType($constraint); } elseif ($constraint instanceof Assert\Regex) { return new ScalarDoc(); - } elseif ($constraint instanceof Assert\All // << Applied only on array - || ($constraint instanceof Assert\Choice - && true === $constraint->multiple // << expect an array multiple choices - ) - ) { + } elseif ($this->isArrayConstraint($constraint)) { return new ArrayDoc(); } @@ -166,4 +162,17 @@ private function guestCollectionType(Assert\Collection $constraint) : TypeDoc return new ObjectDoc(); } + + /** + * @param Constraint $constraint + * + * @return bool + */ + private function isArrayConstraint(Constraint $constraint): bool + { + return $constraint instanceof Assert\All // << Applied only on array + || ($constraint instanceof Assert\Choice + && true === $constraint->multiple // << expect an array multiple choices + ); +} } diff --git a/src/Infra/Transformer/ConstraintToParamsDocTransformer.php b/src/Infra/Transformer/ConstraintToParamsDocTransformer.php index 921659e..7930c7a 100644 --- a/src/Infra/Transformer/ConstraintToParamsDocTransformer.php +++ b/src/Infra/Transformer/ConstraintToParamsDocTransformer.php @@ -39,7 +39,7 @@ class ConstraintToParamsDocTransformer Assert\EqualTo::class => 'value', ]; - const NOT_NULL_CONSTRAINT_LIST = [ + const NULL_NOT_NULL_CONSTRAINT_LIST = [ Assert\NotNull::class, Assert\IsTrue::class, // If it is true, it cannot be null ... Assert\IsFalse::class, // If it is false, it cannot be null ... @@ -223,27 +223,9 @@ private function basicAppendToDoc(TypeDoc $doc, Constraint $constraint): void $this->appendValidItemListDoc($doc, $constraint); if ($constraint instanceof Assert\Existence) { - $doc->setRequired($constraint instanceof Assert\Required); - foreach ($constraint->constraints as $subConstraint) { - $this->appendToDoc($doc, $subConstraint); - } - } elseif (null !== $this->getMatchingClassNameIn($constraint, self::NOT_NULL_CONSTRAINT_LIST)) { - $isIdenticalTo = $constraint instanceof Assert\IdenticalTo; - $doc->setNullable($isIdenticalTo ? is_null($constraint->value) : false); - $defaultValue = $exampleValue = null; - switch (true) { - case $constraint instanceof Assert\IsTrue: - $defaultValue = $exampleValue = true; - break; - case $constraint instanceof Assert\IsFalse: - $defaultValue = $exampleValue = false; - break; - case $isIdenticalTo: - $defaultValue = $exampleValue = $constraint->value; - break; - } - $doc->setDefault($doc->getDefault() ?? $defaultValue); - $doc->setExample($doc->getExample() ?? $exampleValue); + $this->appendExistenceConstraintData($doc, $constraint); + } elseif (null !== ($match = $this->getMatchingClassNameIn($constraint, self::NULL_NOT_NULL_CONSTRAINT_LIST))) { + $this->setNulNotNullConstraintData($doc, $constraint, $match); } } @@ -260,4 +242,43 @@ private function appendChoiceAllowedValue(TypeDoc $doc, Assert\Choice $constrain } $this->addListToAllowedValueListIfNotExist($doc, $choiceList); } + + /** + * @param TypeDoc $doc + * @param Constraint $constraint + * @param string $sanitizedClass + */ + private function setNulNotNullConstraintData(TypeDoc $doc, Constraint $constraint, string $sanitizedClass): void + { + $isIdenticalTo = $sanitizedClass === Assert\IdenticalTo::class; + $doc->setNullable($isIdenticalTo ? is_null($constraint->value) : false); + $defaultValue = $exampleValue = null; + switch (true) { + case $sanitizedClass === Assert\IsTrue::class: + $defaultValue = $exampleValue = true; + break; + case $sanitizedClass === Assert\IsFalse::class: + $defaultValue = $exampleValue = false; + break; + case $isIdenticalTo: + $defaultValue = $exampleValue = $constraint->value; + break; + } + $doc->setDefault($doc->getDefault() ?? $defaultValue); + $doc->setExample($doc->getExample() ?? $exampleValue); + } + + /** + * @param TypeDoc $doc + * @param Assert\Existence $constraint + * + * @throws \ReflectionException + */ + private function appendExistenceConstraintData(TypeDoc $doc, Assert\Existence $constraint): void + { + $doc->setRequired($constraint instanceof Assert\Required); + foreach ($constraint->constraints as $subConstraint) { + $this->appendToDoc($doc, $subConstraint); + } + } } From 9f5097e2ea374194d574e152eadcd0a5d3a5ace2 Mon Sep 17 00:00:00 2001 From: Yoanm Date: Sat, 20 Apr 2019 20:06:35 +0200 Subject: [PATCH 6/8] Fix phpcs --- src/App/Helper/DocTypeHelper.php | 2 +- src/App/Helper/TypeGuesser.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/App/Helper/DocTypeHelper.php b/src/App/Helper/DocTypeHelper.php index f7aab73..81b8786 100644 --- a/src/App/Helper/DocTypeHelper.php +++ b/src/App/Helper/DocTypeHelper.php @@ -136,5 +136,5 @@ private function getTypeFromCallbackConstraint(Assert\Callback $constraint): Typ : [$callbackResult] ); return $doc; -} + } } diff --git a/src/App/Helper/TypeGuesser.php b/src/App/Helper/TypeGuesser.php index 3cf33d5..121177a 100644 --- a/src/App/Helper/TypeGuesser.php +++ b/src/App/Helper/TypeGuesser.php @@ -174,5 +174,5 @@ private function isArrayConstraint(Constraint $constraint): bool || ($constraint instanceof Assert\Choice && true === $constraint->multiple // << expect an array multiple choices ); -} + } } From 0f35d7c23a34f2ce59bf8ba98cbe26f3e332bc08 Mon Sep 17 00:00:00 2001 From: Yoanm Date: Sat, 20 Apr 2019 20:16:25 +0200 Subject: [PATCH 7/8] Polish --- src/App/Helper/DocTypeHelper.php | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/src/App/Helper/DocTypeHelper.php b/src/App/Helper/DocTypeHelper.php index 81b8786..44b0763 100644 --- a/src/App/Helper/DocTypeHelper.php +++ b/src/App/Helper/DocTypeHelper.php @@ -107,16 +107,12 @@ private function createDocFromConstraint(Constraint $constraint) : ?TypeDoc { $doc = null; - if (null !== ($typeFromPayload = $this->constraintPayloadDocHelper->getTypeIfExist($constraint))) { - $doc = $this->normalizeType($typeFromPayload); - } elseif ($constraint instanceof Assert\Type) { - $doc = $this->normalizeType(strtolower($constraint->type)); - } elseif ($constraint instanceof Assert\IdenticalTo) {// Strict comparison so value define the type - $doc = $this->normalizeType(gettype($constraint->value)); - } elseif ($constraint instanceof Assert\Existence && count($constraint->constraints) > 0) { - $doc = $this->guess($constraint->constraints); + if (null !== ($stringType = $this->getStringType($constraint))) { + $doc = $this->normalizeType($stringType); } elseif ($constraint instanceof Assert\Callback) { $doc = $this->getTypeFromCallbackConstraint($constraint); + } elseif ($constraint instanceof Assert\Existence && count($constraint->constraints) > 0) { + $doc = $this->guess($constraint->constraints); } return $doc; @@ -137,4 +133,23 @@ private function getTypeFromCallbackConstraint(Assert\Callback $constraint): Typ ); return $doc; } + + /** + * @param Constraint $constraint + * + * @return string|null + */ + private function getStringType(Constraint $constraint) : ?string + { + $stringType = null; + if (null !== ($typeFromPayload = $this->constraintPayloadDocHelper->getTypeIfExist($constraint))) { + $stringType = $typeFromPayload; + } elseif ($constraint instanceof Assert\Type) { + $stringType = strtolower($constraint->type); + } elseif ($constraint instanceof Assert\IdenticalTo) {// Strict comparison so value define the type + $stringType = gettype($constraint->value); + } + + return $stringType; + } } From f3bdc57bc8c87f62161965005bb180b1951e9a38 Mon Sep 17 00:00:00 2001 From: Yoanm Date: Sat, 20 Apr 2019 20:19:54 +0200 Subject: [PATCH 8/8] Fix scrutinizer issue --- src/App/Helper/MinMaxHelper.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/App/Helper/MinMaxHelper.php b/src/App/Helper/MinMaxHelper.php index 3531142..c11bd31 100644 --- a/src/App/Helper/MinMaxHelper.php +++ b/src/App/Helper/MinMaxHelper.php @@ -165,9 +165,9 @@ private function setMinMaxItemIfNotNull(CollectionDoc $doc, $min, $max): void } /** - * @param NumberDoc $doc - * @param null|int||mixed $min - * @param null|int||mixed $max + * @param NumberDoc $doc + * @param null|int|mixed $min + * @param null|int|mixed $max */ private function setMinMaxIfNotNull(NumberDoc $doc, $min, $max): void {