Skip to content

Commit

Permalink
Improve handling of read types on custom model casts
Browse files Browse the repository at this point in the history
  • Loading branch information
tadhgboyle committed Nov 11, 2022
1 parent 325d2f3 commit 947a975
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/Properties/ModelPropertyExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\ObjectType;
use ReflectionNamedType;

/**
* @internal
Expand Down Expand Up @@ -280,7 +281,24 @@ private function castPropertiesType(Model $modelInstance): void
$realType = '\Illuminate\Support\Collection<array-key, mixed>';
break;
default:
$realType = class_exists($type) ? ('\\'.$type) : 'mixed';
if (class_exists($type)) {
$realType = ('\\'.$type);
$reflection = $this->reflectionProvider
->getClass($realType)
->getNativeReflection();
if ($reflection->hasMethod('get')) {
if ($returnType = $reflection->getMethod('get')->getReturnType()) {
if ($returnType instanceof ReflectionNamedType) {
$realType = $returnType->getName();
if ($returnType->allowsNull()) {
$realType .= '|null';
}
}
}
}
} else {
$realType = 'mixed';
}
break;
}

Expand Down
16 changes: 16 additions & 0 deletions tests/Application/app/Money.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace App;

class Money
{
public function __construct(
private int $amount = 100,
) {
}

public function getAmount(): int
{
return $this->amount;
}
}
20 changes: 20 additions & 0 deletions tests/Application/app/MoneyCast.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace App;

class MoneyCast implements \Illuminate\Contracts\Database\Eloquent\CastsAttributes {

/**
* @inheritDoc
*/
public function get($model, string $key, $value, array $attributes): ?Money {
return new \App\Money($value);
}

/**
* @inheritDoc
*/
public function set($model, string $key, $value, array $attributes) {
// TODO: Implement set() method.
}
}
1 change: 1 addition & 0 deletions tests/Application/app/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class User extends Authenticatable
'floatButRoundedDecimalString' => 'decimal:1',
'options' => AsArrayObject::class,
'properties' => AsCollection::class,
'balance' => MoneyCast::class,
];

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public function up(): void
$table->json('properties');
$table->boolean('blocked');
$table->unknownColumnType('unknown_column');
$table->integer('balance');
$table->rememberToken();
$table->timestamps();
$table->softDeletes();
Expand Down
9 changes: 9 additions & 0 deletions tests/Features/Properties/ModelPropertyExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\Address;
use App\Group;
use App\GuardedModel;
use App\Money;
use App\Role;
use App\Team;
use App\Thread;
Expand Down Expand Up @@ -202,6 +203,14 @@ public function testSoftDeletesCastDateTimeAndNullable(User $user): ?string
return $user->deleted_at?->format('d/m/Y');
}

/** @return Money|null */
public function testCastClassReturnsDifferentClassInstanceOrNull(User $user)
{
assertType('App\\Money|null', $user->balance);

return $user->balance;
}

public function testWriteToSoftDeletesColumn(): void
{
$this->user->deleted_at = 'test';
Expand Down

0 comments on commit 947a975

Please sign in to comment.