Conditions | 12 |
Paths | 1024 |
Total Lines | 109 |
Code Lines | 67 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
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 declare(strict_types=1); |
||
46 | public static function createSalesChannelContext( |
||
47 | ?Context $baseContext = null, |
||
48 | ?CustomerGroupEntity $currentCustomerGroup = null, |
||
49 | ?SalesChannelEntity $salesChannel = null, |
||
50 | ?CurrencyEntity $currency = null, |
||
51 | ?TaxCollection $taxes = null, |
||
52 | ?CountryEntity $country = null, |
||
53 | ?CountryStateEntity $state = null, |
||
54 | ?CustomerAddressEntity $shipping = null, |
||
55 | ?PaymentMethodEntity $paymentMethod = null, |
||
56 | ?ShippingMethodEntity $shippingMethod = null, |
||
57 | ?CustomerEntity $customer = null, |
||
58 | ?string $token = null, |
||
59 | ?string $domainId = null, |
||
60 | ): SalesChannelContext { |
||
61 | if (!$baseContext) { |
||
62 | $baseContext = Context::createDefaultContext(); |
||
63 | } |
||
64 | if ($salesChannel === null) { |
||
65 | $salesChannel = new SalesChannelEntity(); |
||
66 | $salesChannel->setId('ffa32a50e2d04cf38389a53f8d6cd594'); |
||
67 | $salesChannel->setNavigationCategoryId(Uuid::randomHex()); |
||
68 | $salesChannel->setTaxCalculationType(SalesChannelDefinition::CALCULATION_TYPE_HORIZONTAL); |
||
69 | } |
||
70 | |||
71 | $currency = $currency ?: (new CurrencyEntity())->assign([ |
||
72 | 'id' => '4c8eba11bd3546d786afbed481a6e665', |
||
73 | 'factor' => 1, |
||
74 | ]); |
||
75 | |||
76 | $currency->setFactor(1); |
||
77 | |||
78 | if (!$currentCustomerGroup) { |
||
79 | $currentCustomerGroup = new CustomerGroupEntity(); |
||
80 | $currentCustomerGroup->setId(TestDefaults::FALLBACK_CUSTOMER_GROUP); |
||
81 | $currentCustomerGroup->setDisplayGross(true); |
||
82 | } |
||
83 | |||
84 | if (!$taxes) { |
||
85 | $tax = new TaxEntity(); |
||
86 | $tax->setId('4926035368e34d9fa695e017d7a231b9'); |
||
87 | $tax->setName('test'); |
||
88 | $tax->setTaxRate(19.0); |
||
89 | |||
90 | $taxes = new TaxCollection([$tax]); |
||
91 | } |
||
92 | |||
93 | if (!$country) { |
||
94 | $country = new CountryEntity(); |
||
95 | $country->setId('5cff02b1029741a4891c430bcd9e3603'); |
||
96 | $country->setCustomerTax(new TaxFreeConfig(false, Defaults::CURRENCY, 0)); |
||
97 | $country->setCompanyTax(new TaxFreeConfig(false, Defaults::CURRENCY, 0)); |
||
98 | $country->setName('Germany'); |
||
99 | } |
||
100 | if (!$state) { |
||
101 | $state = new CountryStateEntity(); |
||
102 | $state->setId('bd5e2dcf547e4df6bb1ff58a554bc69e'); |
||
103 | $state->setCountryId($country->getId()); |
||
104 | } |
||
105 | |||
106 | if (!$shipping) { |
||
107 | $shipping = new CustomerAddressEntity(); |
||
108 | $shipping->setCountry($country); |
||
109 | $shipping->setCountryState($state); |
||
110 | } |
||
111 | |||
112 | if (!$paymentMethod) { |
||
113 | $paymentMethod = (new PaymentMethodEntity())->assign( |
||
114 | [ |
||
115 | 'id' => '19d144ffe15f4772860d59fca7f207c1', |
||
116 | 'handlerIdentifier' => SyncTestPaymentHandler::class, |
||
117 | 'name' => 'Generated Payment', |
||
118 | 'active' => true, |
||
119 | ] |
||
120 | ); |
||
121 | } |
||
122 | |||
123 | if (!$shippingMethod) { |
||
124 | $deliveryTime = new DeliveryTimeEntity(); |
||
125 | $deliveryTime->setMin(1); |
||
126 | $deliveryTime->setMax(2); |
||
127 | $deliveryTime->setUnit(DeliveryTimeEntity::DELIVERY_TIME_DAY); |
||
128 | |||
129 | $shippingMethod = new ShippingMethodEntity(); |
||
130 | $shippingMethod->setDeliveryTime($deliveryTime); |
||
131 | $shippingMethod->setId('8beeb66e9dda46b18891a059257a590e'); |
||
132 | } |
||
133 | |||
134 | if (!$customer) { |
||
135 | $customer = (new CustomerEntity())->assign(['id' => Uuid::randomHex()]); |
||
136 | $customer->setId(Uuid::randomHex()); |
||
137 | $customer->setGroup($currentCustomerGroup); |
||
138 | } |
||
139 | |||
140 | return new SalesChannelContext( |
||
141 | $baseContext, |
||
142 | $token ?? Uuid::randomHex(), |
||
143 | $domainId ?? Uuid::randomHex(), |
||
144 | $salesChannel, |
||
145 | $currency, |
||
146 | $currentCustomerGroup, |
||
147 | $taxes, |
||
148 | $paymentMethod, |
||
149 | $shippingMethod, |
||
150 | ShippingLocation::createFromAddress($shipping), |
||
151 | $customer, |
||
152 | new CashRoundingConfig(2, 0.01, true), |
||
153 | new CashRoundingConfig(2, 0.01, true), |
||
154 | [] |
||
155 | ); |
||
228 |