Conditions | 9 |
Paths | 12 |
Total Lines | 68 |
Code Lines | 47 |
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:
1 | <?php |
||
164 | protected function loadSalesFlows($entity, $owner) |
||
165 | { |
||
166 | if ($entity instanceof Lead) { |
||
167 | $step = 'start_from_lead'; |
||
168 | $parameters = ['lead' => $entity]; |
||
169 | } else { |
||
170 | $step = 'start_from_opportunity'; |
||
171 | $parameters = ['opportunity' => $entity]; |
||
172 | } |
||
173 | |||
174 | $parameters = array_merge( |
||
175 | [ |
||
176 | 'sales_funnel' => null, |
||
177 | 'sales_funnel_owner' => $owner, |
||
178 | 'sales_funnel_start_date' => new \DateTime('now'), |
||
179 | ], |
||
180 | $parameters |
||
181 | ); |
||
182 | |||
183 | $salesFunnel = new SalesFunnel(); |
||
184 | $salesFunnel->setDataChannel($this->getReference('default_channel')); |
||
185 | if (!$this->workflowManager->isStartTransitionAvailable( |
||
186 | 'b2b_flow_sales_funnel', |
||
187 | $step, |
||
188 | $salesFunnel, |
||
189 | $parameters |
||
190 | )) { |
||
191 | return; |
||
192 | } |
||
193 | |||
194 | $salesFunnelItem = $this->workflowManager->startWorkflow( |
||
195 | 'b2b_flow_sales_funnel', |
||
196 | $salesFunnel, |
||
197 | $step, |
||
198 | $parameters |
||
199 | ); |
||
200 | |||
201 | $salesFunnelItem->getData() |
||
202 | ->set('new_opportunity_name', $entity->getName()) |
||
203 | ->set('new_company_name', $entity->getName()); |
||
204 | |||
205 | if ($entity instanceof Lead && !$this->workflowManager->transitIfAllowed($salesFunnelItem, 'qualify')) { |
||
206 | return; |
||
207 | } |
||
208 | |||
209 | if (rand(1, 100) > 10) { |
||
210 | $salesFunnelItem->getData() |
||
211 | ->set('budget_amount', mt_rand(10, 10000)) |
||
212 | ->set('customer_need', mt_rand(10, 10000)) |
||
213 | ->set('proposed_solution', mt_rand(10, 10000)) |
||
214 | ->set('probability', $this->probabilities[array_rand($this->probabilities)]); |
||
215 | |||
216 | if ($this->workflowManager->transitIfAllowed($salesFunnelItem, 'develop') && $this->getRandomBoolean()) { |
||
217 | $salesFunnelItem->getData() |
||
218 | ->set('close_revenue', mt_rand(10, 1000)) |
||
219 | ->set('close_date', new \DateTime()); |
||
220 | |||
221 | if ($this->getRandomBoolean()) { |
||
222 | $this->workflowManager->transitIfAllowed($salesFunnelItem, 'close_as_won'); |
||
223 | } else { |
||
224 | $salesFunnelItem->getData() |
||
225 | ->set('close_reason_name', 'cancelled') |
||
226 | ->set('close_date', new \DateTime('now', new \DateTimeZone('UTC'))); |
||
227 | $this->workflowManager->transitIfAllowed($salesFunnelItem, 'close_as_lost'); |
||
228 | } |
||
229 | } |
||
230 | } |
||
231 | } |
||
232 | |||
260 |
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: