Skip to content

Commit 2673bf4

Browse files
authored
return mixed if we failed detect castable type (#1306)
fixed #1305
1 parent 771770e commit 2673bf4

File tree

5 files changed

+35
-1
lines changed

5 files changed

+35
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ All notable changes to this project will be documented in this file.
44

55
[Next release](https://github.com/barryvdh/laravel-ide-helper/compare/v2.12.0...master)
66
--------------
7+
### Fixed
8+
- Properly handle `Castable`s without return type. [#1306 / binotaliu](https://github.com/barryvdh/laravel-ide-helper/pull/1306)
79

810
2022-01-23, 2.12.0
911
------------------

src/Console/ModelsCommand.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1291,7 +1291,8 @@ protected function checkForCastableCasts(string $type, array $params = []): stri
12911291
$methodReflection = $castReflection->getMethod('get');
12921292

12931293
return $this->getReturnTypeFromReflection($methodReflection) ??
1294-
$this->getReturnTypeFromDocBlock($methodReflection, $reflection);
1294+
$this->getReturnTypeFromDocBlock($methodReflection, $reflection) ??
1295+
'mixed';
12951296
}
12961297

12971298
/**
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\Castable;
8+
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
9+
10+
class CastableWithoutReturnType implements Castable
11+
{
12+
public static function castUsing(array $arguments)
13+
{
14+
return new class() implements CastsAttributes {
15+
public function get($model, string $key, $value, array $attributes)
16+
{
17+
return new CastedProperty();
18+
}
19+
20+
public function set($model, string $key, $value, array $attributes)
21+
{
22+
// TODO: Implement set() method.
23+
}
24+
};
25+
}
26+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Casts\CastableReturnsAnonymousCaster;
88
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Casts\CastableReturnsCustomCaster;
9+
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Casts\CastableWithoutReturnType;
910
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Casts\CustomCasterWithDocblockReturn;
1011
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Casts\CustomCasterWithDocblockReturnFqn;
1112
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Casts\CustomCasterWithNullablePrimitiveReturn;
@@ -35,6 +36,7 @@ class CustomCast extends Model
3536
'casted_property_with_this_return_docblock' => SelfCastingCasterWithThisDocblockReturn::class,
3637
'casted_property_with_castable' => CastableReturnsCustomCaster::class,
3738
'casted_property_with_anonymous_cast' => CastableReturnsAnonymousCaster::class,
39+
'casted_property_without_return_type' => CastableWithoutReturnType::class,
3840
'extended_casted_property_with_static_return_docblock' => ExtendedSelfCastingCasterWithStaticDocblockReturn::class,
3941
'extended_casted_property_with_this_return_docblock' => ExtendedSelfCastingCasterWithThisDocblockReturn::class,
4042
'casted_property_with_static_return_docblock_and_param' => SelfCastingCasterWithStaticDocblockReturn::class . ':param',

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Casts\CastableReturnsAnonymousCaster;
88
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Casts\CastableReturnsCustomCaster;
9+
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Casts\CastableWithoutReturnType;
910
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Casts\CustomCasterWithDocblockReturn;
1011
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Casts\CustomCasterWithDocblockReturnFqn;
1112
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Casts\CustomCasterWithNullablePrimitiveReturn;
@@ -38,6 +39,7 @@
3839
* @property SelfCastingCasterWithStaticDocblockReturn $casted_property_with_static_return_docblock_and_param
3940
* @property \Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Casts\CastedProperty $casted_property_with_castable
4041
* @property \Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Casts\CastedProperty $casted_property_with_anonymous_cast
42+
* @property mixed $casted_property_without_return_type
4143
* @property \Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Casts\CastedProperty $cast_without_property
4244
* @method static \Illuminate\Database\Eloquent\Builder|CustomCast newModelQuery()
4345
* @method static \Illuminate\Database\Eloquent\Builder|CustomCast newQuery()
@@ -72,6 +74,7 @@ class CustomCast extends Model
7274
'casted_property_with_this_return_docblock' => SelfCastingCasterWithThisDocblockReturn::class,
7375
'casted_property_with_castable' => CastableReturnsCustomCaster::class,
7476
'casted_property_with_anonymous_cast' => CastableReturnsAnonymousCaster::class,
77+
'casted_property_without_return_type' => CastableWithoutReturnType::class,
7578
'extended_casted_property_with_static_return_docblock' => ExtendedSelfCastingCasterWithStaticDocblockReturn::class,
7679
'extended_casted_property_with_this_return_docblock' => ExtendedSelfCastingCasterWithThisDocblockReturn::class,
7780
'casted_property_with_static_return_docblock_and_param' => SelfCastingCasterWithStaticDocblockReturn::class . ':param',

0 commit comments

Comments
 (0)