Skip to content

Commit

Permalink
Split no-default off from SerdeError.
Browse files Browse the repository at this point in the history
  • Loading branch information
Crell committed Nov 2, 2023
1 parent 365df16 commit 0dbadc8
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 10 deletions.
10 changes: 5 additions & 5 deletions src/Attributes/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Crell\fp\Evolvable;
use Crell\Serde\FieldTypeIncompatible;
use Crell\Serde\IntersectionTypesNotSupported;
use Crell\Serde\PropValue;
use Crell\Serde\Renaming\LiteralName;
use Crell\Serde\Renaming\RenamingStrategy;
use Crell\Serde\SerdeError;
Expand Down Expand Up @@ -144,7 +145,7 @@ class Field implements FromReflectionProperty, HasSubAttributes, Excludable, Sup
public function __construct(
?string $serializedName = null,
?RenamingStrategy $renameWith = null,
mixed $default = SerdeError::Missing,
mixed $default = PropValue::None,
protected readonly bool $useDefault = true,
public readonly bool $flatten = false,
public readonly bool $exclude = false,
Expand All @@ -153,7 +154,7 @@ public function __construct(
?bool $requireValue = null,
protected readonly array $scopes = [null],
) {
if ($default !== SerdeError::Missing) {
if ($default !== PropValue::None) {
$this->defaultValue = $default;
$this->shouldUseDefault = true;
}
Expand Down Expand Up @@ -192,7 +193,7 @@ public function fromReflection(\ReflectionProperty $subject): void
$constructorDefault = $this->getDefaultValueFromConstructor($subject);

$this->shouldUseDefault
??= $this->useDefault && $constructorDefault !== SerdeError::NoDefaultValue;
??= $this->useDefault && $constructorDefault !== PropValue::None;

if ($this->shouldUseDefault) {
$this->defaultValue ??= $constructorDefault;
Expand Down Expand Up @@ -225,8 +226,7 @@ protected function getDefaultValueFromConstructor(\ReflectionProperty $subject):

return $param?->isDefaultValueAvailable()
? $param->getDefaultValue()
: SerdeError::NoDefaultValue;

: PropValue::None;
}

public function finalize(): void
Expand Down
1 change: 0 additions & 1 deletion src/SerdeError.php → src/DeformatterResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@
enum SerdeError
{
case Missing;
case NoDefaultValue;
}
9 changes: 9 additions & 0 deletions src/PropValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
declare(strict_types=1);

namespace Crell\Serde;

enum PropValue
{
case None;
}
2 changes: 0 additions & 2 deletions src/PropertyHandler/DictionaryExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,6 @@ public function importValue(Deserializer $deserializer, Field $field, mixed $sou
// We cannot easily tell them apart at the moment.
if ($typeField instanceof DictionaryField && $typeField->implodeOn) {
$val = $deserializer->deformatter->deserializeString($source, $field);
// This is already an exhaustive match, but PHPStan doesn't know that.
// @phpstan-ignore-next-line
return $val === SerdeError::Missing ? null : $typeField->explode($val);
}

Expand Down
2 changes: 0 additions & 2 deletions src/PropertyHandler/SequenceExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,6 @@ public function importValue(Deserializer $deserializer, Field $field, mixed $sou
// We cannot easily tell them apart at the moment.
if ($typeField instanceof SequenceField && $typeField->implodeOn) {
$val = $deserializer->deformatter->deserializeString($source, $field);
// This is already an exhaustive match, but PHPStan doesn't know that.
// @phpstan-ignore-next-line
return $val === SerdeError::Missing ? null : $typeField->explode($val);
}

Expand Down

0 comments on commit 0dbadc8

Please sign in to comment.