Conditions | 35 |
Paths | 50 |
Total Lines | 109 |
Code Lines | 61 |
Lines | 19 |
Ratio | 17.43 % |
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 |
||
58 | public function updateCalculatedPrice(&$startingPrice) |
||
59 | { |
||
60 | $newPrice = -1; |
||
61 | $fieldName = $this->owner->ClassName."ID"; |
||
62 | $singleton = ComplexPriceObject::get()->first(); |
||
63 | if ($singleton) { |
||
64 | // Check that ComplexPriceObject can be joined to this type of object |
||
65 | if (!$singleton->hasField($fieldName)) { |
||
66 | $ancestorArray = ClassInfo::ancestry($this->owner, true); |
||
67 | foreach ($ancestorArray as $ancestor) { |
||
68 | $fieldName = $ancestor."ID"; |
||
69 | if ($singleton->hasField($fieldName)) { |
||
70 | break; |
||
71 | } |
||
72 | } |
||
73 | } |
||
74 | |||
75 | // Load up the alternate prices for this product |
||
76 | $prices = ComplexPriceObject::get() |
||
77 | ->filter(array($fieldName => $this->owner->ID, "NoLongerValid" => 0)) |
||
78 | ->sort("NewPrice", "DESC"); |
||
79 | ; |
||
80 | $memberGroupsArray = array(); |
||
81 | if ($prices->count()) { |
||
82 | // Load up the groups for the current memeber, if any |
||
83 | if ($member = Member::currentUser()) { |
||
84 | if ($memberGroupComponents = $member->getManyManyComponents('Groups')) { |
||
85 | if ($memberGroupComponents && $memberGroupComponents->count()) { |
||
86 | $memberGroupsArray = $memberGroupComponents->column("ID"); |
||
87 | if (!is_array($memberGroupsArray)) { |
||
88 | $memberGroupsArray = array(); |
||
89 | } |
||
90 | } |
||
91 | } |
||
92 | } |
||
93 | |||
94 | $countryID = EcommerceCountry::get_country_id(); |
||
95 | |||
96 | // Look at each price and see if it can be used |
||
97 | foreach ($prices as $price) { |
||
98 | $priceCanBeUsed = true; |
||
99 | |||
100 | // Does it pass the group filter? |
||
101 | if ($priceGroupComponents = $price->getManyManyComponents('Groups')) { |
||
102 | if ($priceGroupComponents && $priceGroupComponents->count()) { |
||
103 | $priceCanBeUsed = false; |
||
104 | $priceGroupArray = $priceGroupComponents->column("ID"); |
||
105 | if (!is_array($priceGroupArray)) { |
||
106 | $priceGroupArray = array(); |
||
107 | } |
||
108 | $interSectionArray = array_intersect($priceGroupArray, $memberGroupsArray); |
||
109 | if (is_array($interSectionArray) && count($interSectionArray)) { |
||
110 | $priceCanBeUsed = true; |
||
111 | } |
||
112 | } |
||
113 | } |
||
114 | |||
115 | // Does it pass the country filter? |
||
116 | if ($priceCanBeUsed) { |
||
117 | if ($priceCountryComponents = $price->getManyManyComponents('EcommerceCountries')) { |
||
118 | if ($priceCountryComponents && $priceCountryComponents->count()) { |
||
119 | $priceCanBeUsed = false; |
||
120 | $priceCountryArray = $priceCountryComponents->column("ID"); |
||
121 | if (!is_array($priceCountryArray)) { |
||
122 | $priceCountryArray = array(); |
||
123 | } |
||
124 | if ($countryID && in_array($countryID, $priceCountryArray)) { |
||
125 | $priceCanBeUsed = true; |
||
126 | } |
||
127 | } |
||
128 | } |
||
129 | } |
||
130 | |||
131 | // Does it pass the date filter? |
||
132 | View Code Duplication | if ($priceCanBeUsed) { |
|
133 | $nowTS = strtotime("now"); |
||
134 | if ($price->From) { |
||
135 | $priceCanBeUsed = false; |
||
136 | $fromTS = strtotime($price->From); |
||
137 | if ($fromTS && $fromTS < $nowTS) { |
||
138 | $priceCanBeUsed = true; |
||
139 | } |
||
140 | } |
||
141 | } |
||
142 | |||
143 | View Code Duplication | if ($priceCanBeUsed) { |
|
144 | if ($price->Until) { |
||
145 | $priceCanBeUsed = false; |
||
146 | $untilTS = strtotime($price->Until); |
||
147 | if ($untilTS && $untilTS > $nowTS) { |
||
148 | $priceCanBeUsed = true; |
||
149 | } |
||
150 | } |
||
151 | } |
||
152 | |||
153 | // If so, apply the price |
||
154 | if ($priceCanBeUsed) { |
||
155 | $newPrice = $price->getCalculatedPrice(); |
||
156 | } |
||
157 | } |
||
158 | } |
||
159 | } |
||
160 | |||
161 | if ($newPrice > -1) { |
||
162 | $startingPrice = $newPrice; |
||
163 | } |
||
164 | |||
165 | return $startingPrice; |
||
166 | } |
||
167 | } |
||
200 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.