Completed
Push — snake-case-tests ( 55729f )
by Kamil
87:29 queued 51:35
created

TaxonomyContext::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 19
rs 9.4285
cc 1
eloc 17
nc 1
nop 8

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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\Behat\Context\Setup;
13
14
use Behat\Behat\Context\Context;
15
use Behat\Mink\Element\NodeElement;
16
use Doctrine\Common\Persistence\ObjectManager;
17
use Sylius\Component\Core\Formatter\StringInflector;
18
use Sylius\Component\Core\Model\ImageInterface;
19
use Sylius\Component\Core\Model\TaxonInterface;
20
use Sylius\Component\Core\Uploader\ImageUploaderInterface;
21
use Sylius\Component\Resource\Factory\FactoryInterface;
22
use Sylius\Component\Resource\Model\TranslationInterface;
23
use Sylius\Component\Resource\Repository\RepositoryInterface;
24
use Sylius\Component\Taxonomy\Generator\TaxonSlugGeneratorInterface;
25
use Sylius\Component\Taxonomy\Model\TaxonTranslationInterface;
26
use Symfony\Component\HttpFoundation\File\UploadedFile;
27
28
/**
29
 * @author Mateusz Zalewski <[email protected]>
30
 */
31
final class TaxonomyContext implements Context
32
{
33
    /**
34
     * @var RepositoryInterface
35
     */
36
    private $taxonRepository;
37
38
    /**
39
     * @var FactoryInterface
40
     */
41
    private $taxonFactory;
42
43
    /**
44
     * @var FactoryInterface
45
     */
46
    private $taxonTranslationFactory;
47
48
    /**
49
     * @var FactoryInterface
50
     */
51
    private $taxonImageFactory;
52
53
    /**
54
     * @var ObjectManager
55
     */
56
    private $objectManager;
57
58
    /**
59
     * @var ImageUploaderInterface
60
     */
61
    private $imageUploader;
62
63
    /**
64
     * @var TaxonSlugGeneratorInterface
65
     */
66
    private $taxonSlugGenerator;
67
68
    /**
69
     * @var array
70
     */
71
    private $minkParameters;
72
73
    /**
74
     * @param RepositoryInterface $taxonRepository
75
     * @param FactoryInterface $taxonFactory
76
     * @param FactoryInterface $taxonTranslationFactory
77
     * @param FactoryInterface $taxonImageFactory
78
     * @param ObjectManager $objectManager
79
     * @param ImageUploaderInterface $imageUploader
80
     * @param TaxonSlugGeneratorInterface $taxonSlugGenerator
81
     * @param array $minkParameters
82
     */
83
    public function __construct(
84
        RepositoryInterface $taxonRepository,
85
        FactoryInterface $taxonFactory,
86
        FactoryInterface $taxonTranslationFactory,
87
        FactoryInterface $taxonImageFactory,
88
        ObjectManager $objectManager,
89
        ImageUploaderInterface $imageUploader,
90
        TaxonSlugGeneratorInterface $taxonSlugGenerator,
91
        array $minkParameters
92
    ) {
93
        $this->taxonRepository = $taxonRepository;
94
        $this->taxonFactory = $taxonFactory;
95
        $this->taxonTranslationFactory = $taxonTranslationFactory;
96
        $this->taxonImageFactory = $taxonImageFactory;
97
        $this->objectManager = $objectManager;
98
        $this->imageUploader = $imageUploader;
99
        $this->taxonSlugGenerator = $taxonSlugGenerator;
100
        $this->minkParameters = $minkParameters;
101
    }
102
103
    /**
104
     * @Given the store has :firstTaxonName taxonomy
105
     * @Given the store classifies its products as :firstTaxonName
106
     * @Given the store classifies its products as :firstTaxonName and :secondTaxonName
107
     * @Given the store classifies its products as :firstTaxonName, :secondTaxonName and :thirdTaxonName
108
     * @Given the store classifies its products as :firstTaxonName, :secondTaxonName, :thirdTaxonName and :fourthTaxonName
109
     */
110
    public function storeClassifiesItsProductsAs(...$taxonsNames)
111
    {
112
        foreach ($taxonsNames as $taxonName) {
113
            $this->taxonRepository->add($this->createTaxon($taxonName));
114
        }
115
    }
116
117
    /**
118
     * @Given /^the store has taxonomy named "([^"]+)" in ("[^"]+" locale) and "([^"]+)" in ("[^"]+" locale)$/
119
     */
120
    public function theStoreHasTaxonomyNamedInAndIn($firstName, $firstLocale, $secondName, $secondLocale)
121
    {
122
        $translationMap = [
123
            $firstLocale => $firstName,
124
            $secondLocale => $secondName,
125
        ];
126
127
        $this->taxonRepository->add($this->createTaxonInManyLanguages($translationMap));
128
    }
129
130
    /**
131
     * @Given /^the ("[^"]+" taxon)(?:| also) has an image "([^"]+)" with "([^"]+)" type$/
132
     */
133
    public function theTaxonHasAnImageWithType(TaxonInterface $taxon, $imagePath, $imageType)
134
    {
135
        $filesPath = $this->getParameter('files_path');
136
137
        /** @var ImageInterface $taxonImage */
138
        $taxonImage = $this->taxonImageFactory->createNew();
139
        $taxonImage->setFile(new UploadedFile($filesPath.$imagePath, basename($imagePath)));
140
        $taxonImage->setType($imageType);
141
        $this->imageUploader->upload($taxonImage);
142
143
        $taxon->addImage($taxonImage);
144
145
        $this->objectManager->flush($taxon);
146
    }
147
148
    /**
149
     * @Given /^the ("[^"]+" taxon) has children taxon "([^"]+)" and "([^"]+)"$/
150
     */
151
    public function theTaxonHasChildrenTaxonAnd(TaxonInterface $taxon, $firstTaxonName, $secondTaxonName)
152
    {
153
        $taxon->addChild($this->createTaxon($firstTaxonName));
154
        $taxon->addChild($this->createTaxon($secondTaxonName));
155
156
        $this->objectManager->flush($taxon);
157
    }
158
159
    /**
160
     * @param string $name
161
     *
162
     * @return TaxonInterface
163
     */
164
    private function createTaxon($name)
165
    {
166
        /** @var TaxonInterface $taxon */
167
        $taxon = $this->taxonFactory->createNew();
168
        $taxon->setName($name);
169
        $taxon->setCode(StringInflector::nameToCode($name));
170
        $taxon->setSlug($this->taxonSlugGenerator->generate($name));
171
172
        return $taxon;
173
    }
174
175
    /**
176
     * @param array $names
177
     *
178
     * @return TaxonInterface
179
     */
180
    private function createTaxonInManyLanguages(array $names)
181
    {
182
        /** @var TaxonInterface $taxon */
183
        $taxon = $this->taxonFactory->createNew();
184
        $taxon->setCode(StringInflector::nameToCode($names['en_US']));
185
        foreach ($names as $locale => $name) {
186
            /** @var TranslationInterface|TaxonTranslationInterface $taxonTranslation */
187
            $taxonTranslation = $this->taxonTranslationFactory->createNew();
188
            $taxonTranslation->setLocale($locale);
0 ignored issues
show
Bug introduced by
The method setLocale does only exist in Sylius\Component\Resourc...el\TranslationInterface, but not in Sylius\Component\Taxonom...xonTranslationInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
189
            $taxonTranslation->setName($name);
0 ignored issues
show
Bug introduced by
The method setName does only exist in Sylius\Component\Taxonom...xonTranslationInterface, but not in Sylius\Component\Resourc...el\TranslationInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
190
            $taxonTranslation->setSlug($this->taxonSlugGenerator->generate($name));
0 ignored issues
show
Bug introduced by
The method setSlug does only exist in Sylius\Component\Taxonom...xonTranslationInterface, but not in Sylius\Component\Resourc...el\TranslationInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
191
192
            $taxon->addTranslation($taxonTranslation);
0 ignored issues
show
Bug introduced by
It seems like $taxonTranslation defined by $this->taxonTranslationFactory->createNew() on line 187 can also be of type object<Sylius\Component\...onTranslationInterface>; however, Sylius\Component\Resourc...rface::addTranslation() does only seem to accept object<Sylius\Component\...l\TranslationInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
193
        }
194
195
        return $taxon;
196
    }
197
198
    /**
199
     * @param string $name
200
     *
201
     * @return NodeElement
202
     */
203
    private function getParameter($name)
204
    {
205
        return isset($this->minkParameters[$name]) ? $this->minkParameters[$name] : null;
206
    }
207
}
208