Skip to content

Commit 5cd540e

Browse files
authored
fix: PruneCommand finding its usage within other traits (#42350)
This addresses an edge case where using the Prunable trait within another trait (due to several reasons like modifying behaviour, removing duplication, etc..) it was breaking the PruneCommand from behaving properly ince the trait was being detected as a class. Signed-off-by: Bruno Gaspar <[email protected]>
1 parent 0a85d0c commit 5cd540e

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/Illuminate/Database/Console/PruneCommand.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,9 @@ public function handle(Dispatcher $events)
8787
protected function models()
8888
{
8989
if (! empty($models = $this->option('model'))) {
90-
return collect($models);
90+
return collect($models)->filter(function ($model) {
91+
return class_exists($model);
92+
})->values();
9193
}
9294

9395
$except = $this->option('except');
@@ -111,6 +113,8 @@ protected function models()
111113
});
112114
})->filter(function ($model) {
113115
return $this->isPrunable($model);
116+
})->filter(function ($model) {
117+
return class_exists($model);
114118
})->values();
115119
}
116120

tests/Database/PruneCommandTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,16 @@ public function testNonPrunableTest()
8989
$this->assertEquals(<<<'EOF'
9090
No prunable [Illuminate\Tests\Database\NonPrunableTestModel] records found.
9191

92+
EOF, str_replace("\r", '', $output->fetch()));
93+
}
94+
95+
public function testNonPrunableTestWithATrait()
96+
{
97+
$output = $this->artisan(['--model' => NonPrunableTrait::class]);
98+
99+
$this->assertEquals(<<<'EOF'
100+
No prunable models found.
101+
92102
EOF, str_replace("\r", '', $output->fetch()));
93103
}
94104

@@ -227,3 +237,8 @@ class NonPrunableTestModel extends Model
227237
{
228238
// ..
229239
}
240+
241+
trait NonPrunableTrait
242+
{
243+
use Prunable;
244+
}

0 commit comments

Comments
 (0)