Fix broken ReflectionUnionTypes (#1132)

* Fix exceptions on ReflectionUnionTypes
this commit fixes exceptions thrown on ReflectionUnionType::isBuiltIn() and ReflectionUnionType::getNme() called on php8 union types

* Fix exceptions on ReflectionUnionTypes
this commit fixes exceptions thrown on ReflectionUnionType::isBuiltIn() and ReflectionUnionType::getNme() called on php8 union types

* add check for php8.0

* fix static analysis error

* fix $types variable undefined

* fix failing test

* fix failing test

* fixed style with php-cs-fixer

* add test for union types in parameters and return type

* add test for nullable union types in parameters and return type

* updated CHANGELOG.md
This commit is contained in:
Fabio Ivona
2021-01-11 09:15:22 +01:00
committed by GitHub
parent 3376522d68
commit 73c502d5c5
5 changed files with 157 additions and 18 deletions
@@ -0,0 +1,32 @@
<?php
declare(strict_types=1);
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\UnionTypes\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Query\Builder;
class UnionTypeModel extends Model
{
public function scopeWithUnionTypeParameter(Builder $query, string|int $bar): Builder
{
return $query->where('foo', $bar);
}
public function scopeWithNullableUnionTypeParameter(Builder $query, null|string|int $bar): Builder
{
return $query->where('foo', $bar);
}
public function withUnionTypeReturn(): HasMany|UnionTypeModel
{
return $this->hasMany(UnionTypeModel::class);
}
public function getFooAttribute(): string|int|null
{
return $this->getAttribute('foo');
}
}
@@ -0,0 +1,34 @@
<?php
declare(strict_types=1);
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\UnionTypes;
use Barryvdh\LaravelIdeHelper\Console\ModelsCommand;
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\AbstractModelsCommand;
use Illuminate\Foundation\Application;
class Test extends AbstractModelsCommand
{
protected function setUp(): void
{
parent::setUp();
if (PHP_VERSION_ID < 80000) {
$this->markTestSkipped('This test requires PHP 8.0 or higher');
}
}
public function test(): void
{
$command = $this->app->make(ModelsCommand::class);
$tester = $this->runCommand($command, [
'--write' => true,
]);
$this->assertSame(0, $tester->getStatusCode());
$this->assertStringContainsString('Written new phpDocBlock to', $tester->getDisplay());
$this->assertMatchesMockedSnapshot();
}
}
@@ -0,0 +1,45 @@
<?php
declare(strict_types=1);
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\UnionTypes\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Query\Builder;
/**
* Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\UnionTypes\Models\UnionTypeModel
*
* @property-read string|int|null $foo
* @property-read \Illuminate\Database\Eloquent\Collection|UnionTypeModel[] $withUnionTypeReturn
* @property-read int|null $with_union_type_return_count
* @method static \Illuminate\Database\Eloquent\Builder|UnionTypeModel newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|UnionTypeModel newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|UnionTypeModel query()
* @method static \Illuminate\Database\Eloquent\Builder|UnionTypeModel withNullableUnionTypeParameter(string|int|null $bar)
* @method static \Illuminate\Database\Eloquent\Builder|UnionTypeModel withUnionTypeParameter(string|int $bar)
* @mixin \Eloquent
*/
class UnionTypeModel extends Model
{
public function scopeWithUnionTypeParameter(Builder $query, string|int $bar): Builder
{
return $query->where('foo', $bar);
}
public function scopeWithNullableUnionTypeParameter(Builder $query, null|string|int $bar): Builder
{
return $query->where('foo', $bar);
}
public function withUnionTypeReturn(): HasMany|UnionTypeModel
{
return $this->hasMany(UnionTypeModel::class);
}
public function getFooAttribute(): string|int|null
{
return $this->getAttribute('foo');
}
}