Executor::execute()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
1
<?php
2
3
namespace T4web\Cron\Executor;
4
5
use Zend\EventManager\EventManager;
6
use Cron\Job\JobInterface;
7
use Cron\Report\ReportInterface;
8
use Cron\Report\CronReport;
9
use Cron\Executor\ExecutorInterface;
10
use T4web\Cron\JobEndedEvent;
11
use T4web\Cron\Job\ShellJob;
12
13
class Executor implements ExecutorInterface
14
{
15
    /**
16
     * @var JobInterface[]
17
     */
18
    private $jobs;
19
20
    /**
21
     * @var EventManager
22
     */
23
    private $eventManager;
24
25
    /**
26
     * @var array
27
     */
28
    private $processedJobs = [];
29
30
    /**
31
     * @param JobInterface[] $jobs
32
     *
33
     * @return ReportInterface
34
     */
35
    public function execute(array $jobs)
36
    {
37
        $report = new CronReport();
38
39
        $this->jobs = $jobs;
40
        $this->startProcesses($report);
41
42
        return $report;
43
    }
44
45
    /**
46
     * @param CronReport $report
47
     */
48
    protected function startProcesses(CronReport $report)
49
    {
50
        foreach ($this->jobs as $job) {
51
            $report->addJobReport($job->getReport());
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Cron\Job\JobInterface as the method getReport() does only exist in the following implementations of said interface: T4web\Cron\Job\ShellJob.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
52
            // :) brilliantly
53
            $job->run($job->getReport());
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Cron\Job\JobInterface as the method getReport() does only exist in the following implementations of said interface: T4web\Cron\Job\ShellJob.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
54
        }
55
    }
56
57
    private function getEventManager()
58
    {
59
        if (!$this->eventManager) {
60
            $this->eventManager = new EventManager();
61
            $this->eventManager->setIdentifiers(get_class($this));
62
        }
63
64
        return $this->eventManager;
65
    }
66
67
    /**
68
     * @return bool
69
     */
70
    public function isRunning()
71
    {
72
        $isRunning = false;
73
74
        foreach ($this->jobs as $id => $job) {
75
            if ($job->isRunning()) {
76
                $isRunning = true;
77
            } else {
78
79
                if (isset($this->processedJobs[$id])) {
80
                    continue;
81
                }
82
83
                $event = new JobEndedEvent();
84
                $event->setTarget($this);
85
                $event->setJob($job);
0 ignored issues
show
Compatibility introduced by
$job of type object<Cron\Job\JobInterface> is not a sub-type of object<T4web\Cron\Job\ShellJob>. It seems like you assume a concrete implementation of the interface Cron\Job\JobInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
86
                $event->setReport($job->getReport());
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Cron\Job\JobInterface as the method getReport() does only exist in the following implementations of said interface: T4web\Cron\Job\ShellJob.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
87
                $this->getEventManager()->trigger($event);
88
89
                $this->processedJobs[$id] = true;
90
            }
91
        }
92
93
        return $isRunning;
94
    }
95
96
    /**
97
     * @return ShellJob[]
98
     */
99
    public function getRunningJobs()
100
    {
101
        $jobs = [];
102
        foreach ($this->jobs as $job) {
103
            if ($job->isRunning()) {
104
                $jobs[] = $job;
105
            }
106
        }
107
108
        return $jobs;
109
    }
110
}
111