Completed
Push — locale-in-url ( 0f5e8a...c8ed20 )
by Kamil
20:36
created

CheckoutContext::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
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\Ui\Shop;
13
14
use Behat\Behat\Context\Context;
15
use Sylius\Behat\Context\Ui\Shop\Checkout\CheckoutAddressingContext;
16
use Sylius\Behat\Context\Ui\Shop\Checkout\CheckoutPaymentContext;
17
use Sylius\Behat\Context\Ui\Shop\Checkout\CheckoutShippingContext;
18
use Sylius\Behat\Page\Shop\Checkout\AddressPageInterface;
19
use Sylius\Behat\Page\Shop\Checkout\CompletePageInterface;
20
use Sylius\Behat\Page\Shop\Checkout\SelectPaymentPageInterface;
21
use Sylius\Behat\Page\Shop\Checkout\SelectShippingPageInterface;
22
use Sylius\Behat\Page\UnexpectedPageException;
23
use Sylius\Behat\Service\Resolver\CurrentPageResolverInterface;
24
use Sylius\Component\Addressing\Model\CountryInterface;
25
use Webmozart\Assert\Assert;
26
27
/**
28
 * @author Arkadiusz Krakowiak <[email protected]>
29
 */
30
final class CheckoutContext implements Context
31
{
32
    /**
33
     * @var AddressPageInterface
34
     */
35
    private $addressPage;
36
37
    /**
38
     * @var SelectPaymentPageInterface
39
     */
40
    private $selectPaymentPage;
41
42
    /**
43
     * @var SelectShippingPageInterface
44
     */
45
    private $selectShippingPage;
46
47
    /**
48
     * @var CompletePageInterface
49
     */
50
    private $completePage;
51
52
    /**
53
     * @var CurrentPageResolverInterface
54
     */
55
    private $currentPageResolver;
56
57
    /**
58
     * @var CheckoutAddressingContext
59
     */
60
    private $addressingContext;
61
62
    /**
63
     * @var CheckoutShippingContext
64
     */
65
    private $shippingContext;
66
67
    /**
68
     * @var CheckoutPaymentContext
69
     */
70
    private $paymentContext;
71
72
    /**
73
     * @param AddressPageInterface $addressPage
74
     * @param SelectPaymentPageInterface $selectPaymentPage
75
     * @param SelectShippingPageInterface $selectShippingPage
76
     * @param CompletePageInterface $completePage
77
     * @param CurrentPageResolverInterface $currentPageResolver
78
     * @param CheckoutAddressingContext $addressingContext
79
     * @param CheckoutShippingContext $shippingContext
80
     * @param CheckoutPaymentContext $paymentContext
81
     */
82
    public function __construct(
83
        AddressPageInterface $addressPage,
84
        SelectPaymentPageInterface $selectPaymentPage,
85
        SelectShippingPageInterface $selectShippingPage,
86
        CompletePageInterface $completePage,
87
        CurrentPageResolverInterface $currentPageResolver,
88
        CheckoutAddressingContext $addressingContext,
89
        CheckoutShippingContext $shippingContext,
90
        CheckoutPaymentContext $paymentContext
91
    ) {
92
        $this->addressPage = $addressPage;
93
        $this->selectPaymentPage = $selectPaymentPage;
94
        $this->selectShippingPage = $selectShippingPage;
95
        $this->completePage = $completePage;
96
        $this->currentPageResolver = $currentPageResolver;
97
        $this->addressingContext = $addressingContext;
98
        $this->shippingContext = $shippingContext;
99
        $this->paymentContext = $paymentContext;
100
    }
101
102
    /**
103
     * @Given I was at the checkout summary step
104
     */
105
    public function iWasAtTheCheckoutSummaryStep()
106
    {
107
        $this->addressingContext->iSpecifiedTheShippingAddress();
108
        $this->iProceedOrderWithShippingMethodAndPayment('Free', 'Offline');
109
    }
110
111
    /**
112
     * @Given I chose :shippingMethodName shipping method
113
     * @When I proceed selecting :shippingMethodName shipping method
114
     */
115
    public function iProceedSelectingShippingMethod($shippingMethodName)
116
    {
117
        $this->iProceedSelectingShippingCountryAndShippingMethod(null, $shippingMethodName);
118
    }
119
120
    /**
121
     * @Given I have proceeded selecting :paymentMethodName payment method
122
     * @When I proceed selecting :paymentMethodName payment method
123
     */
124
    public function iProceedSelectingPaymentMethod($paymentMethodName)
125
    {
126
        $this->iProceedSelectingShippingCountryAndShippingMethod();
127
        $this->paymentContext->iChoosePaymentMethod($paymentMethodName);
128
    }
129
130
    /**
131
     * @Given I have proceeded order with :shippingMethodName shipping method and :paymentMethodName payment
132
     * @When I proceed with :shippingMethodName shipping method and :paymentMethodName payment
133
     */
134
    public function iProceedOrderWithShippingMethodAndPayment($shippingMethodName, $paymentMethodName)
135
    {
136
        $this->shippingContext->iHaveProceededSelectingShippingMethod($shippingMethodName);
137
        $this->paymentContext->iChoosePaymentMethod($paymentMethodName);
138
    }
139
140
    /**
141
     * @When I proceed through checkout process
142
     * @When I proceed through checkout process in the :localeCode locale
143
     */
144
    public function iProceedThroughCheckoutProcess($localeCode = 'en_US')
145
    {
146
        $this->addressingContext->iProceedSelectingShippingCountry(null, $localeCode);
147
        $this->shippingContext->iCompleteTheShippingStep();
148
        $this->paymentContext->iCompleteThePaymentStep();
149
    }
150
151
    /**
152
     * @When /^I proceed selecting ("[^"]+" as shipping country) with "([^"]+)" method$/
153
     */
154
    public function iProceedSelectingShippingCountryAndShippingMethod(CountryInterface $shippingCountry = null, $shippingMethodName = null)
155
    {
156
        $this->addressingContext->iProceedSelectingShippingCountry($shippingCountry);
157
        $this->shippingContext->iHaveProceededSelectingShippingMethod($shippingMethodName ?: 'Free');
158
    }
159
160
    /**
161
     * @When /^I change shipping method to "([^"]*)"$/
162
     */
163
    public function iChangeShippingMethod($shippingMethodName)
164
    {
165
        $this->paymentContext->iDecideToChangeMyShippingMethod();
166
        $this->shippingContext->iHaveProceededSelectingShippingMethod($shippingMethodName);
167
    }
168
169
    /**
170
     * @When I go to the addressing step
171
     */
172
    public function iGoToTheAddressingStep()
173
    {
174
        if ($this->selectShippingPage->isOpen()) {
175
            $this->selectShippingPage->changeAddressByStepLabel();
176
177
            return;
178
        }
179
180
        if ($this->selectPaymentPage->isOpen()) {
181
            $this->selectPaymentPage->changeAddressByStepLabel();
182
183
            return;
184
        }
185
186
        if ($this->completePage->isOpen()) {
187
            $this->completePage->changeAddress();
188
189
            return;
190
        }
191
192
        throw new UnexpectedPageException('It is impossible to go to addressing step from current page.');
193
    }
194
195
    /**
196
     * @When I go to the shipping step
197
     */
198
    public function iGoToTheShippingStep()
199
    {
200
        if ($this->selectPaymentPage->isOpen()) {
201
            $this->selectPaymentPage->changeShippingMethodByStepLabel();
202
203
            return;
204
        }
205
206
        if ($this->completePage->isOpen()) {
207
            $this->completePage->changeShippingMethod();
208
209
            return;
210
        }
211
212
        throw new UnexpectedPageException('It is impossible to go to shipping step from current page.');
213
    }
214
215
    /**
216
     * @Then the subtotal of :item item should be :price
217
     */
218
    public function theSubtotalOfItemShouldBe($item, $price)
219
    {
220
        /** @var AddressPageInterface|SelectPaymentPageInterface|SelectShippingPageInterface|CompletePageInterface $currentPage */
221
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([
222
            $this->addressPage,
223
            $this->selectPaymentPage,
224
            $this->selectShippingPage,
225
            $this->completePage,
226
        ]);
227
228
        Assert::eq($currentPage->getItemSubtotal($item), $price);
0 ignored issues
show
Bug introduced by
The method getItemSubtotal does only exist in Sylius\Behat\Page\Shop\C...ctShippingPageInterface, but not in Sylius\Behat\Page\Shop\C...t\CompletePageInterface.

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...
229
    }
230
}
231