Test Failed
Pull Request — master (#7)
by Jim
03:47
created

Processor::process()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 36
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 36
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 21
nc 4
nop 1
1
<?php
2
3
namespace Jarobe\TaskRunnerBundle\Processor;
4
5
use Jarobe\TaskRunnerBundle\Exception\TaskException;
6
use Jarobe\TaskRunnerBundle\Model\TaskResult;
7
use Jarobe\TaskRunnerBundle\Driver\Factory\DriverFactory;
8
use Jarobe\TaskRunnerBundle\TaskType\TaskTypeInterface;
9
10
class Processor implements ProcessorInterface
11
{
12
    /**
13
     * @var DriverFactory
14
     */
15
    private $driverFactory;
16
17
    public function __construct(DriverFactory $driverFactory)
18
    {
19
        $this->driverFactory = $driverFactory;
20
    }
21
22
    /**
23
     * @param TaskTypeInterface $task
24
     * @return TaskResult
25
     */
26
    public function process(TaskTypeInterface $task)
27
    {
28
        $taskResult = new TaskResult();
29
30
        try {
31
            $driver = $this->driverFactory->getDriverForTask($task);
32
        } catch (TaskException $e) {
33
            $exceptionMessage = sprintf("TaskException: %s", $e->getMessage());
34
            $taskResult->setSuccess(false)
35
                ->setErrors([$exceptionMessage])
36
            ;
37
            return $taskResult;
38
        }
39
40
41
        //See if there's any issues with the tasks before running.
42
        $validationErrors = $driver->canRun($task);
43
        if (count($validationErrors) > 0) {
44
            $taskResult->setSuccess(false)
45
                ->setErrors($validationErrors)
0 ignored issues
show
Bug introduced by
It seems like $validationErrors defined by $driver->canRun($task) on line 42 can also be of type null; however, Jarobe\TaskRunnerBundle\...TaskResult::setErrors() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
46
            ;
47
            return $taskResult;
48
        }
49
50
        //Run the task, and then check for errors
51
        $errors = $driver->run($task);
52
        if (count($errors) > 0) {
53
            $taskResult->setSuccess(false)
54
                ->setErrors($errors)
0 ignored issues
show
Bug introduced by
It seems like $errors defined by $driver->run($task) on line 51 can also be of type null; however, Jarobe\TaskRunnerBundle\...TaskResult::setErrors() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
55
            ;
56
            return $taskResult;
57
        }
58
59
        $taskResult->setSuccess(true);
60
        return $taskResult;
61
    }
62
}
63