| Conditions | 2 |
| Paths | 2 |
| Total Lines | 242 |
| Code Lines | 217 |
| 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 |
||
| 210 | public function testHandleWithAllFields() |
||
| 211 | { |
||
| 212 | |||
| 213 | // create a dummy CSV file row |
||
| 214 | $row = array( |
||
| 215 | 0 => $attributeCode = 'test_attribute_code', |
||
| 216 | 1 => 'Magento\Catalog\Block\Adminhtml\Product\Helper\Form\Product', |
||
| 217 | 2 => 1, |
||
| 218 | 3 => 1, |
||
| 219 | 4 => 0, |
||
| 220 | 5 => 0, |
||
| 221 | 6 => 0, |
||
| 222 | 7 => 0, |
||
| 223 | 8 => 0, |
||
| 224 | 9 => 0, |
||
| 225 | 10 => 0, |
||
| 226 | 11 => 0, |
||
| 227 | 12 => 0, |
||
| 228 | 13 => 'virtual', |
||
| 229 | 14 => 0, |
||
| 230 | 15 => 0, |
||
| 231 | 16 => 0, |
||
| 232 | 17 => 0, |
||
| 233 | 18 => 0, |
||
| 234 | 19 => 0, |
||
| 235 | 20 => 0, |
||
| 236 | 21 => 0, |
||
| 237 | 22 => 1, |
||
| 238 | 23 => $additionaData = 'swatch_input_type=visual,update_product_preview_image=0,use_product_image_for_swatch=1' |
||
| 239 | ); |
||
| 240 | |||
| 241 | // prepare the exploded additional data |
||
| 242 | $explodedAdditionalData = array(); |
||
| 243 | foreach (explode(',', $additionaData) as $data) { |
||
| 244 | list ($key, $value) = explode('=', $data); |
||
| 245 | $explodedAdditionalData[$key] = $value; |
||
| 246 | } |
||
| 247 | |||
| 248 | // create a mock subject instance |
||
| 249 | $mockSubject = $this->getMockBuilder('TechDivision\Import\Attribute\Observers\AttributeSubjectImpl') |
||
| 250 | ->setMethods(get_class_methods('TechDivision\Import\Attribute\Observers\AttributeSubjectImpl')) |
||
| 251 | ->getMock(); |
||
| 252 | $mockSubject->expects($this->once()) |
||
| 253 | ->method('getRow') |
||
| 254 | ->willReturn($row); |
||
| 255 | $mockSubject->expects($this->exactly(69)) |
||
| 256 | ->method('hasHeader') |
||
| 257 | ->willReturn(true); |
||
| 258 | $mockSubject->expects($this->exactly(46)) |
||
| 259 | ->method('getHeader') |
||
| 260 | ->withConsecutive( |
||
| 261 | array(ColumnKeys::ATTRIBUTE_CODE), |
||
| 262 | array(ColumnKeys::FRONTEND_INPUT_RENDERER), |
||
| 263 | array(ColumnKeys::FRONTEND_INPUT_RENDERER), |
||
| 264 | array(ColumnKeys::IS_GLOBAL), |
||
| 265 | array(ColumnKeys::IS_GLOBAL), |
||
| 266 | array(ColumnKeys::IS_VISIBLE), |
||
| 267 | array(ColumnKeys::IS_VISIBLE), |
||
| 268 | array(ColumnKeys::IS_SEARCHABLE), |
||
| 269 | array(ColumnKeys::IS_SEARCHABLE), |
||
| 270 | array(ColumnKeys::IS_FILTERABLE), |
||
| 271 | array(ColumnKeys::IS_FILTERABLE), |
||
| 272 | array(ColumnKeys::IS_COMPARABLE), |
||
| 273 | array(ColumnKeys::IS_COMPARABLE), |
||
| 274 | array(ColumnKeys::IS_VISIBLE_ON_FRONT), |
||
| 275 | array(ColumnKeys::IS_VISIBLE_ON_FRONT), |
||
| 276 | array(ColumnKeys::IS_HTML_ALLOWED_ON_FRONT), |
||
| 277 | array(ColumnKeys::IS_HTML_ALLOWED_ON_FRONT), |
||
| 278 | array(ColumnKeys::IS_USED_FOR_PRICE_RULES), |
||
| 279 | array(ColumnKeys::IS_USED_FOR_PRICE_RULES), |
||
| 280 | array(ColumnKeys::IS_FILTERABLE_IN_SEARCH), |
||
| 281 | array(ColumnKeys::IS_FILTERABLE_IN_SEARCH), |
||
| 282 | array(ColumnKeys::USED_IN_PRODUCT_LISTING), |
||
| 283 | array(ColumnKeys::USED_IN_PRODUCT_LISTING), |
||
| 284 | array(ColumnKeys::USED_FOR_SORT_BY), |
||
| 285 | array(ColumnKeys::USED_FOR_SORT_BY), |
||
| 286 | array(ColumnKeys::APPLY_TO), |
||
| 287 | array(ColumnKeys::APPLY_TO), |
||
| 288 | array(ColumnKeys::IS_VISIBLE_IN_ADVANCED_SEARCH), |
||
| 289 | array(ColumnKeys::IS_VISIBLE_IN_ADVANCED_SEARCH), |
||
| 290 | array(ColumnKeys::POSITION), |
||
| 291 | array(ColumnKeys::POSITION), |
||
| 292 | array(ColumnKeys::IS_WYSIWYG_ENABLED), |
||
| 293 | array(ColumnKeys::IS_WYSIWYG_ENABLED), |
||
| 294 | array(ColumnKeys::IS_USED_FOR_PROMO_RULES), |
||
| 295 | array(ColumnKeys::IS_USED_FOR_PROMO_RULES), |
||
| 296 | array(ColumnKeys::IS_REQUIRED_IN_ADMIN_STORE), |
||
| 297 | array(ColumnKeys::IS_REQUIRED_IN_ADMIN_STORE), |
||
| 298 | array(ColumnKeys::IS_USED_IN_GRID), |
||
| 299 | array(ColumnKeys::IS_USED_IN_GRID), |
||
| 300 | array(ColumnKeys::IS_VISIBLE_IN_GRID), |
||
| 301 | array(ColumnKeys::IS_VISIBLE_IN_GRID), |
||
| 302 | array(ColumnKeys::IS_FILTERABLE_IN_GRID), |
||
| 303 | array(ColumnKeys::IS_FILTERABLE_IN_GRID), |
||
| 304 | array(ColumnKeys::SEARCH_WEIGHT), |
||
| 305 | array(ColumnKeys::SEARCH_WEIGHT), |
||
| 306 | array(ColumnKeys::ADDITIONAL_DATA), |
||
| 307 | array(ColumnKeys::ADDITIONAL_DATA) |
||
| 308 | ) |
||
| 309 | ->willReturnOnConsecutiveCalls( |
||
| 310 | 0, |
||
| 311 | 1, |
||
| 312 | 1, |
||
| 313 | 2, |
||
| 314 | 2, |
||
| 315 | 3, |
||
| 316 | 3, |
||
| 317 | 4, |
||
| 318 | 4, |
||
| 319 | 5, |
||
| 320 | 5, |
||
| 321 | 6, |
||
| 322 | 6, |
||
| 323 | 7, |
||
| 324 | 7, |
||
| 325 | 8, |
||
| 326 | 8, |
||
| 327 | 9, |
||
| 328 | 9, |
||
| 329 | 10, |
||
| 330 | 10, |
||
| 331 | 11, |
||
| 332 | 11, |
||
| 333 | 12, |
||
| 334 | 12, |
||
| 335 | 13, |
||
| 336 | 13, |
||
| 337 | 14, |
||
| 338 | 14, |
||
| 339 | 15, |
||
| 340 | 15, |
||
| 341 | 16, |
||
| 342 | 16, |
||
| 343 | 17, |
||
| 344 | 17, |
||
| 345 | 18, |
||
| 346 | 18, |
||
| 347 | 19, |
||
| 348 | 19, |
||
| 349 | 20, |
||
| 350 | 20, |
||
| 351 | 21, |
||
| 352 | 21, |
||
| 353 | 22, |
||
| 354 | 22, |
||
| 355 | 23, |
||
| 356 | 23 |
||
| 357 | ); |
||
| 358 | $mockSubject->expects($this->once()) |
||
| 359 | ->method('hasBeenProcessed') |
||
| 360 | ->with($attributeCode) |
||
| 361 | ->willReturn(false); |
||
| 362 | $mockSubject->expects($this->exactly(4)) |
||
| 363 | ->method('explode') |
||
| 364 | ->withConsecutive( |
||
| 365 | array($additionaData), |
||
| 366 | array('swatch_input_type=visual', '='), |
||
| 367 | array('update_product_preview_image=0', '='), |
||
| 368 | array('use_product_image_for_swatch=1', '=') |
||
| 369 | ) |
||
| 370 | ->willReturnOnConsecutiveCalls( |
||
| 371 | array('swatch_input_type=visual', 'update_product_preview_image=0', 'use_product_image_for_swatch=1'), |
||
| 372 | array('swatch_input_type', 'visual'), |
||
| 373 | array('update_product_preview_image', '0'), |
||
| 374 | array('use_product_image_for_swatch', '1') |
||
| 375 | ); |
||
| 376 | $mockSubject->expects($this->once()) |
||
| 377 | ->method('getLastAttributeId') |
||
| 378 | ->willReturn($lastAttributeId = 1001); |
||
| 379 | |||
| 380 | // initialize the existing entity |
||
| 381 | $existingEntity = array( |
||
| 382 | MemberNames::ATTRIBUTE_ID => $lastAttributeId, |
||
| 383 | MemberNames::FRONTEND_INPUT_RENDERER => 'Magento\Catalog\Block\Adminhtml\Product\Helper\Form\Category', |
||
| 384 | MemberNames::IS_GLOBAL => 0, |
||
| 385 | MemberNames::IS_VISIBLE => 0, |
||
| 386 | MemberNames::IS_SEARCHABLE => 1, |
||
| 387 | MemberNames::IS_FILTERABLE => 1, |
||
| 388 | MemberNames::IS_COMPARABLE => 1, |
||
| 389 | MemberNames::IS_VISIBLE_ON_FRONT => 1, |
||
| 390 | MemberNames::IS_HTML_ALLOWED_ON_FRONT => 1, |
||
| 391 | MemberNames::IS_USED_FOR_PRICE_RULES => 1, |
||
| 392 | MemberNames::IS_FILTERABLE_IN_SEARCH => 1, |
||
| 393 | MemberNames::USED_IN_PRODUCT_LISTING => 1, |
||
| 394 | MemberNames::USED_FOR_SORT_BY => 1, |
||
| 395 | MemberNames::APPLY_TO => 'simple,virtual', |
||
| 396 | MemberNames::IS_VISIBLE_IN_ADVANCED_SEARCH => 1, |
||
| 397 | MemberNames::POSITION => 1, |
||
| 398 | MemberNames::IS_WYSIWYG_ENABLED => 1, |
||
| 399 | MemberNames::IS_USED_FOR_PROMO_RULES => 1, |
||
| 400 | MemberNames::IS_REQUIRED_IN_ADMIN_STORE => 1, |
||
| 401 | MemberNames::IS_USED_IN_GRID => 1, |
||
| 402 | MemberNames::IS_VISIBLE_IN_GRID => 1, |
||
| 403 | MemberNames::IS_FILTERABLE_IN_GRID => 1, |
||
| 404 | MemberNames::SEARCH_WEIGHT => 0, |
||
| 405 | MemberNames::ADDITIONAL_DATA => serialize(array()), |
||
| 406 | EntityStatus::MEMBER_NAME => EntityStatus::STATUS_UPDATE |
||
| 407 | ); |
||
| 408 | |||
| 409 | // initialize the expected entity |
||
| 410 | $expectedEntity = array( |
||
| 411 | MemberNames::ATTRIBUTE_ID => $lastAttributeId, |
||
| 412 | MemberNames::FRONTEND_INPUT_RENDERER => 'Magento\Catalog\Block\Adminhtml\Product\Helper\Form\Product', |
||
| 413 | MemberNames::IS_GLOBAL => 1, |
||
| 414 | MemberNames::IS_VISIBLE => 1, |
||
| 415 | MemberNames::IS_SEARCHABLE => 0, |
||
| 416 | MemberNames::IS_FILTERABLE => 0, |
||
| 417 | MemberNames::IS_COMPARABLE => 0, |
||
| 418 | MemberNames::IS_VISIBLE_ON_FRONT => 0, |
||
| 419 | MemberNames::IS_HTML_ALLOWED_ON_FRONT => 0, |
||
| 420 | MemberNames::IS_USED_FOR_PRICE_RULES => 0, |
||
| 421 | MemberNames::IS_FILTERABLE_IN_SEARCH => 0, |
||
| 422 | MemberNames::USED_IN_PRODUCT_LISTING => 0, |
||
| 423 | MemberNames::USED_FOR_SORT_BY => 0, |
||
| 424 | MemberNames::APPLY_TO => 'virtual', |
||
| 425 | MemberNames::IS_VISIBLE_IN_ADVANCED_SEARCH => 0, |
||
| 426 | MemberNames::POSITION => 0, |
||
| 427 | MemberNames::IS_WYSIWYG_ENABLED => 0, |
||
| 428 | MemberNames::IS_USED_FOR_PROMO_RULES => 0, |
||
| 429 | MemberNames::IS_REQUIRED_IN_ADMIN_STORE => 0, |
||
| 430 | MemberNames::IS_USED_IN_GRID => 0, |
||
| 431 | MemberNames::IS_VISIBLE_IN_GRID => 0, |
||
| 432 | MemberNames::IS_FILTERABLE_IN_GRID => 0, |
||
| 433 | MemberNames::SEARCH_WEIGHT => 1, |
||
| 434 | MemberNames::ADDITIONAL_DATA => serialize($explodedAdditionalData), |
||
| 435 | EntityStatus::MEMBER_NAME => EntityStatus::STATUS_UPDATE |
||
| 436 | ); |
||
| 437 | |||
| 438 | // mock the method that loads the existing entity |
||
| 439 | $this->mockBunchProcessor->expects($this->once()) |
||
| 440 | ->method('loadCatalogAttribute') |
||
| 441 | ->with($lastAttributeId) |
||
| 442 | ->willReturn($existingEntity); |
||
| 443 | // mock the method that persists the entity |
||
| 444 | $this->mockBunchProcessor->expects($this->once()) |
||
| 445 | ->method('persistCatalogAttribute') |
||
| 446 | ->with($expectedEntity) |
||
| 447 | ->willReturn(null); |
||
| 448 | |||
| 449 | // invoke the handle method |
||
| 450 | $this->assertSame($row, $this->observer->handle($mockSubject)); |
||
| 451 | } |
||
| 452 | } |
||
| 453 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.