Skip to content

Commit

Permalink
[9.x] Fix schedule:list crash when call() is given class-string (#45306)
Browse files Browse the repository at this point in the history
* [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
  • Loading branch information
odinns committed Dec 14, 2022
1 parent e6ee0f6 commit 1e00add
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/Illuminate/Console/Scheduling/ScheduleListCommand.php
Expand Up @@ -220,8 +220,14 @@ private function getClosureLocation(CallbackEvent $event)
);
}

if (is_string($callback)) {
return $callback;
}

if (is_array($callback)) {
return sprintf('%s::%s', $callback[0]::class, $callback[1]);
$className = is_string($callback[0]) ? $callback[0] : $callback[0]::class;

return sprintf('%s::%s', $className, $callback[1]);
}

return sprintf('%s::__invoke', $callback::class);
Expand Down
19 changes: 19 additions & 0 deletions tests/Integration/Console/Scheduling/ScheduleListCommandTest.php
Expand Up @@ -36,6 +36,8 @@ public function testDisplaySchedule()
$this->schedule->job(FooJob::class)->everyMinute();
$this->schedule->command('inspire')->cron('0 9,17 * * *');
$this->schedule->command('inspire')->cron("0 10\t* * *");
$this->schedule->call(FooCall::class)->everyMinute();
$this->schedule->call([FooCall::class, 'fooFunction'])->everyMinute();

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

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

$this->schedule->call(fn () => '')->everyMinute();
$closureLineNumber = __LINE__ - 1;
Expand All @@ -69,6 +75,8 @@ public function testDisplayScheduleWithSort()
->assertSuccessful()
->expectsOutput(' * * * * * php artisan foobar a='.ProcessUtils::escapeArgument('b').' ... Next Due: 1 minute from now')
->expectsOutput(' * * * * * Illuminate\Tests\Integration\Console\Scheduling\FooJob Next Due: 1 minute from now')
->expectsOutput(' * * * * * Closure at: Illuminate\Tests\Integration\Console\Scheduling\FooCall Next Due: 1 minute from now')
->expectsOutput(' * * * * * Closure at: Illuminate\Tests\Integration\Console\Scheduling\FooCall::fooFunction Next Due: 1 minute from now')
->expectsOutput(' * * * * * Closure at: '.$closureFilePath.':'.$closureLineNumber.' Next Due: 1 minute from now')
->expectsOutput(' 0 9,17 * * * php artisan inspire ......... Next Due: 9 hours from now')
->expectsOutput(' 0 10 * * * php artisan inspire ........ Next Due: 10 hours from now')
Expand Down Expand Up @@ -106,3 +114,14 @@ class FooCommand extends Command
class FooJob
{
}

class FooCall
{
public function __invoke(): void
{
}

public function fooFunction(): void
{
}
}

0 comments on commit 1e00add

Please sign in to comment.