Skip to content

Commit 1e00add

Browse files
authored
[9.x] Fix schedule:list crash when call() is given class-string (#45306)
* [9.x] Fix schedule:list crash when call() is given class-string When creating schedule of the form: ```php $this->schedule->call(FooCall::class); $this->schedule->call([FooCall::class, 'fooFunction']); ``` Then the `ScheduleListCommand::getClosureLocation()` method would crash with: ``` TypeError Cannot use "::class" on value of type string ``` This PR fixes it by adding checks for string `$callback` and returning that string instead of adding `::class` to it. Tests for both single string and array callable syntax included. * Streamlined the array callable fix
1 parent e6ee0f6 commit 1e00add

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/Illuminate/Console/Scheduling/ScheduleListCommand.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,14 @@ private function getClosureLocation(CallbackEvent $event)
220220
);
221221
}
222222

223+
if (is_string($callback)) {
224+
return $callback;
225+
}
226+
223227
if (is_array($callback)) {
224-
return sprintf('%s::%s', $callback[0]::class, $callback[1]);
228+
$className = is_string($callback[0]) ? $callback[0] : $callback[0]::class;
229+
230+
return sprintf('%s::%s', $className, $callback[1]);
225231
}
226232

227233
return sprintf('%s::__invoke', $callback::class);

tests/Integration/Console/Scheduling/ScheduleListCommandTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ public function testDisplaySchedule()
3636
$this->schedule->job(FooJob::class)->everyMinute();
3737
$this->schedule->command('inspire')->cron('0 9,17 * * *');
3838
$this->schedule->command('inspire')->cron("0 10\t* * *");
39+
$this->schedule->call(FooCall::class)->everyMinute();
40+
$this->schedule->call([FooCall::class, 'fooFunction'])->everyMinute();
3941

4042
$this->schedule->call(fn () => '')->everyMinute();
4143
$closureLineNumber = __LINE__ - 1;
@@ -49,6 +51,8 @@ public function testDisplaySchedule()
4951
->expectsOutput(' * * * * * Illuminate\Tests\Integration\Console\Scheduling\FooJob Next Due: 1 minute from now')
5052
->expectsOutput(' 0 9,17 * * * php artisan inspire ......... Next Due: 9 hours from now')
5153
->expectsOutput(' 0 10 * * * php artisan inspire ........ Next Due: 10 hours from now')
54+
->expectsOutput(' * * * * * Closure at: Illuminate\Tests\Integration\Console\Scheduling\FooCall Next Due: 1 minute from now')
55+
->expectsOutput(' * * * * * Closure at: Illuminate\Tests\Integration\Console\Scheduling\FooCall::fooFunction Next Due: 1 minute from now')
5256
->expectsOutput(' * * * * * Closure at: '.$closureFilePath.':'.$closureLineNumber.' Next Due: 1 minute from now');
5357
}
5458

@@ -60,6 +64,8 @@ public function testDisplayScheduleWithSort()
6064
$this->schedule->job(FooJob::class)->everyMinute();
6165
$this->schedule->command('inspire')->cron('0 9,17 * * *');
6266
$this->schedule->command('inspire')->cron("0 10\t* * *");
67+
$this->schedule->call(FooCall::class)->everyMinute();
68+
$this->schedule->call([FooCall::class, 'fooFunction'])->everyMinute();
6369

6470
$this->schedule->call(fn () => '')->everyMinute();
6571
$closureLineNumber = __LINE__ - 1;
@@ -69,6 +75,8 @@ public function testDisplayScheduleWithSort()
6975
->assertSuccessful()
7076
->expectsOutput(' * * * * * php artisan foobar a='.ProcessUtils::escapeArgument('b').' ... Next Due: 1 minute from now')
7177
->expectsOutput(' * * * * * Illuminate\Tests\Integration\Console\Scheduling\FooJob Next Due: 1 minute from now')
78+
->expectsOutput(' * * * * * Closure at: Illuminate\Tests\Integration\Console\Scheduling\FooCall Next Due: 1 minute from now')
79+
->expectsOutput(' * * * * * Closure at: Illuminate\Tests\Integration\Console\Scheduling\FooCall::fooFunction Next Due: 1 minute from now')
7280
->expectsOutput(' * * * * * Closure at: '.$closureFilePath.':'.$closureLineNumber.' Next Due: 1 minute from now')
7381
->expectsOutput(' 0 9,17 * * * php artisan inspire ......... Next Due: 9 hours from now')
7482
->expectsOutput(' 0 10 * * * php artisan inspire ........ Next Due: 10 hours from now')
@@ -106,3 +114,14 @@ class FooCommand extends Command
106114
class FooJob
107115
{
108116
}
117+
118+
class FooCall
119+
{
120+
public function __invoke(): void
121+
{
122+
}
123+
124+
public function fooFunction(): void
125+
{
126+
}
127+
}

0 commit comments

Comments
 (0)