Skip to content

Commit b3a79fe

Browse files
authored
Allow casts with a return type of static or this to reference themselves (#1103)
* Allow casts with a return type of static or this to reference themselves Supports ```php /** * @param $model * @param string $key * @param $value * @param array $attributes * @return static */ public function get($model, string $key, $value, array $attributes) { return new static($value); } ``` or ```php /** * @param $model * @param string $key * @param $value * @param array $attributes * @return $this */ public function get($model, string $key, $value, array $attributes) { return new static($value); } ``` * Update CHANGELOG.md
1 parent b4138e5 commit b3a79fe

9 files changed

+118
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ All notable changes to this project will be documented in this file.
1818
- Compatibility with Lumen [\#1043 / mfn](https://github.com/barryvdh/laravel-ide-helper/pull/1043)
1919
- Allow model_locations to have glob patterns [\#1059 / saackearl](https://github.com/barryvdh/laravel-ide-helper/pull/1059)
2020
- Error when generating helper for macroable classes which are not facades and contain a "fake" method [\#1066 / domkrm] (https://github.com/barryvdh/laravel-ide-helper/pull/1066)
21+
- Casts with a return type of `static` or `$this` now resolve to an instance of the cast [\#1103 / riesjart](https://github.com/barryvdh/laravel-ide-helper/pull/1103)
2122

2223
2020-09-07, 2.8.1
2324
-----------------

src/Console/ModelsCommand.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,13 +1089,17 @@ protected function checkForCustomLaravelCasts(string $type): ?string
10891089

10901090
$methodReflection = new \ReflectionMethod($type, 'get');
10911091

1092-
$type = $this->getReturnTypeFromReflection($methodReflection);
1092+
$reflectionType = $this->getReturnTypeFromReflection($methodReflection);
10931093

1094-
if ($type === null) {
1095-
$type = $this->getReturnTypeFromDocBlock($methodReflection);
1094+
if ($reflectionType === null) {
1095+
$reflectionType = $this->getReturnTypeFromDocBlock($methodReflection);
1096+
}
1097+
1098+
if($reflectionType === 'static' || $reflectionType === '$this') {
1099+
$reflectionType = $type;
10961100
}
10971101

1098-
return $type;
1102+
return $reflectionType;
10991103
}
11001104

11011105
protected function getTypeInModel(object $model, ?string $type): ?string
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Casts;
6+
7+
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
8+
9+
class ExtendedSelfCastingCasterWithStaticDocblockReturn extends SelfCastingCasterWithStaticDocblockReturn
10+
{
11+
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Casts;
6+
7+
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
8+
9+
class ExtendedSelfCastingCasterWithThisDocblockReturn extends SelfCastingCasterWithStaticDocblockReturn
10+
{
11+
12+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Casts;
6+
7+
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
8+
9+
class SelfCastingCasterWithStaticDocblockReturn implements CastsAttributes
10+
{
11+
/**
12+
* @return static
13+
*/
14+
public function get($model, string $key, $value, array $attributes)
15+
{
16+
return new static();
17+
}
18+
19+
/**
20+
* @inheritDoc
21+
*/
22+
public function set($model, string $key, $value, array $attributes)
23+
{
24+
// TODO: Implement set() method.
25+
}
26+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Casts;
6+
7+
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
8+
9+
class SelfCastingCasterWithThisDocblockReturn implements CastsAttributes
10+
{
11+
/**
12+
* @return $this
13+
*/
14+
public function get($model, string $key, $value, array $attributes)
15+
{
16+
return new static();
17+
}
18+
19+
/**
20+
* @inheritDoc
21+
*/
22+
public function set($model, string $key, $value, array $attributes)
23+
{
24+
// TODO: Implement set() method.
25+
}
26+
}

tests/Console/ModelsCommand/LaravelCustomCasts/Models/CustomCast.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Casts\CustomCasterWithPrimitiveDocblockReturn;
1313
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Casts\CustomCasterWithPrimitiveReturn;
1414
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Casts\CustomCasterWithReturnType;
15+
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Casts\ExtendedSelfCastingCasterWithStaticDocblockReturn;
16+
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Casts\ExtendedSelfCastingCasterWithThisDocblockReturn;
17+
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Casts\SelfCastingCasterWithStaticDocblockReturn;
18+
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Casts\SelfCastingCasterWithThisDocblockReturn;
1519
use Illuminate\Database\Eloquent\Model;
1620

1721
class CustomCast extends Model
@@ -25,5 +29,10 @@ class CustomCast extends Model
2529
'casted_property_with_return_nullable_primitive' => CustomCasterWithNullablePrimitiveReturn::class,
2630
'casted_property_without_return' => CustomCasterWithoutReturnType::class,
2731
'casted_property_with_param' => CustomCasterWithParam::class . ':param',
32+
'casted_property_with_static_return_docblock' => SelfCastingCasterWithStaticDocblockReturn::class,
33+
'casted_property_with_this_return_docblock' => SelfCastingCasterWithThisDocblockReturn::class,
34+
'extended_casted_property_with_static_return_docblock' => ExtendedSelfCastingCasterWithStaticDocblockReturn::class,
35+
'extended_casted_property_with_this_return_docblock' => ExtendedSelfCastingCasterWithThisDocblockReturn::class,
36+
'casted_property_with_static_return_docblock_and_param' => SelfCastingCasterWithStaticDocblockReturn::class .':param',
2837
];
2938
}

tests/Console/ModelsCommand/LaravelCustomCasts/__snapshots__/Test__test__1.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Casts\CustomCasterWithPrimitiveDocblockReturn;
1313
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Casts\CustomCasterWithPrimitiveReturn;
1414
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Casts\CustomCasterWithReturnType;
15+
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Casts\ExtendedSelfCastingCasterWithStaticDocblockReturn;
16+
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Casts\ExtendedSelfCastingCasterWithThisDocblockReturn;
17+
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Casts\SelfCastingCasterWithStaticDocblockReturn;
18+
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Casts\SelfCastingCasterWithThisDocblockReturn;
1519
use Illuminate\Database\Eloquent\Model;
1620

1721
/**
@@ -25,6 +29,11 @@
2529
* @property array|null $casted_property_with_return_nullable_primitive
2630
* @property $casted_property_without_return
2731
* @property \Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Casts\CastedProperty $casted_property_with_param
32+
* @property SelfCastingCasterWithStaticDocblockReturn $casted_property_with_static_return_docblock
33+
* @property SelfCastingCasterWithThisDocblockReturn $casted_property_with_this_return_docblock
34+
* @property ExtendedSelfCastingCasterWithStaticDocblockReturn $extended_casted_property_with_static_return_docblock
35+
* @property ExtendedSelfCastingCasterWithThisDocblockReturn $extended_casted_property_with_this_return_docblock
36+
* @property SelfCastingCasterWithStaticDocblockReturn $casted_property_with_static_return_docblock_and_param
2837
* @method static \Illuminate\Database\Eloquent\Builder|CustomCast newModelQuery()
2938
* @method static \Illuminate\Database\Eloquent\Builder|CustomCast newQuery()
3039
* @method static \Illuminate\Database\Eloquent\Builder|CustomCast query()
@@ -35,7 +44,12 @@
3544
* @method static \Illuminate\Database\Eloquent\Builder|CustomCast whereCastedPropertyWithReturnPrimitive($value)
3645
* @method static \Illuminate\Database\Eloquent\Builder|CustomCast whereCastedPropertyWithReturnPrimitiveDocblock($value)
3746
* @method static \Illuminate\Database\Eloquent\Builder|CustomCast whereCastedPropertyWithReturnType($value)
47+
* @method static \Illuminate\Database\Eloquent\Builder|CustomCast whereCastedPropertyWithStaticReturnDocblock($value)
48+
* @method static \Illuminate\Database\Eloquent\Builder|CustomCast whereCastedPropertyWithStaticReturnDocblockAndParam($value)
49+
* @method static \Illuminate\Database\Eloquent\Builder|CustomCast whereCastedPropertyWithThisReturnDocblock($value)
3850
* @method static \Illuminate\Database\Eloquent\Builder|CustomCast whereCastedPropertyWithoutReturn($value)
51+
* @method static \Illuminate\Database\Eloquent\Builder|CustomCast whereExtendedCastedPropertyWithStaticReturnDocblock($value)
52+
* @method static \Illuminate\Database\Eloquent\Builder|CustomCast whereExtendedCastedPropertyWithThisReturnDocblock($value)
3953
* @mixin \Eloquent
4054
*/
4155
class CustomCast extends Model
@@ -49,5 +63,10 @@ class CustomCast extends Model
4963
'casted_property_with_return_nullable_primitive' => CustomCasterWithNullablePrimitiveReturn::class,
5064
'casted_property_without_return' => CustomCasterWithoutReturnType::class,
5165
'casted_property_with_param' => CustomCasterWithParam::class . ':param',
66+
'casted_property_with_static_return_docblock' => SelfCastingCasterWithStaticDocblockReturn::class,
67+
'casted_property_with_this_return_docblock' => SelfCastingCasterWithThisDocblockReturn::class,
68+
'extended_casted_property_with_static_return_docblock' => ExtendedSelfCastingCasterWithStaticDocblockReturn::class,
69+
'extended_casted_property_with_this_return_docblock' => ExtendedSelfCastingCasterWithThisDocblockReturn::class,
70+
'casted_property_with_static_return_docblock_and_param' => SelfCastingCasterWithStaticDocblockReturn::class .':param',
5271
];
5372
}

tests/Console/ModelsCommand/migrations/____custom_casts_table.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ public function up(): void
1919
$table->string('casted_property_with_return_nullable_primitive');
2020
$table->string('casted_property_without_return');
2121
$table->string('casted_property_with_param');
22+
$table->string('casted_property_with_static_return_docblock');
23+
$table->string('casted_property_with_this_return_docblock');
24+
$table->string('extended_casted_property_with_static_return_docblock');
25+
$table->string('extended_casted_property_with_this_return_docblock');
26+
$table->string('casted_property_with_static_return_docblock_and_param');
2227
});
2328
}
2429
}

0 commit comments

Comments
 (0)