Skip to content

Commit

Permalink
Handle encrypted casts
Browse files Browse the repository at this point in the history
  • Loading branch information
erikgaal committed Dec 19, 2022
1 parent 69e9554 commit bdbea43
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions src/Properties/ModelCastHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Illuminate\Contracts\Database\Eloquent\CastsInboundAttributes;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use PHPStan\Reflection\ParameterReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Reflection\ReflectionProvider;
Expand All @@ -31,9 +30,9 @@ public function __construct(protected ReflectionProvider $reflectionProvider)

public function getReadableType(string $cast, Type $originalType): Type
{
$cast = Str::before($cast, ':');
$castType = $this->parseCast($cast);

$attributeType = match ($cast) {
$attributeType = match ($castType) {
'int', 'integer' => new IntegerType(),
'real', 'float', 'double' => new FloatType(),
'decimal' => TypeCombinator::intersect(new StringType(), new AccessoryNumericStringType()),
Expand All @@ -53,7 +52,7 @@ public function getReadableType(string $cast, Type $originalType): Type
}

if (! $this->reflectionProvider->hasClass($cast)) {
return new MixedType();
return $originalType;
}

$classReflection = $this->reflectionProvider->getClass($cast);
Expand Down Expand Up @@ -82,7 +81,9 @@ public function getReadableType(string $cast, Type $originalType): Type

public function getWriteableType(string $cast, Type $originalType): Type
{
$attributeType = match ($cast) {
$castType = $this->parseCast($cast);

$attributeType = match ($castType) {
'int', 'integer' => new IntegerType(),
'real', 'float', 'double' => new FloatType(),
'decimal' => TypeCombinator::intersect(new StringType(), new AccessoryNumericStringType()),
Expand All @@ -102,7 +103,7 @@ public function getWriteableType(string $cast, Type $originalType): Type
}

if (! $this->reflectionProvider->hasClass($cast)) {
return new MixedType();
return $originalType;
}

$classReflection = $this->reflectionProvider->getClass($cast);
Expand Down Expand Up @@ -143,4 +144,20 @@ public function getDateType(): Type

return new ObjectType($dateClass);
}

/**
* @param string $cast
* @return string|null
*/
private function parseCast(string $cast): ?string
{
foreach (explode(':', $cast) as $part) {
// If the cast is prefixed with `encrypted:` we need to skip to the next
if ($part === 'encrypted') continue;

return $part;
}

return null;
}
}

0 comments on commit bdbea43

Please sign in to comment.