| Conditions | 6 |
| Paths | 18 |
| Total Lines | 216 |
| Code Lines | 157 |
| 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 |
||
| 463 | public function testCopySubtree() |
||
| 464 | { |
||
| 465 | $handler = $this->getPartlyMockedHandler( |
||
| 466 | array( |
||
| 467 | 'load', |
||
| 468 | 'changeMainLocation', |
||
| 469 | 'setSectionForSubtree', |
||
| 470 | 'create', |
||
| 471 | ) |
||
| 472 | ); |
||
| 473 | $subtreeContentRows = array( |
||
| 474 | array('node_id' => 10, 'main_node_id' => 1, 'parent_node_id' => 3, 'contentobject_id' => 21, 'contentobject_version' => 1, 'is_hidden' => 0, 'is_invisible' => 0, 'priority' => 0, 'path_identification_string' => 'test_10', 'sort_field' => 2, 'sort_order' => 1), |
||
| 475 | array('node_id' => 11, 'main_node_id' => 11, 'parent_node_id' => 10, 'contentobject_id' => 211, 'contentobject_version' => 1, 'is_hidden' => 0, 'is_invisible' => 0, 'priority' => 0, 'path_identification_string' => 'test_11', 'sort_field' => 2, 'sort_order' => 1), |
||
| 476 | array('node_id' => 12, 'main_node_id' => 15, 'parent_node_id' => 10, 'contentobject_id' => 215, 'contentobject_version' => 1, 'is_hidden' => 0, 'is_invisible' => 0, 'priority' => 0, 'path_identification_string' => 'test_12', 'sort_field' => 2, 'sort_order' => 1), |
||
| 477 | array('node_id' => 13, 'main_node_id' => 2, 'parent_node_id' => 10, 'contentobject_id' => 22, 'contentobject_version' => 1, 'is_hidden' => 0, 'is_invisible' => 0, 'priority' => 0, 'path_identification_string' => 'test_13', 'sort_field' => 2, 'sort_order' => 1), |
||
| 478 | array('node_id' => 14, 'main_node_id' => 11, 'parent_node_id' => 13, 'contentobject_id' => 211, 'contentobject_version' => 1, 'is_hidden' => 0, 'is_invisible' => 0, 'priority' => 0, 'path_identification_string' => 'test_14', 'sort_field' => 2, 'sort_order' => 1), |
||
| 479 | array('node_id' => 15, 'main_node_id' => 15, 'parent_node_id' => 13, 'contentobject_id' => 215, 'contentobject_version' => 1, 'is_hidden' => 0, 'is_invisible' => 0, 'priority' => 0, 'path_identification_string' => 'test_15', 'sort_field' => 2, 'sort_order' => 1), |
||
| 480 | array('node_id' => 16, 'main_node_id' => 16, 'parent_node_id' => 15, 'contentobject_id' => 216, 'contentobject_version' => 1, 'is_hidden' => 0, 'is_invisible' => 0, 'priority' => 0, 'path_identification_string' => 'test_16', 'sort_field' => 2, 'sort_order' => 1), |
||
| 481 | ); |
||
| 482 | $destinationData = array('node_id' => 5, 'main_node_id' => 5, 'parent_node_id' => 4, 'contentobject_id' => 200, 'contentobject_version' => 1, 'is_hidden' => 0, 'is_invisible' => 1, 'path_identification_string' => 'test_destination'); |
||
| 483 | $mainLocationsMap = array(true, true, true, true, 1011, 1012, true); |
||
| 484 | $updateMainLocationsMap = array(1215 => 1015); |
||
| 485 | $offset = 1000; |
||
| 486 | |||
| 487 | $this->locationGateway |
||
| 488 | ->expects($this->once()) |
||
| 489 | ->method('getSubtreeContent') |
||
| 490 | ->with($subtreeContentRows[0]['node_id']) |
||
| 491 | ->will($this->returnValue($subtreeContentRows)); |
||
| 492 | $this->locationGateway |
||
| 493 | ->expects($this->once()) |
||
| 494 | ->method('getBasicNodeData') |
||
| 495 | ->with($destinationData['node_id']) |
||
| 496 | ->will($this->returnValue($destinationData)); |
||
| 497 | |||
| 498 | $objectStateHandlerCall = 0; |
||
| 499 | $this->objectStateHandler->expects($this->at($objectStateHandlerCall++)) |
||
| 500 | ->method('loadAllGroups') |
||
| 501 | ->will( |
||
| 502 | $this->returnValue( |
||
| 503 | array( |
||
| 504 | new ObjectStateGroup(array('id' => 10)), |
||
| 505 | new ObjectStateGroup(array('id' => 20)), |
||
| 506 | ) |
||
| 507 | ) |
||
| 508 | ); |
||
| 509 | $this->objectStateHandler->expects($this->at($objectStateHandlerCall++)) |
||
| 510 | ->method('loadObjectStates') |
||
| 511 | ->with($this->equalTo(10)) |
||
| 512 | ->will( |
||
| 513 | $this->returnValue( |
||
| 514 | array( |
||
| 515 | new ObjectState(array('id' => 11, 'groupId' => 10)), |
||
| 516 | new ObjectState(array('id' => 12, 'groupId' => 10)), |
||
| 517 | ) |
||
| 518 | ) |
||
| 519 | ); |
||
| 520 | $this->objectStateHandler->expects($this->at($objectStateHandlerCall++)) |
||
| 521 | ->method('loadObjectStates') |
||
| 522 | ->with($this->equalTo(20)) |
||
| 523 | ->will( |
||
| 524 | $this->returnValue( |
||
| 525 | array( |
||
| 526 | new ObjectState(array('id' => 21, 'groupId' => 20)), |
||
| 527 | new ObjectState(array('id' => 22, 'groupId' => 20)), |
||
| 528 | ) |
||
| 529 | ) |
||
| 530 | ); |
||
| 531 | $defaultObjectStates = array( |
||
| 532 | new ObjectState(array('id' => 11, 'groupId' => 10)), |
||
| 533 | new ObjectState(array('id' => 21, 'groupId' => 20)), |
||
| 534 | ); |
||
| 535 | |||
| 536 | $contentIds = array_values( |
||
| 537 | array_unique( |
||
| 538 | array_map( |
||
| 539 | function ($row) { |
||
| 540 | return $row['contentobject_id']; |
||
| 541 | }, |
||
| 542 | $subtreeContentRows |
||
| 543 | ) |
||
| 544 | ) |
||
| 545 | ); |
||
| 546 | foreach ($contentIds as $index => $contentId) { |
||
| 547 | $this->contentHandler |
||
| 548 | ->expects($this->at($index * 2)) |
||
| 549 | ->method('copy') |
||
| 550 | ->with($contentId, 1) |
||
| 551 | ->will( |
||
| 552 | $this->returnValue( |
||
| 553 | new Content( |
||
| 554 | array( |
||
| 555 | 'versionInfo' => new VersionInfo( |
||
| 556 | array( |
||
| 557 | 'contentInfo' => new ContentInfo( |
||
| 558 | array( |
||
| 559 | 'id' => $contentId + $offset, |
||
| 560 | 'currentVersionNo' => 1, |
||
| 561 | ) |
||
| 562 | ), |
||
| 563 | ) |
||
| 564 | ), |
||
| 565 | ) |
||
| 566 | ) |
||
| 567 | ) |
||
| 568 | ); |
||
| 569 | |||
| 570 | foreach ($defaultObjectStates as $objectState) { |
||
| 571 | $this->objectStateHandler->expects($this->at($objectStateHandlerCall++)) |
||
| 572 | ->method('setContentState') |
||
| 573 | ->with( |
||
| 574 | $contentId + $offset, |
||
| 575 | $objectState->groupId, |
||
| 576 | $objectState->id |
||
| 577 | ); |
||
| 578 | } |
||
| 579 | |||
| 580 | $this->contentHandler |
||
| 581 | ->expects($this->at($index * 2 + 1)) |
||
| 582 | ->method('publish') |
||
| 583 | ->with( |
||
| 584 | $contentId + $offset, |
||
| 585 | 1, |
||
| 586 | $this->isInstanceOf('eZ\\Publish\\SPI\\Persistence\\Content\\MetadataUpdateStruct') |
||
| 587 | ) |
||
| 588 | ->will( |
||
| 589 | $this->returnValue( |
||
| 590 | new Content( |
||
| 591 | array( |
||
| 592 | 'versionInfo' => new VersionInfo( |
||
| 593 | array( |
||
| 594 | 'contentInfo' => new ContentInfo( |
||
| 595 | array( |
||
| 596 | 'id' => ($contentId + $offset), |
||
| 597 | ) |
||
| 598 | ), |
||
| 599 | ) |
||
| 600 | ), |
||
| 601 | ) |
||
| 602 | ) |
||
| 603 | ) |
||
| 604 | ); |
||
| 605 | } |
||
| 606 | $lastContentHandlerIndex = $index * 2 + 1; |
||
| 607 | |||
| 608 | $pathStrings = array($destinationData['node_id'] => $destinationData['path_identification_string']); |
||
| 609 | foreach ($subtreeContentRows as $index => $row) { |
||
| 610 | $mapper = new Mapper(); |
||
| 611 | $createStruct = $mapper->getLocationCreateStruct($row); |
||
| 612 | $this->locationMapper |
||
| 613 | ->expects($this->at($index)) |
||
| 614 | ->method('getLocationCreateStruct') |
||
| 615 | ->with($row) |
||
| 616 | ->will($this->returnValue($createStruct)); |
||
| 617 | |||
| 618 | $createStruct = clone $createStruct; |
||
| 619 | $createStruct->contentId = $createStruct->contentId + $offset; |
||
| 620 | $createStruct->parentId = $index === 0 ? $destinationData['node_id'] : $createStruct->parentId + $offset; |
||
| 621 | $createStruct->invisible = true; |
||
| 622 | $createStruct->mainLocationId = $mainLocationsMap[$index]; |
||
| 623 | $createStruct->pathIdentificationString = $pathStrings[$createStruct->parentId] . '/' . $row['path_identification_string']; |
||
| 624 | $pathStrings[$row['node_id'] + $offset] = $createStruct->pathIdentificationString; |
||
| 625 | $handler |
||
| 626 | ->expects($this->at($index)) |
||
| 627 | ->method('create') |
||
| 628 | ->with($createStruct) |
||
| 629 | ->will( |
||
| 630 | $this->returnValue( |
||
| 631 | new Location( |
||
| 632 | array( |
||
| 633 | 'id' => $row['node_id'] + $offset, |
||
| 634 | 'contentId' => $row['contentobject_id'], |
||
| 635 | 'hidden' => false, |
||
| 636 | 'invisible' => true, |
||
| 637 | 'pathIdentificationString' => $createStruct->pathIdentificationString, |
||
| 638 | ) |
||
| 639 | ) |
||
| 640 | ) |
||
| 641 | ); |
||
| 642 | } |
||
| 643 | |||
| 644 | foreach ($updateMainLocationsMap as $contentId => $locationId) { |
||
| 645 | $handler |
||
| 646 | ->expects($this->any()) |
||
| 647 | ->method('changeMainLocation') |
||
| 648 | ->with($contentId, $locationId); |
||
| 649 | } |
||
| 650 | |||
| 651 | $handler |
||
| 652 | ->expects($this->once()) |
||
| 653 | ->method('load') |
||
| 654 | ->with($destinationData['node_id']) |
||
| 655 | ->will($this->returnValue(new Location(array('contentId' => $destinationData['contentobject_id'])))); |
||
| 656 | |||
| 657 | $this->contentHandler |
||
| 658 | ->expects($this->at($lastContentHandlerIndex + 1)) |
||
| 659 | ->method('loadContentInfo') |
||
| 660 | ->with($destinationData['contentobject_id']) |
||
| 661 | ->will($this->returnValue(new ContentInfo(array('sectionId' => 12345)))); |
||
| 662 | |||
| 663 | $this->contentHandler |
||
| 664 | ->expects($this->at($lastContentHandlerIndex + 2)) |
||
| 665 | ->method('loadContentInfo') |
||
| 666 | ->with(21) |
||
| 667 | ->will($this->returnValue(new ContentInfo(array('mainLocationId' => 1010)))); |
||
| 668 | |||
| 669 | $handler |
||
| 670 | ->expects($this->once()) |
||
| 671 | ->method('setSectionForSubtree') |
||
| 672 | ->with($subtreeContentRows[0]['node_id'] + $offset, 12345); |
||
| 673 | |||
| 674 | $handler->copySubtree( |
||
| 675 | $subtreeContentRows[0]['node_id'], |
||
| 676 | $destinationData['node_id'] |
||
| 677 | ); |
||
| 678 | } |
||
| 679 | |||
| 702 |
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.