Completed
Push — locale-in-url ( 6d9eda...81052c )
by Kamil
57:16 queued 38:25
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 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 go to the addressing step
160
     */
161
    public function iGoToTheAddressingStep()
162
    {
163
        if ($this->selectShippingPage->isOpen()) {
164
            $this->selectShippingPage->changeAddressByStepLabel();
165
166
            return;
167
        }
168
169
        if ($this->selectPaymentPage->isOpen()) {
170
            $this->selectPaymentPage->changeAddressByStepLabel();
171
172
            return;
173
        }
174
175
        if ($this->completePage->isOpen()) {
176
            $this->completePage->changeAddress();
177
178
            return;
179
        }
180
181
        throw new UnexpectedPageException('It is impossible to go to addressing step from current page.');
182
    }
183
184
    /**
185
     * @When I go to the shipping step
186
     */
187
    public function iGoToTheShippingStep()
188
    {
189
        if ($this->selectPaymentPage->isOpen()) {
190
            $this->selectPaymentPage->changeShippingMethodByStepLabel();
191
192
            return;
193
        }
194
195
        if ($this->completePage->isOpen()) {
196
            $this->completePage->changeShippingMethod();
197
198
            return;
199
        }
200
201
        throw new UnexpectedPageException('It is impossible to go to shipping step from current page.');
202
    }
203
204
    /**
205
     * @Then the subtotal of :item item should be :price
206
     */
207
    public function theSubtotalOfItemShouldBe($item, $price)
208
    {
209
        /** @var AddressPageInterface|SelectPaymentPageInterface|SelectShippingPageInterface|CompletePageInterface $currentPage */
210
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([
211
            $this->addressPage,
212
            $this->selectPaymentPage,
213
            $this->selectShippingPage,
214
            $this->completePage,
215
        ]);
216
217
        $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...
218
219
        Assert::eq($actualPrice, $price, sprintf('The %s subtotal should be %s, but is %s', $item, $price, $actualPrice));
220
    }
221
}
222