Completed
Pull Request — master (#15)
by Wachter
24:49 queued 08:56
created

Task   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 5
Bugs 1 Features 4
Metric Value
wmc 3
c 5
b 1
f 4
lcom 1
cbo 2
dl 0
loc 38
rs 10
ccs 0
cts 2
cp 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getId() 0 4 1
A getIntervalExpression() 0 4 1
A setInterval() 0 6 1
1
<?php
2
3
namespace Task\TaskBundle\Entity;
4
5
use Cron\CronExpression;
6
use Task\Task as BaseTask;
7
8
class Task extends BaseTask
9
{
10
    /**
11
     * @var int
12
     */
13
    private $id;
14
15
    /**
16
     * @var string
17
     */
18
    private $intervalExpression;
19
20
    /**
21
     * @return int
22
     */
23
    public function getId()
24
    {
25
        return $this->id;
26
    }
27
28
    /**
29
     * @return mixed
30
     */
31
    public function getIntervalExpression()
32
    {
33
        return $this->intervalExpression;
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function setInterval(CronExpression $interval, \DateTime $firstExecution = null, \DateTime $lastExecution = null)
40
    {
41
        parent::setInterval($interval, $firstExecution, $lastExecution);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Task\Task as the method setInterval() does only exist in the following sub-classes of Task\Task: Task\TaskBundle\Entity\Task. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

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

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
42
43
        $this->intervalExpression = $interval->getExpression();
44
    }
45
}
46