Skip to content

Commit

Permalink
update(config/model command): add option to force usage of FQN + tests (
Browse files Browse the repository at this point in the history
  • Loading branch information
edvordo committed Sep 1, 2020
1 parent 4a6d127 commit a96add6
Show file tree
Hide file tree
Showing 5 changed files with 267 additions and 1 deletion.
12 changes: 12 additions & 0 deletions config/ide-helper.php
Expand Up @@ -242,4 +242,16 @@
*/
'include_class_docblocks' => false,

/*
|--------------------------------------------------------------------------
| Force FQN usage
|--------------------------------------------------------------------------
|
| Use the fully qualified (class) name in docBlock,
| event if class exists in a given file
| or there is an import (use className) of a given class
|
*/
'force_fqn' => false,

);
3 changes: 2 additions & 1 deletion src/Console/ModelsCommand.php
Expand Up @@ -1067,8 +1067,9 @@ protected function getClassNameInDestinationFile(object $model, string $classNam
$className = trim($className, '\\');
$writingToExternalFile = !$this->write;
$classIsNotInExternalFile = $reflection->getName() !== $className;
$forceFQCN = $this->laravel['config']->get('ide-helper.force_fqn', false);

if ($writingToExternalFile && $classIsNotInExternalFile) {
if (($writingToExternalFile && $classIsNotInExternalFile) || $forceFQCN) {
return '\\' . $className;
}

Expand Down
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

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

use Eloquent;
use Illuminate\Database\Eloquent\{
Builder as EloquentBuilder,
Collection,
SoftDeletes
};
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Query\Builder as QueryBuilder;
use Illuminate\Support\Carbon;

class Post extends Model
{
use SoftDeletes;

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

public function scopeNull($query, string $unusedParam)
{
return $query;
}
}
31 changes: 31 additions & 0 deletions tests/Console/ModelsCommand/GeneratePhpdocWithForcedFqn/Test.php
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

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

use Barryvdh\LaravelIdeHelper\Console\ModelsCommand;
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\AbstractModelsCommand;

class Test extends AbstractModelsCommand
{
protected function getEnvironmentSetUp($app)
{
parent::getEnvironmentSetUp($app);

$app['config']->set('ide-helper.force_fqn', true);
}

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 comments on commit a96add6

Please sign in to comment.