From a4b0f450e020bb6a5e84b2787de3c0b8918a4b50 Mon Sep 17 00:00:00 2001 From: Mathis Burger Date: Fri, 10 May 2024 16:59:04 +0200 Subject: [PATCH 1/6] feature #1557 [make:entity] Added prompt for default value if possible --- src/DefaultValueValidator.php | 77 ++++++++++++++++++++ src/Maker/MakeEntity.php | 10 +++ src/Util/ClassSource/Model/ClassProperty.php | 3 + src/Util/ClassSourceManipulator.php | 3 +- 4 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 src/DefaultValueValidator.php diff --git a/src/DefaultValueValidator.php b/src/DefaultValueValidator.php new file mode 100644 index 000000000..388630a44 --- /dev/null +++ b/src/DefaultValueValidator.php @@ -0,0 +1,77 @@ +nullable = true; } + + if (in_array($classProperty->type, DefaultValueValidator::SUPPORTED_TYPES)) { + $defaultValue = $io->ask('Please enter your default value (press if no default value)',null, DefaultValueValidator::getValidator($classProperty->type)); + if ($defaultValue !== null) { + $classProperty->defaultValue = $defaultValue; + $classProperty->options['default'] = $classProperty->defaultValue; + } + } + return $classProperty; } diff --git a/src/Util/ClassSource/Model/ClassProperty.php b/src/Util/ClassSource/Model/ClassProperty.php index 220c42029..f1622c1ee 100644 --- a/src/Util/ClassSource/Model/ClassProperty.php +++ b/src/Util/ClassSource/Model/ClassProperty.php @@ -33,6 +33,7 @@ public function __construct( public ?int $scale = null, public bool $needsTypeHint = true, public bool $unique = false, + public mixed $defaultValue = null, ) { } @@ -74,6 +75,7 @@ public static function createFromObject(FieldMapping|array $data): self precision: $data->precision, scale: $data->scale, unique: $data->unique ?? false, + defaultValue: $data->defaultValue ?? null ); } @@ -93,6 +95,7 @@ public static function createFromObject(FieldMapping|array $data): self precision: $data['precision'] ?? null, scale: $data['scale'] ?? null, unique: $data['unique'] ?? false, + defaultValue: $data['defaultValue'] ?? null ); } } diff --git a/src/Util/ClassSourceManipulator.php b/src/Util/ClassSourceManipulator.php index e93aae2ca..d935d49b5 100644 --- a/src/Util/ClassSourceManipulator.php +++ b/src/Util/ClassSourceManipulator.php @@ -118,12 +118,13 @@ public function addEntityField(ClassProperty $mapping): void $nullable = $mapping->nullable ?? false; $attributes[] = $this->buildAttributeNode(Column::class, $mapping->getAttributes(), 'ORM'); - $defaultValue = null; if ('array' === $typeHint && !$nullable) { $defaultValue = new Node\Expr\Array_([], ['kind' => Node\Expr\Array_::KIND_SHORT]); } elseif ($typeHint && '\\' === $typeHint[0] && false !== strpos($typeHint, '\\', 1)) { $typeHint = $this->addUseStatementIfNecessary(substr($typeHint, 1)); + } else if ($mapping->defaultValue) { + $defaultValue = $mapping->defaultValue; } $propertyType = $typeHint; From 5a29142753a7d58e2d164d9b80660bd3572e1a04 Mon Sep 17 00:00:00 2001 From: Mathis Burger Date: Fri, 10 May 2024 17:05:45 +0200 Subject: [PATCH 2/6] feature #1557 [make:entity][style] Fixed code style --- src/DefaultValueValidator.php | 41 ++++++++++++++++++----------- src/Maker/MakeEntity.php | 7 +++-- src/Util/ClassSourceManipulator.php | 2 +- 3 files changed, 29 insertions(+), 21 deletions(-) diff --git a/src/DefaultValueValidator.php b/src/DefaultValueValidator.php index 388630a44..32874e3e9 100644 --- a/src/DefaultValueValidator.php +++ b/src/DefaultValueValidator.php @@ -1,17 +1,25 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Symfony\Bundle\MakerBundle; use Symfony\Bundle\MakerBundle\Exception\RuntimeCommandException; /** - * Validator made for default values + * Validator made for default values. */ class DefaultValueValidator { - /** - * All currently supported types with default value + * All currently supported types with default value. */ public const SUPPORTED_TYPES = [ 'string', @@ -21,13 +29,14 @@ class DefaultValueValidator 'integer', 'smallint', 'bigint', - 'float' + 'float', ]; /** - * Gets the validator that belongs to type + * Gets the validator that belongs to type. * * @param string $type The type in doctrine format + * * @return callable|null The validator that belongs to type */ public static function getValidator(string $type): ?callable @@ -42,36 +51,36 @@ public static function getValidator(string $type): ?callable case 'bigint': return self::validateInt(...); } + return null; } - public static function validateBoolean(mixed $value): bool { - if (in_array($value, ['true', 'false'])) { - return $value === 'true'; + if (\in_array($value, ['true', 'false'])) { + return 'true' === $value; } - throw new RuntimeCommandException(sprintf("Value %s is invalid for type boolean", $value)); + throw new RuntimeCommandException(sprintf('Value %s is invalid for type boolean', $value)); } public static function validateFloat(mixed $value): float { if (is_numeric($value)) { - return floatval($value); + return (float) $value; } - throw new RuntimeCommandException(sprintf("Value %s is invalid for type float", $value)); + throw new RuntimeCommandException(sprintf('Value %s is invalid for type float', $value)); } public static function validateInt(mixed $value): int { if (is_numeric($value)) { - $val = intval($value); - if ($value === '0' && $val === 0) { + $val = (int) $value; + if ('0' === $value && 0 === $val) { return $val; - } else if ($val !== 0) { + } elseif (0 !== $val) { return $val; } } - throw new RuntimeCommandException(sprintf("Value %s is invalid for type int", $value)); + throw new RuntimeCommandException(sprintf('Value %s is invalid for type int', $value)); } -} \ No newline at end of file +} diff --git a/src/Maker/MakeEntity.php b/src/Maker/MakeEntity.php index 9650f66ec..13edfb4ce 100644 --- a/src/Maker/MakeEntity.php +++ b/src/Maker/MakeEntity.php @@ -437,10 +437,9 @@ private function askForNextField(ConsoleStyle $io, array $fields, string $entity $classProperty->nullable = true; } - - if (in_array($classProperty->type, DefaultValueValidator::SUPPORTED_TYPES)) { - $defaultValue = $io->ask('Please enter your default value (press if no default value)',null, DefaultValueValidator::getValidator($classProperty->type)); - if ($defaultValue !== null) { + if (\in_array($classProperty->type, DefaultValueValidator::SUPPORTED_TYPES)) { + $defaultValue = $io->ask('Please enter your default value (press if no default value)', null, DefaultValueValidator::getValidator($classProperty->type)); + if (null !== $defaultValue) { $classProperty->defaultValue = $defaultValue; $classProperty->options['default'] = $classProperty->defaultValue; } diff --git a/src/Util/ClassSourceManipulator.php b/src/Util/ClassSourceManipulator.php index d935d49b5..a74a263a0 100644 --- a/src/Util/ClassSourceManipulator.php +++ b/src/Util/ClassSourceManipulator.php @@ -123,7 +123,7 @@ public function addEntityField(ClassProperty $mapping): void $defaultValue = new Node\Expr\Array_([], ['kind' => Node\Expr\Array_::KIND_SHORT]); } elseif ($typeHint && '\\' === $typeHint[0] && false !== strpos($typeHint, '\\', 1)) { $typeHint = $this->addUseStatementIfNecessary(substr($typeHint, 1)); - } else if ($mapping->defaultValue) { + } elseif ($mapping->defaultValue) { $defaultValue = $mapping->defaultValue; } From 9a596c7298f41b6c41efaa06e42bd12029e14429 Mon Sep 17 00:00:00 2001 From: Mathis Burger Date: Fri, 10 May 2024 17:20:01 +0200 Subject: [PATCH 3/6] feature #1557 [make:entity][test] Updated tests for default values --- tests/Maker/MakeEntityTest.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/Maker/MakeEntityTest.php b/tests/Maker/MakeEntityTest.php index 87fbb1c9e..69d5a02c4 100644 --- a/tests/Maker/MakeEntityTest.php +++ b/tests/Maker/MakeEntityTest.php @@ -83,6 +83,8 @@ public function getTestDetails(): \Generator '', // nullable '', + // no default value + '', // no more properties '', ]); @@ -216,6 +218,8 @@ public function getTestDetails(): \Generator '', // length (default 255) // nullable 'y', + // no default value + '', // finish adding fields '', ]); @@ -623,6 +627,8 @@ public function getTestDetails(): \Generator '', // length (default 255) // nullable '', + // no default value + '', // finish adding fields '', ], '--overwrite'); From 4557390a8c24b747113d77a6f5beb2844d8758a1 Mon Sep 17 00:00:00 2001 From: Mathis Burger Date: Tue, 19 Nov 2024 21:30:44 +0100 Subject: [PATCH 4/6] chore: fixed linting and some small errors --- src/{ => Maker}/DefaultValueValidator.php | 8 ++++---- src/Maker/MakeEntity.php | 1 - src/Util/ClassSource/Model/ClassProperty.php | 4 ++-- 3 files changed, 6 insertions(+), 7 deletions(-) rename src/{ => Maker}/DefaultValueValidator.php (84%) diff --git a/src/DefaultValueValidator.php b/src/Maker/DefaultValueValidator.php similarity index 84% rename from src/DefaultValueValidator.php rename to src/Maker/DefaultValueValidator.php index 32874e3e9..388fe1b8c 100644 --- a/src/DefaultValueValidator.php +++ b/src/Maker/DefaultValueValidator.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace Symfony\Bundle\MakerBundle; +namespace Symfony\Bundle\MakerBundle\Maker; use Symfony\Bundle\MakerBundle\Exception\RuntimeCommandException; @@ -60,7 +60,7 @@ public static function validateBoolean(mixed $value): bool if (\in_array($value, ['true', 'false'])) { return 'true' === $value; } - throw new RuntimeCommandException(sprintf('Value %s is invalid for type boolean', $value)); + throw new RuntimeCommandException(\sprintf('Value %s is invalid for type boolean', $value)); } public static function validateFloat(mixed $value): float @@ -68,7 +68,7 @@ public static function validateFloat(mixed $value): float if (is_numeric($value)) { return (float) $value; } - throw new RuntimeCommandException(sprintf('Value %s is invalid for type float', $value)); + throw new RuntimeCommandException(\sprintf('Value %s is invalid for type float', $value)); } public static function validateInt(mixed $value): int @@ -81,6 +81,6 @@ public static function validateInt(mixed $value): int return $val; } } - throw new RuntimeCommandException(sprintf('Value %s is invalid for type int', $value)); + throw new RuntimeCommandException(\sprintf('Value %s is invalid for type int', $value)); } } diff --git a/src/Maker/MakeEntity.php b/src/Maker/MakeEntity.php index b75dceb16..23e13d223 100644 --- a/src/Maker/MakeEntity.php +++ b/src/Maker/MakeEntity.php @@ -14,7 +14,6 @@ use ApiPlatform\Metadata\ApiResource; use Doctrine\DBAL\Types\Type; use Symfony\Bundle\MakerBundle\ConsoleStyle; -use Symfony\Bundle\MakerBundle\DefaultValueValidator; use Symfony\Bundle\MakerBundle\DependencyBuilder; use Symfony\Bundle\MakerBundle\Doctrine\DoctrineHelper; use Symfony\Bundle\MakerBundle\Doctrine\EntityClassGenerator; diff --git a/src/Util/ClassSource/Model/ClassProperty.php b/src/Util/ClassSource/Model/ClassProperty.php index d9a290816..1b94792fa 100644 --- a/src/Util/ClassSource/Model/ClassProperty.php +++ b/src/Util/ClassSource/Model/ClassProperty.php @@ -80,7 +80,7 @@ public static function createFromObject(FieldMapping|array $data): self precision: $data->precision, scale: $data->scale, unique: $data->unique ?? false, - defaultValue: $data->defaultValue ?? null + defaultValue: $data->defaultValue ?? null, enumType: $data->enumType, ); } @@ -101,7 +101,7 @@ enumType: $data->enumType, precision: $data['precision'] ?? null, scale: $data['scale'] ?? null, unique: $data['unique'] ?? false, - defaultValue: $data['defaultValue'] ?? null + defaultValue: $data['defaultValue'] ?? null, enumType: $data['enumType'] ?? null, ); } From 219d22864820e219445d9d9cc60c7a25c3058148 Mon Sep 17 00:00:00 2001 From: Mathis Burger Date: Tue, 19 Nov 2024 21:47:04 +0100 Subject: [PATCH 5/6] fix: Moved validator to different namespace --- src/{Maker => }/DefaultValueValidator.php | 2 +- src/Maker/MakeEntity.php | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) rename src/{Maker => }/DefaultValueValidator.php (98%) diff --git a/src/Maker/DefaultValueValidator.php b/src/DefaultValueValidator.php similarity index 98% rename from src/Maker/DefaultValueValidator.php rename to src/DefaultValueValidator.php index 388fe1b8c..87e1204f9 100644 --- a/src/Maker/DefaultValueValidator.php +++ b/src/DefaultValueValidator.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace Symfony\Bundle\MakerBundle\Maker; +namespace Symfony\Bundle\MakerBundle; use Symfony\Bundle\MakerBundle\Exception\RuntimeCommandException; diff --git a/src/Maker/MakeEntity.php b/src/Maker/MakeEntity.php index 23e13d223..b75dceb16 100644 --- a/src/Maker/MakeEntity.php +++ b/src/Maker/MakeEntity.php @@ -14,6 +14,7 @@ use ApiPlatform\Metadata\ApiResource; use Doctrine\DBAL\Types\Type; use Symfony\Bundle\MakerBundle\ConsoleStyle; +use Symfony\Bundle\MakerBundle\DefaultValueValidator; use Symfony\Bundle\MakerBundle\DependencyBuilder; use Symfony\Bundle\MakerBundle\Doctrine\DoctrineHelper; use Symfony\Bundle\MakerBundle\Doctrine\EntityClassGenerator; From 68724a418617a379cf1af30e13b633618a6560a9 Mon Sep 17 00:00:00 2001 From: Mathis Burger Date: Tue, 19 Nov 2024 22:03:38 +0100 Subject: [PATCH 6/6] test: Fixed single test to add default value --- tests/Maker/MakeEntityTest.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/Maker/MakeEntityTest.php b/tests/Maker/MakeEntityTest.php index 994c867ec..ef5061b33 100644 --- a/tests/Maker/MakeEntityTest.php +++ b/tests/Maker/MakeEntityTest.php @@ -738,6 +738,8 @@ public function getTestDetails(): \Generator '', // nullable 'y', + // no default value + '', // finish adding fields '', ]);