Completed
Push — develop ( 379a64...407c5a )
by Alexandr
10s
created

AbstractCronTimeFormatValidator   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 12
c 0
b 0
f 0
lcom 1
cbo 0
dl 0
loc 80
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A validate() 0 14 3
A validateList() 0 10 3
A validateItem() 0 8 2
A validateRange() 0 16 4
validateTimeEntry() 0 1 ?
1
<?php
2
3
namespace FOA\CronBundle\Validator\Constraints;
4
5
use Symfony\Component\Validator\Constraint;
6
use Symfony\Component\Validator\ConstraintValidator;
7
8
/**
9
 * @author JM Leroux <[email protected]>
10
 */
11
abstract class AbstractCronTimeFormatValidator extends ConstraintValidator
12
{
13
    /**
14
     * @param string|int $value
15
     * @param Constraint $constraint
16
     */
17
    public function validate($value, Constraint $constraint)
18
    {
19
        if (false !== strpos($value, ',')) {
20
            $isValid = $this->validateList(explode(',', $value));
21
        } else {
22
            $isValid = $this->validateItem($value);
23
        }
24
25
        if (!$isValid) {
26
            $this->context->buildViolation($constraint->message)
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Symfony\Component\Valida...ecutionContextInterface as the method buildViolation() does only exist in the following implementations of said interface: Symfony\Component\Valida...ontext\ExecutionContext, Symfony\Component\Valida...\LegacyExecutionContext.

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...
27
                ->setParameter('%string%', $value)
28
                ->addViolation();
29
        }
30
    }
31
32
    /**
33
     * @param array $list
34
     *
35
     * @return bool
36
     */
37
    private function validateList($list)
38
    {
39
        foreach ($list as $item) {
40
            if (!$this->validateItem($item)) {
41
                return false;
42
            }
43
        }
44
45
        return true;
46
    }
47
48
    /**
49
     * @param int|string $item
50
     *
51
     * @return bool
52
     */
53
    private function validateItem($item)
54
    {
55
        if (false !== strpos($item, '-')) {
56
            return $this->validateRange($item);
57
        }
58
59
        return $this->validateTimeEntry($item);
60
    }
61
62
    /**
63
     * @param int|string $range
64
     *
65
     * @return bool
66
     */
67
    private function validateRange($range)
68
    {
69
        $items = explode('-', $range);
70
71
        if (count($items) !== 2) {
72
            return false;
73
        }
74
75
        foreach ($items as $item) {
76
            if (!$this->validateTimeEntry($item)) {
77
                return false;
78
            }
79
        }
80
81
        return true;
82
    }
83
84
    /**
85
     * @param int|string $item
86
     *
87
     * @return bool
88
     */
89
    abstract protected function validateTimeEntry($item);
90
}
91