Passed
Push — master ( 3ce531...7a6df5 )
by Kevin
02:21
created

CallbackTask::stringify()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 19
ccs 10
cts 10
cp 1
rs 9.2222
cc 6
nc 5
nop 1
crap 6
1
<?php
2
3
namespace Zenstruck\ScheduleBundle\Schedule\Task;
4
5
use Zenstruck\ScheduleBundle\Schedule\Extension\CallbackExtension;
6
use Zenstruck\ScheduleBundle\Schedule\Task;
7
8
/**
9
 * @author Kevin Bond <[email protected]>
10
 */
11
final class CallbackTask extends Task implements SelfRunningTask
12
{
13
    private $callback;
14
15
    /**
16
     * @param callable $callback Return value is considered "output"
17
     */
18 14
    public function __construct(callable $callback)
19
    {
20 14
        parent::__construct('(callable) '.CallbackExtension::createDescriptionFromCallback($callback));
21
22 14
        $this->callback = $callback;
23 14
    }
24
25 8
    public function __invoke(): Result
26
    {
27 8
        $output = ($this->callback)();
28
29 8
        return Result::successful($this, self::stringify($output));
30
    }
31
32 1
    public function getContext(): array
33
    {
34 1
        return ['Callable' => CallbackExtension::createDescriptionFromCallback($this->callback)];
35
    }
36
37
    /**
38
     * @param mixed $value
39
     */
40 8
    private static function stringify($value): ?string
41
    {
42 8
        if (null === $value) {
43 2
            return null;
44
        }
45
46 6
        if (\is_scalar($value)) {
47 3
            return $value;
48
        }
49
50 3
        if (\is_object($value) && \method_exists($value, '__toString')) {
51 1
            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...
52
        }
53
54 2
        if (\is_object($value)) {
55 1
            return '[object] '.\get_class($value);
56
        }
57
58 1
        return '('.\gettype($value).')';
59
    }
60
}
61