Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return the class name without the namespace if in the same namespace #1259

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/Console/ModelsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -1374,6 +1374,14 @@ protected function getClassNameInDestinationFile(object $model, string $classNam
return '\\' . $className;
}

$namespace = $reflection->getNamespaceName();
$classIsInGlobalNamespace = strcasecmp($namespace, '') === 0;
$classIsInTheSameNamespace = strpos($className, $namespace) === 0;
if (!$classIsInGlobalNamespace && $classIsNotInExternalFile && $classIsInTheSameNamespace) {
$path = explode('\\', $className);
return array_pop($path);
}

$usedClassNames = $this->getUsedClassNames($reflection);
return $usedClassNames[$className] ?? ('\\' . $className);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GeneratePhpdocWithForcedFqn\Models;

use Illuminate\Database\Eloquent\Model;

class AnotherModelSameNamespace extends Model
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ public function posts(): HasMany
return $this->hasMany(Post::class);
}

public function anotherModels(): HasMany
{
return $this->hasMany(AnotherModelSameNamespace::class);
}

public function scopeNull($query, string $unusedParam)
{
return $query;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,25 @@

namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GeneratePhpdocWithForcedFqn\Models;

use Illuminate\Database\Eloquent\Model;

/**
* Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GeneratePhpdocWithForcedFqn\Models\AnotherModelSameNamespace
*
* @method static \Illuminate\Database\Eloquent\Builder|\Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GeneratePhpdocWithForcedFqn\Models\AnotherModelSameNamespace newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|\Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GeneratePhpdocWithForcedFqn\Models\AnotherModelSameNamespace newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|\Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GeneratePhpdocWithForcedFqn\Models\AnotherModelSameNamespace query()
* @mixin \Eloquent
*/
class AnotherModelSameNamespace extends Model
{
}
<?php

declare(strict_types=1);

namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GeneratePhpdocWithForcedFqn\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
Expand Down Expand Up @@ -84,6 +103,8 @@
* @property string $macaddress_not_nullable
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @property-read \Illuminate\Database\Eloquent\Collection|\Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GeneratePhpdocWithForcedFqn\Models\AnotherModelSameNamespace[] $anotherModels
* @property-read int|null $another_models_count
* @property-read \Illuminate\Database\Eloquent\Collection|\Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GeneratePhpdocWithForcedFqn\Models\Post[] $posts
* @property-read int|null $posts_count
* @method static \Illuminate\Database\Eloquent\Builder|\Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GeneratePhpdocWithForcedFqn\Models\Post newModelQuery()
Expand Down Expand Up @@ -177,6 +198,11 @@ public function posts(): HasMany
return $this->hasMany(Post::class);
}

public function anotherModels(): HasMany
{
return $this->hasMany(AnotherModelSameNamespace::class);
}

public function scopeNull($query, string $unusedParam)
{
return $query;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GeneratePhpdocWithFqn\Models;

use Illuminate\Database\Eloquent\Model;

class AnotherModelSameNamespace extends Model
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ public function posts(): HasMany
return $this->hasMany(Post::class);
}

public function anotherModels(): HasMany
{
return $this->hasMany(AnotherModelSameNamespace::class);
}

public function scopeNull($query, string $unusedParam)
{
return $query;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,25 @@

namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GeneratePhpdocWithFqn\Models;

use Illuminate\Database\Eloquent\Model;

/**
* Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GeneratePhpdocWithFqn\Models\AnotherModelSameNamespace
*
* @method static \Illuminate\Database\Eloquent\Builder|AnotherModelSameNamespace newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|AnotherModelSameNamespace newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|AnotherModelSameNamespace query()
* @mixin \Eloquent
*/
class AnotherModelSameNamespace extends Model
{
}
<?php

declare(strict_types=1);

namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GeneratePhpdocWithFqn\Models;

use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GeneratePhpdocWithFqn\Casts\CastType;
use Eloquent;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
Expand Down Expand Up @@ -90,6 +109,8 @@
* @property string $macaddress_not_nullable
* @property Carbon|null $created_at
* @property Carbon|null $updated_at
* @property-read Collection|AnotherModelSameNamespace[] $anotherModels
* @property-read int|null $another_models_count
* @property-read Collection|Post[] $posts
* @property-read int|null $posts_count
* @method static EloquentBuilder|Post newModelQuery()
Expand Down Expand Up @@ -199,6 +220,11 @@ public function posts(): HasMany
return $this->hasMany(Post::class);
}

public function anotherModels(): HasMany
{
return $this->hasMany(AnotherModelSameNamespace::class);
}

public function scopeNull($query, string $unusedParam)
{
return $query;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GeneratePhpdocWithFqnInExternalFile\Models;

use Illuminate\Database\Eloquent\Model;

class AnotherModelSameNamespace extends Model
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GeneratePhpdocWithFqnInExternalFile\Builders\EMaterialQueryBuilder;
use Illuminate\Database\Eloquent\Model;

/**
* @package Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GeneratePhpdocWithFqnInExternalFile\Models
* @property AnotherModelSameNamespace $someProp
*/
class Post extends Model
{
public function newEloquentBuilder($query): EMaterialQueryBuilder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,23 @@
*/


namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GeneratePhpdocWithFqnInExternalFile\Models{
/**
* Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GeneratePhpdocWithFqnInExternalFile\Models\AnotherModelSameNamespace
*
* @method static \Illuminate\Database\Eloquent\Builder|AnotherModelSameNamespace newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|AnotherModelSameNamespace newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|AnotherModelSameNamespace query()
*/
class AnotherModelSameNamespace extends \Eloquent {}
}

namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GeneratePhpdocWithFqnInExternalFile\Models{
/**
* Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GeneratePhpdocWithFqnInExternalFile\Models\Post
*
* @package Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GeneratePhpdocWithFqnInExternalFile\Models
* @property AnotherModelSameNamespace $someProp
* @property integer $id
* @property string|null $char_nullable
* @property string $char_not_nullable
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Relations\Models;

use Illuminate\Database\Eloquent\Model;

class AnotherModelSameNamespace extends Model
{
}
5 changes: 5 additions & 0 deletions tests/Console/ModelsCommand/Relations/Models/Simple.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ public function relationMorphedByMany(): MorphToMany

// Custom relations

public function relationBelongsToInTheSameNamespace(): BelongsTo
{
return $this->belongsTo(AnotherModelSameNamespace::class);
}

public function relationBelongsToInAnotherNamespace(): BelongsTo
{
return $this->belongsTo(AnotherModel::class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,25 @@

namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Relations\Models;

use Illuminate\Database\Eloquent\Model;

/**
* Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Relations\Models\AnotherModelSameNamespace
*
* @method static \Illuminate\Database\Eloquent\Builder|AnotherModelSameNamespace newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|AnotherModelSameNamespace newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|AnotherModelSameNamespace query()
* @mixin \Eloquent
*/
class AnotherModelSameNamespace extends Model
{
}
<?php

declare(strict_types=1);

namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Relations\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

Expand Down Expand Up @@ -75,6 +94,7 @@ public function nullableColumnWithNoForeignKeyConstraint(): BelongsTo
* @property integer $id
* @property-read Simple|null $relationBelongsTo
* @property-read AnotherModel|null $relationBelongsToInAnotherNamespace
* @property-read AnotherModelSameNamespace|null $relationBelongsToInTheSameNamespace
* @property-read \Illuminate\Database\Eloquent\Collection|Simple[] $relationBelongsToMany
* @property-read int|null $relation_belongs_to_many_count
* @property-read \Illuminate\Database\Eloquent\Collection|Simple[] $relationBelongsToManyWithSub
Expand Down Expand Up @@ -166,6 +186,11 @@ public function relationMorphedByMany(): MorphToMany

// Custom relations

public function relationBelongsToInTheSameNamespace(): BelongsTo
{
return $this->belongsTo(AnotherModelSameNamespace::class);
}

public function relationBelongsToInAnotherNamespace(): BelongsTo
{
return $this->belongsTo(AnotherModel::class);
Expand Down