Skip to content

Commit 5db6029

Browse files
authored
Give feedback when a relation can't be resolved (#1052)
Improves #1017 slightly
1 parent 2a3c7fb commit 5db6029

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

src/Console/ModelsCommand.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
use Symfony\Component\Console\Input\InputArgument;
3939
use Symfony\Component\Console\Input\InputOption;
4040
use Symfony\Component\Console\Output\OutputInterface;
41+
use Throwable;
4142

4243
/**
4344
* A command to generate autocomplete information for your IDE
@@ -277,7 +278,7 @@ protected function generateDocs($loadModels, $ignore = '')
277278
$output .= $this->createPhpDocs($name);
278279
$ignore[] = $name;
279280
$this->nullableColumns = [];
280-
} catch (\Throwable $e) {
281+
} catch (Throwable $e) {
281282
$this->error('Exception: ' . $e->getMessage() .
282283
"\nCould not analyze class $name.\n\nTrace:\n" .
283284
$e->getTraceAsString());
@@ -601,7 +602,9 @@ protected function getPropertiesFromMethods($model)
601602
$relationObj = Relation::noConstraints(function () use ($model, $method) {
602603
try {
603604
return $model->$method();
604-
} catch (\Throwable $e) {
605+
} catch (Throwable $e) {
606+
$this->warn(sprintf('Error resolving relation model of %s:%s() : %s', get_class($model), $method, $e->getMessage()));
607+
605608
return null;
606609
}
607610
});

tests/Console/ModelsCommand/DynamicRelations/Test.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,15 @@ public function test(): void
1717
'--write' => true,
1818
]);
1919

20+
$errors = <<<TXT
21+
Error resolving relation model of Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\DynamicRelations\Models\Dynamic:dynamicBelongsTo() : Trying to get property 'created_at' of non-object
22+
Error resolving relation model of Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\DynamicRelations\Models\Dynamic:dynamicHasMany() : Trying to get property 'created_at' of non-object
23+
Error resolving relation model of Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\DynamicRelations\Models\Dynamic:dynamicHasOne() : Trying to get property 'created_at' of non-object
24+
TXT;
25+
2026
$this->assertSame(0, $tester->getStatusCode());
2127
$this->assertStringContainsString('Written new phpDocBlock to', $tester->getDisplay());
28+
$this->assertStringContainsString($errors, $tester->getDisplay());
2229
$this->assertMatchesMockedSnapshot();
2330
}
2431
}

0 commit comments

Comments
 (0)