These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | /* |
||
4 | * This file is part of the Sylius package. |
||
5 | * |
||
6 | * (c) Paweł Jędrzejewski |
||
7 | * |
||
8 | * For the full copyright and license information, please view the LICENSE |
||
9 | * file that was distributed with this source code. |
||
10 | */ |
||
11 | |||
12 | namespace Sylius\Bundle\PromotionBundle\Validator; |
||
13 | |||
14 | use Sylius\Component\Promotion\Model\PromotionInterface; |
||
15 | use Symfony\Component\Validator\Constraint; |
||
16 | use Symfony\Component\Validator\ConstraintValidator; |
||
17 | use Webmozart\Assert\Assert; |
||
18 | |||
19 | /** |
||
20 | * @author Łukasz Chruściel <[email protected]> |
||
21 | */ |
||
22 | class PromotionDateRangeValidator extends ConstraintValidator |
||
23 | { |
||
24 | /** |
||
25 | * {@inheritdoc} |
||
26 | * |
||
27 | * @param mixed $value |
||
28 | * @param Constraint $constraint |
||
29 | */ |
||
30 | public function validate($value, Constraint $constraint) |
||
31 | { |
||
32 | if (null === $value) { |
||
33 | return; |
||
34 | } |
||
35 | |||
36 | Assert::isInstanceOf( |
||
37 | $value, |
||
38 | PromotionInterface::class |
||
39 | ); |
||
40 | |||
41 | if (null === $value->getStartsAt() || null === $value->getEndsAt()) { |
||
42 | return; |
||
43 | } |
||
44 | |||
45 | if ($value->getStartsAt()->getTimestamp() > $value->getEndsAt()->getTimestamp()) { |
||
46 | $this->context->buildViolation($constraint->message) |
||
0 ignored issues
–
show
|
|||
47 | ->atPath('endsAt') |
||
48 | ->addViolation(); |
||
49 | } |
||
50 | } |
||
51 | } |
||
52 |
Let’s take a look at an example:
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
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: