Skip to content

Commit c0955af

Browse files
authored
fix: unreachable code on PruneCommand when using --model and --except at same time (#56140)
Fix: #56139
1 parent 2879f17 commit c0955af

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

src/Illuminate/Database/Console/PruneCommand.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -115,18 +115,19 @@ protected function pruneModel(string $model)
115115
*/
116116
protected function models()
117117
{
118-
if (! empty($models = $this->option('model'))) {
119-
return (new Collection($models))->filter(function ($model) {
120-
return class_exists($model);
121-
})->values();
122-
}
123-
118+
$models = $this->option('model');
124119
$except = $this->option('except');
125120

126-
if (! empty($models) && ! empty($except)) {
121+
if ($models && $except) {
127122
throw new InvalidArgumentException('The --models and --except options cannot be combined.');
128123
}
129124

125+
if ($models) {
126+
return (new Collection($models))
127+
->filter(static fn (string $model) => class_exists($model))
128+
->values();
129+
}
130+
130131
return (new Collection(Finder::create()->in($this->getPath())->files()->name('*.php')))
131132
->map(function ($model) {
132133
$namespace = $this->laravel->getNamespace();

tests/Database/PruneCommandTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,17 @@ protected function setUp(): void
3939
$container->alias(DispatcherContract::class, 'events');
4040
}
4141

42+
public function testPrunableModelAndExceptWithEachOther(): void
43+
{
44+
$this->expectException(\InvalidArgumentException::class);
45+
$this->expectExceptionMessage('The --models and --except options cannot be combined.');
46+
47+
$this->artisan([
48+
'--model' => Pruning\Models\PrunableTestModelWithPrunableRecords::class,
49+
'--except' => Pruning\Models\PrunableTestModelWithPrunableRecords::class,
50+
]);
51+
}
52+
4253
public function testPrunableModelWithPrunableRecords()
4354
{
4455
$output = $this->artisan(['--model' => Pruning\Models\PrunableTestModelWithPrunableRecords::class]);

0 commit comments

Comments
 (0)