From 08e465fcc52e322f77c5d2cb3519d91de01c9d2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ji=C5=99=C3=AD=20Pudil?= <me@jiripudil.cz> Date: Mon, 11 Mar 2024 13:34:42 +0100 Subject: [PATCH] use constants for DefaultValue / Unchanged --- composer.json | 1 + ...aultValue.php => DefaultOrExistingValue.php} | 5 ++++- src/Scaffolding/TableImplementation.php | 17 ++++++++++++----- src/constants.php | 8 ++++++++ 4 files changed, 25 insertions(+), 6 deletions(-) rename src/{DefaultValue.php => DefaultOrExistingValue.php} (53%) create mode 100644 src/constants.php diff --git a/composer.json b/composer.json index e095005..6f14188 100644 --- a/composer.json +++ b/composer.json @@ -53,6 +53,7 @@ "src/exceptions.php" ], "files": [ + "src/constants.php", "src/functions.php", "src/Conditions/functions.php", "src/Types/functions.php" diff --git a/src/DefaultValue.php b/src/DefaultOrExistingValue.php similarity index 53% rename from src/DefaultValue.php rename to src/DefaultOrExistingValue.php index abdf2ba..fe4bf0e 100644 --- a/src/DefaultValue.php +++ b/src/DefaultOrExistingValue.php @@ -4,6 +4,9 @@ declare(strict_types=1); namespace Grifart\Tables; -final class DefaultValue +/** + * @internal + */ +final class DefaultOrExistingValue { } diff --git a/src/Scaffolding/TableImplementation.php b/src/Scaffolding/TableImplementation.php index 122d16e..a4f01d5 100644 --- a/src/Scaffolding/TableImplementation.php +++ b/src/Scaffolding/TableImplementation.php @@ -14,7 +14,7 @@ use Grifart\Tables\Column; use Grifart\Tables\ColumnMetadata; use Grifart\Tables\ColumnNotFound; use Grifart\Tables\Conditions\Condition; -use Grifart\Tables\DefaultValue; +use Grifart\Tables\DefaultOrExistingValue; use Grifart\Tables\Expression; use Grifart\Tables\RowNotFound; use Grifart\Tables\OrderBy\OrderBy; @@ -209,8 +209,8 @@ final class TableImplementation implements Capability $isNullable = $fieldType->isNullable(); if ($hasDefaultValue) { - $namespace->addUse(DefaultValue::class); - $fieldType = new UnionType($fieldType, resolve(DefaultValue::class)); + $namespace->addUse(DefaultOrExistingValue::class); + $fieldType = new UnionType($fieldType, resolve(DefaultOrExistingValue::class)); } @@ -219,7 +219,14 @@ final class TableImplementation implements Capability ->setNullable($isNullable); if ($hasDefaultValue) { - $parameter->setDefaultValue(new Code\Literal('new ?', [new Code\Literal($namespace->simplifyName(DefaultValue::class))])); + $parameter->setDefaultValue( + new Code\Literal( + $namespace->simplifyName( + $isEditMethod ? 'Grifart\Tables\Unchanged' : 'Grifart\Tables\DefaultValue', + $namespace::NameConstant, + ), + ), + ); } if ($fieldType->requiresDocComment()) { @@ -233,7 +240,7 @@ final class TableImplementation implements Capability if ($hasDefaultValue) { $method->addBody( 'if (!? instanceof ?) {', - [new Code\Literal('$' . $fieldName), new Code\Literal($namespace->simplifyName(DefaultValue::class))], + [new Code\Literal('$' . $fieldName), new Code\Literal($namespace->simplifyName(DefaultOrExistingValue::class))], ); } diff --git a/src/constants.php b/src/constants.php new file mode 100644 index 0000000..93fb528 --- /dev/null +++ b/src/constants.php @@ -0,0 +1,8 @@ +<?php + +declare(strict_types=1); + +namespace Grifart\Tables; + +const DefaultValue = new DefaultOrExistingValue(); +const Unchanged = new DefaultOrExistingValue(); -- GitLab