Completed
Push — master ( 4e7b1e...2ea0a3 )
by Paweł
12:37 queued 03:15
created

RandomReviewsFixture::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 1
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\CoreBundle\Fixture;
13
14
use Sylius\Bundle\FixturesBundle\Fixture\AbstractFixture;
15
use Sylius\Component\Core\Repository\ProductRepositoryInterface;
16
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
17
use Symfony\Component\OptionsResolver\OptionsResolver;
18
19
/**
20
 * @author Mateusz Zalewski <[email protected]>
21
 */
22
final class RandomReviewsFixture extends AbstractFixture
23
{
24
    /**
25
     * @var AbstractResourceFixture
26
     */
27
    private $productReviewFixture;
28
29
    /**
30
     * @var \Faker\Generator
31
     */
32
    private $faker;
33
34
    /**
35
     * @var OptionsResolver
36
     */
37
    private $optionsResolver;
38
39
    /**
40
     * @param AbstractResourceFixture $productReviewFixture
41
     */
42
    public function __construct(AbstractResourceFixture $productReviewFixture)
43
    {
44
        $this->productReviewFixture = $productReviewFixture;
45
46
        $this->faker = \Faker\Factory::create();
47
        $this->optionsResolver =
48
            (new OptionsResolver())
49
                ->setRequired('amount')
50
                ->setAllowedTypes('amount', 'int')
51
        ;
52
    }
53
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function load(array $options)
59
    {
60
        $this->productReviewFixture->load(['random' => $options['amount']]);
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function getName()
67
    {
68
        return 'random_product_review';
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    protected function configureOptionsNode(ArrayNodeDefinition $optionsNode)
75
    {
76
        $optionsNode
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Symfony\Component\Config...\Builder\NodeDefinition as the method min() does only exist in the following sub-classes of Symfony\Component\Config...\Builder\NodeDefinition: Symfony\Component\Config...der\FloatNodeDefinition, Symfony\Component\Config...r\IntegerNodeDefinition, Symfony\Component\Config...r\NumericNodeDefinition. 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...
77
            ->children()
78
            ->integerNode('amount')->isRequired()->min(0)->end()
79
        ;
80
    }
81
}
82