| Conditions | 13 |
| Paths | 64 |
| Total Lines | 116 |
| Code Lines | 71 |
| Lines | 8 |
| Ratio | 6.9 % |
| 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 |
||
| 150 | public function importPriceInfo( |
||
| 151 | \CultureFeed_Cdb_Data_DetailList $details, |
||
| 152 | $jsonLD |
||
| 153 | ) { |
||
| 154 | $mainLanguage = $this->getMainLanguage($jsonLD); |
||
| 155 | |||
| 156 | $detailsArray = []; |
||
| 157 | foreach ($details as $detail) { |
||
| 158 | $detailsArray[] = $detail; |
||
| 159 | } |
||
| 160 | $details = $detailsArray; |
||
| 161 | |||
| 162 | $mainLanguageDetails = array_filter( |
||
| 163 | $details, |
||
| 164 | function (\CultureFeed_Cdb_Data_Detail $detail) use ($mainLanguage) { |
||
| 165 | return $detail->getLanguage() === $mainLanguage->getCode(); |
||
| 166 | } |
||
| 167 | ); |
||
| 168 | |||
| 169 | /* @var \CultureFeed_Cdb_Data_EventDetail $mainLanguageDetail */ |
||
| 170 | $mainLanguageDetail = reset($mainLanguageDetails); |
||
| 171 | if (!$mainLanguageDetail) { |
||
| 172 | return; |
||
| 173 | } |
||
| 174 | |||
| 175 | $mainLanguagePrice = $mainLanguageDetail->getPrice(); |
||
| 176 | |||
| 177 | if (!$mainLanguagePrice) { |
||
| 178 | return; |
||
| 179 | } |
||
| 180 | |||
| 181 | $basePrice = $mainLanguagePrice->getValue(); |
||
| 182 | if ($basePrice !== null) { |
||
| 183 | $basePrice = floatval($basePrice); |
||
| 184 | } |
||
| 185 | |||
| 186 | if (!$basePrice) { |
||
| 187 | return; |
||
| 188 | } |
||
| 189 | |||
| 190 | $basePrice = new BasePrice( |
||
| 191 | Price::fromFloat($basePrice), |
||
| 192 | Currency::fromNative('EUR') |
||
| 193 | ); |
||
| 194 | |||
| 195 | /* @var Tariff[] $tariffs */ |
||
| 196 | $tariffs = []; |
||
| 197 | foreach ($details as $detail) { |
||
| 198 | $language = null; |
||
|
|
|||
| 199 | $price = null; |
||
| 200 | $description = null; |
||
| 201 | |||
| 202 | $language = $detail->getLanguage(); |
||
| 203 | |||
| 204 | $price = $detail->getPrice(); |
||
| 205 | if (!$price) { |
||
| 206 | continue; |
||
| 207 | } |
||
| 208 | |||
| 209 | $description = $price->getDescription(); |
||
| 210 | if (!$description) { |
||
| 211 | continue; |
||
| 212 | } |
||
| 213 | |||
| 214 | $translatedTariffs = $this->priceDescriptionParser->parse($description); |
||
| 215 | if (empty($translatedTariffs)) { |
||
| 216 | continue; |
||
| 217 | } |
||
| 218 | |||
| 219 | // Skip the base price. |
||
| 220 | array_shift($translatedTariffs); |
||
| 221 | |||
| 222 | $tariffIndex = 0; |
||
| 223 | foreach ($translatedTariffs as $tariffName => $tariffPrice) { |
||
| 224 | if (!isset($tariffs[$tariffIndex])) { |
||
| 225 | $tariff = new Tariff( |
||
| 226 | new MultilingualString(new Language($language), new StringLiteral($tariffName)), |
||
| 227 | Price::fromFloat($tariffPrice), |
||
| 228 | Currency::fromNative('EUR') |
||
| 229 | ); |
||
| 230 | } else { |
||
| 231 | $tariff = $tariffs[$tariffIndex]; |
||
| 232 | $name = $tariff->getName(); |
||
| 233 | $name = $name->withTranslation(new Language($language), new StringLiteral($tariffName)); |
||
| 234 | $tariff = new Tariff( |
||
| 235 | $name, |
||
| 236 | $tariff->getPrice(), |
||
| 237 | $tariff->getCurrency() |
||
| 238 | ); |
||
| 239 | } |
||
| 240 | |||
| 241 | $tariffs[$tariffIndex] = $tariff; |
||
| 242 | $tariffIndex++; |
||
| 243 | } |
||
| 244 | } |
||
| 245 | |||
| 246 | $jsonLD->priceInfo = [ |
||
| 247 | [ |
||
| 248 | 'category' => 'base', |
||
| 249 | 'name' => [ |
||
| 250 | 'nl' => 'Basistarief', |
||
| 251 | ], |
||
| 252 | 'price' => $basePrice->getPrice()->toFloat(), |
||
| 253 | 'priceCurrency' => $basePrice->getCurrency()->getCode()->toNative(), |
||
| 254 | ] |
||
| 255 | ]; |
||
| 256 | |||
| 257 | View Code Duplication | foreach ($tariffs as $tariff) { |
|
| 258 | $jsonLD->priceInfo[] = [ |
||
| 259 | 'category' => 'tariff', |
||
| 260 | 'name' => $tariff->getName()->serialize(), |
||
| 261 | 'price' => $tariff->getPrice()->toFloat(), |
||
| 262 | 'priceCurrency' => $tariff->getCurrency()->getCode()->toNative(), |
||
| 263 | ]; |
||
| 264 | } |
||
| 265 | } |
||
| 266 | } |
||
| 267 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.