Completed
Push — checkout-context-cleanup ( 1e6a42...9f1562 )
by Kamil
18:50
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->iProceedOrderWithShippingMethod($shippingMethodName);
137
        $this->paymentContext->iChoosePaymentMethod($paymentMethodName);
138
    }
139
140
    /**
141
     * @When /^I proceed selecting ("[^"]+" as shipping country) with "([^"]+)" method$/
142
     */
143
    public function iProceedSelectingShippingCountryAndShippingMethod(CountryInterface $shippingCountry = null, $shippingMethodName = null)
144
    {
145
        $this->addressingContext->iProceedSelectingShippingCountry($shippingCountry);
146
        $this->shippingContext->iHaveProceededSelectingShippingMethod($shippingMethodName ?: 'Free');
147
    }
148
149
    /**
150
     * @When /^I change shipping method to "([^"]*)"$/
151
     */
152
    public function iChangeShippingMethod($shippingMethodName)
153
    {
154
        $this->paymentContext->iDecideToChangeMyShippingMethod();
155
        $this->shippingContext->iHaveProceededSelectingShippingMethod($shippingMethodName);
156
    }
157
158
    /**
159
     * @When I proceed with :shippingMethod shipping method
160
     */
161
    public function iProceedOrderWithShippingMethod($shippingMethod)
162
    {
163
        $this->shippingContext->iHaveProceededSelectingShippingMethod($shippingMethod);
164
    }
165
166
    /**
167
     * @When I go to the addressing step
168
     */
169
    public function iGoToTheAddressingStep()
170
    {
171
        if ($this->selectShippingPage->isOpen()) {
172
            $this->selectShippingPage->changeAddressByStepLabel();
173
174
            return;
175
        }
176
177
        if ($this->selectPaymentPage->isOpen()) {
178
            $this->selectPaymentPage->changeAddressByStepLabel();
179
180
            return;
181
        }
182
183
        if ($this->completePage->isOpen()) {
184
            $this->completePage->changeAddress();
185
186
            return;
187
        }
188
189
        throw new UnexpectedPageException('It is impossible to go to addressing step from current page.');
190
    }
191
192
    /**
193
     * @When I go to the shipping step
194
     */
195
    public function iGoToTheShippingStep()
196
    {
197
        if ($this->selectPaymentPage->isOpen()) {
198
            $this->selectPaymentPage->changeShippingMethodByStepLabel();
199
200
            return;
201
        }
202
203
        if ($this->completePage->isOpen()) {
204
            $this->completePage->changeShippingMethod();
205
206
            return;
207
        }
208
209
        throw new UnexpectedPageException('It is impossible to go to shipping step from current page.');
210
    }
211
212
    /**
213
     * @Then the subtotal of :item item should be :price
214
     */
215
    public function theSubtotalOfItemShouldBe($item, $price)
216
    {
217
        /** @var AddressPageInterface|SelectPaymentPageInterface|SelectShippingPageInterface|CompletePageInterface $currentPage */
218
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([
219
            $this->addressPage,
220
            $this->selectPaymentPage,
221
            $this->selectShippingPage,
222
            $this->completePage,
223
        ]);
224
225
        $actualPrice = $currentPage->getItemSubtotal($item);
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...
226
227
        Assert::eq($actualPrice, $price, sprintf('The %s subtotal should be %s, but is %s', $item, $price, $actualPrice));
228
    }
229
}
230