Conditions | 9 |
Paths | 5 |
Total Lines | 54 |
Code Lines | 31 |
Lines | 0 |
Ratio | 0 % |
Tests | 0 |
CRAP Score | 90 |
Changes | 2 | ||
Bugs | 0 | Features | 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 |
||
44 | public function calcularPrecoPrazo( |
||
45 | $service, |
||
46 | $cepTo, |
||
47 | $cepFrom = null, |
||
48 | $weight = 1, |
||
49 | $format = 1, |
||
50 | $length = 16, |
||
51 | $height = 2, |
||
52 | $width = 11, |
||
53 | $diameter = 1, |
||
54 | $price = 0, |
||
55 | $maoPropria = false, |
||
56 | $ar = false |
||
57 | ) { |
||
58 | if ($format == self::FORMAT_ENVELOPE && $weight > 1) { |
||
59 | throw new InvalidArgumentException('The weight value can not be greater than 1kg when the format is letter'); |
||
60 | } |
||
61 | if ($length < 16) { |
||
62 | throw new InvalidArgumentException('Length less than 16cm is not accepted'); |
||
63 | } |
||
64 | if ($height < 2) { |
||
65 | throw new InvalidArgumentException('Height less than 2cm is not accepted'); |
||
66 | } |
||
67 | if ($width < 11) { |
||
68 | throw new InvalidArgumentException('Width less than 11cm is not accepted'); |
||
69 | } |
||
70 | |||
71 | $cepFrom = $cepFrom ?: $this->getConfig()->getSender()->getCep(); |
||
72 | |||
73 | $request = '<CalcPrecoPrazo xmlns="http://tempuri.org/">'; |
||
74 | $request .= sprintf('<nCdEmpresa>%s</nCdEmpresa>', $this->getConfig()->getAdministrativeCode()); |
||
75 | $request .= sprintf('<sDsSenha>%s</sDsSenha>', $this->getConfig()->getPassword()); |
||
76 | $request .= sprintf('<nCdServico>%s</nCdServico>', $service); |
||
77 | $request .= sprintf('<sCepOrigem>%08s</sCepOrigem>', preg_replace('/[^0-9]/', '', $cepFrom)); |
||
78 | $request .= sprintf('<sCepDestino>%08s</sCepDestino>', preg_replace('/[^0-9]/', '', $cepTo)); |
||
79 | $request .= sprintf('<nVlPeso>%d</nVlPeso>', $weight); |
||
80 | $request .= sprintf('<nCdFormato>%d</nCdFormato>', $format); |
||
81 | $request .= sprintf('<nVlComprimento>%d</nVlComprimento>', $length); |
||
82 | $request .= sprintf('<nVlAltura>%d</nVlAltura>', $height); |
||
83 | $request .= sprintf('<nVlLargura>%d</nVlLargura>', $width); |
||
84 | $request .= sprintf('<nVlDiametro>%d</nVlDiametro>', $diameter); |
||
85 | $request .= sprintf('<sCdMaoPropria>%s</sCdMaoPropria>', $maoPropria ? 'S' : 'N'); |
||
86 | $request .= sprintf('<nVlValorDeclarado>%s</nVlValorDeclarado>', $price); |
||
87 | $request .= sprintf('<sCdAvisoRecebimento>%s</sCdAvisoRecebimento>', $ar ? 'S' : 'N'); |
||
88 | $request .= '</CalcPrecoPrazo>'; |
||
89 | $namespaces = []; |
||
90 | $actions = [ |
||
91 | 'curl' => 'http://tempuri.org/CalcPrecoPrazo', |
||
92 | 'native' => 'http://tempuri.org/CalcPrecoPrazo', |
||
93 | ]; |
||
94 | |||
95 | $result = $this->getSoap()->send($this->url(), $actions, $request, $namespaces); |
||
96 | |||
97 | return $result->CalcPrecoPrazoResult; |
||
98 | } |
||
100 |