Passed
Pull Request — master (#8)
by Kevin
15:51
created

CallbackTaskRunner::__invoke()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Zenstruck\ScheduleBundle\Schedule\Task\Runner;
4
5
use Zenstruck\ScheduleBundle\Schedule\Task;
6
use Zenstruck\ScheduleBundle\Schedule\Task\CallbackTask;
7
use Zenstruck\ScheduleBundle\Schedule\Task\Result;
8
use Zenstruck\ScheduleBundle\Schedule\Task\TaskRunner;
9
10
/**
11
 * @author Kevin Bond <[email protected]>
12
 */
13
final class CallbackTaskRunner implements TaskRunner
14
{
15
    /**
16
     * @param CallbackTask|Task $task
17
     */
18
    public function __invoke(Task $task): Result
19
    {
20
        $output = $task->getCallback()();
0 ignored issues
show
Bug introduced by
The method getCallback() does not exist on Zenstruck\ScheduleBundle\Schedule\Task. It seems like you code against a sub-type of Zenstruck\ScheduleBundle\Schedule\Task such as Zenstruck\ScheduleBundle...edule\Task\CallbackTask. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

20
        $output = $task->/** @scrutinizer ignore-call */ getCallback()();
Loading history...
21
22
        return Result::successful($task, self::stringify($output));
23
    }
24
25
    public function supports(Task $task): bool
26
    {
27
        return $task instanceof CallbackTask;
28
    }
29
30
    /**
31
     * @param mixed $value
32
     */
33
    private static function stringify($value): ?string
34
    {
35
        if (null === $value) {
36
            return null;
37
        }
38
39
        if (\is_scalar($value)) {
40
            return $value;
41
        }
42
43
        if (\is_object($value) && \method_exists($value, '__toString')) {
44
            return $value;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $value returns the type object which is incompatible with the type-hinted return null|string.
Loading history...
45
        }
46
47
        if (\is_object($value)) {
48
            return '[object] '.\get_class($value);
49
        }
50
51
        return '('.\gettype($value).')';
52
    }
53
}
54