Conditions | 6 |
Paths | 9 |
Total Lines | 54 |
Code Lines | 43 |
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 |
||
20 | public function calculateRate(ShippingPackage $package, Address $address) |
||
21 | { |
||
22 | $rate = null; |
||
23 | $ids = Zone::get_zones_for_address($address); |
||
24 | if (!$ids->exists()) { |
||
25 | return $rate; |
||
26 | } |
||
27 | $ids = $ids->map('ID', 'ID')->toArray(); |
||
28 | $packageconstraints = array( |
||
29 | "Weight" => 'weight', |
||
30 | "Volume" => 'volume', |
||
31 | "Value" => 'value', |
||
32 | "Quantity" => 'quantity' |
||
33 | ); |
||
34 | $constraintfilters = array(); |
||
35 | $emptyconstraint = array(); |
||
36 | foreach ($packageconstraints as $db => $pakval) { |
||
37 | $mincol = "\"ZonedShippingRate\".\"{$db}Min\""; |
||
38 | $maxcol = "\"ZonedShippingRate\".\"{$db}Max\""; |
||
39 | $constraintfilters[] = "(". |
||
40 | "$mincol >= 0" . |
||
41 | " AND $mincol <= " . $package->{$pakval}() . |
||
42 | " AND $maxcol > 0". //ignore constraints with maxvalue = 0 |
||
43 | " AND $maxcol >= " . $package->{$pakval}() . |
||
44 | " AND $mincol < $maxcol" . //sanity check |
||
45 | ")"; |
||
46 | //also include a special case where all constraints are empty |
||
47 | $emptyconstraint[] = "($mincol = 0 AND $maxcol = 0)"; |
||
48 | } |
||
49 | $constraintfilters[] = "(".implode(" AND ", $emptyconstraint).")"; |
||
50 | |||
51 | $filter = "(".implode(") AND (", array( |
||
52 | "\"ZonedShippingMethodID\" = ".$this->ID, |
||
53 | "\"ZoneID\" IN(".implode(",", $ids).")", //zone restriction |
||
54 | implode(" OR ", $constraintfilters) //metrics restriction |
||
55 | )).")"; |
||
56 | //order by zone specificity |
||
57 | $orderby = ""; |
||
58 | if (count($ids) > 1) { |
||
59 | $orderby = "CASE \"ZonedShippingRate\".\"ZoneID\""; |
||
60 | $count = 1; |
||
61 | foreach ($ids as $id) { |
||
62 | $orderby .= " WHEN $id THEN $count "; |
||
63 | $count ++; |
||
64 | } |
||
65 | $orderby .= "ELSE $count END ASC,"; |
||
66 | } |
||
67 | $orderby .= "\"ZonedShippingRate\".\"Rate\" ASC"; |
||
68 | if ($sr = DataObject::get_one("ZonedShippingRate", $filter, true, $orderby)) { |
||
69 | $rate = $sr->Rate; |
||
70 | } |
||
71 | $this->CalculatedRate = $rate; |
||
72 | return $rate; |
||
73 | } |
||
74 | |||
153 |