fix(scheduler): pre-filter files for #[Scheduled] before class_exists
Some checks failed
CI / PHP 8.3 (push) Failing after 2m3s
CI / PHP 8.4 (push) Failing after 2m21s

class_exists() can trigger uncatchable E_COMPILE_ERROR when autoloading
classes with method signature mismatches (e.g. Activity model vs updated
Spatie parent). Now checks file contents for '#[Scheduled' string before
attempting to load — avoids autoloading hundreds of unrelated classes.

Also fixes Activity::getChangesAttribute() return type to match the
updated Spatie parent (Collection instead of array).

Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
Snider 2026-03-12 15:58:15 +00:00
parent d02f4361e3
commit 303186323a
2 changed files with 13 additions and 7 deletions

View file

@ -60,7 +60,13 @@ class ScheduledActionScanner
continue;
}
$class = $this->classFromFile($file->getPathname());
$contents = file_get_contents($file->getPathname());
if ($contents === false || ! str_contains($contents, '#[Scheduled')) {
continue;
}
$class = $this->classFromFile($file->getPathname(), $contents);
if ($class === null) {
continue;
@ -110,9 +116,9 @@ class ScheduledActionScanner
*
* Reads the file's namespace declaration and class name.
*/
private function classFromFile(string $file): ?string
private function classFromFile(string $file, ?string $contents = null): ?string
{
$contents = file_get_contents($file);
$contents ??= file_get_contents($file);
if ($contents === false) {
return null;

View file

@ -81,9 +81,9 @@ class Activity extends SpatieActivity
/**
* Get the changed attributes.
*
* @return array<string, array{old: mixed, new: mixed}>
* @return \Illuminate\Support\Collection<string, array{old: mixed, new: mixed}>
*/
public function getChangesAttribute(): array
public function getChangesAttribute(): \Illuminate\Support\Collection
{
$old = $this->old_values;
$new = $this->new_values;
@ -99,7 +99,7 @@ class Activity extends SpatieActivity
}
}
return $changes;
return collect($changes);
}
/**
@ -117,7 +117,7 @@ class Activity extends SpatieActivity
{
$changes = $this->changes;
if (empty($changes)) {
if ($changes->isEmpty()) {
return 'No changes recorded';
}