| Conditions | 1 |
| Paths | 1 |
| Total Lines | 78 |
| Code Lines | 58 |
| 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 |
||
| 59 | public function testDuplicateManyManyClasses() { |
||
| 60 | //create new test classes below |
||
| 61 | $one = new DataObjectDuplicateTestClass1(); |
||
| 62 | $two = new DataObjectDuplicateTestClass2(); |
||
| 63 | $three = new DataObjectDuplicateTestClass3(); |
||
| 64 | |||
| 65 | //set some simple fields |
||
| 66 | $text1 = "Test Text 1"; |
||
| 67 | $text2 = "Test Text 2"; |
||
| 68 | $text3 = "Test Text 3"; |
||
| 69 | $one->text = $text1; |
||
| 70 | $two->text = $text2; |
||
| 71 | $three->text = $text3; |
||
| 72 | |||
| 73 | //write the to DB |
||
| 74 | $one->write(); |
||
| 75 | $two->write(); |
||
| 76 | $three->write(); |
||
| 77 | |||
| 78 | //create relations |
||
| 79 | $one->twos()->add($two); |
||
| 80 | $one->threes()->add($three, array('TestExtra'=>'three')); |
||
| 81 | |||
| 82 | $one = DataObject::get_by_id("DataObjectDuplicateTestClass1", $one->ID); |
||
| 83 | $two = DataObject::get_by_id("DataObjectDuplicateTestClass2", $two->ID); |
||
| 84 | $three = DataObject::get_by_id("DataObjectDuplicateTestClass3", $three->ID); |
||
| 85 | |||
| 86 | $this->assertCount(1, $one->twos(), |
||
| 87 | "Many-to-one relation not copied (has_many)"); |
||
| 88 | $this->assertCount(1, $one->threes(), |
||
| 89 | "Object has the correct number of relations"); |
||
| 90 | $this->assertCount(1, $three->ones(), |
||
| 91 | "Object has the correct number of relations"); |
||
| 92 | |||
| 93 | //test duplication |
||
| 94 | $oneCopy = $one->duplicate(); |
||
| 95 | $twoCopy = $two->duplicate(); |
||
| 96 | $threeCopy = $three->duplicate(); |
||
| 97 | |||
| 98 | $oneCopy = DataObject::get_by_id("DataObjectDuplicateTestClass1", $oneCopy->ID); |
||
| 99 | $twoCopy = DataObject::get_by_id("DataObjectDuplicateTestClass2", $twoCopy->ID); |
||
| 100 | $threeCopy = DataObject::get_by_id("DataObjectDuplicateTestClass3", $threeCopy->ID); |
||
| 101 | |||
| 102 | $this->assertNotNull($oneCopy, "Copy of 1 exists"); |
||
| 103 | $this->assertNotNull($twoCopy, "Copy of 2 exists"); |
||
| 104 | $this->assertNotNull($threeCopy, "Copy of 3 exists"); |
||
| 105 | |||
| 106 | $this->assertEquals($text1, $oneCopy->text); |
||
| 107 | $this->assertEquals($text2, $twoCopy->text); |
||
| 108 | $this->assertEquals($text3, $threeCopy->text); |
||
| 109 | |||
| 110 | $this->assertCount(0, $oneCopy->twos(), |
||
| 111 | "Many-to-one relation not copied (has_many)"); |
||
| 112 | $this->assertCount(2, $oneCopy->threes(), |
||
| 113 | "Object has the correct number of relations"); |
||
| 114 | $this->assertCount(2, $threeCopy->ones(), |
||
| 115 | "Object has the correct number of relations"); |
||
| 116 | |||
| 117 | $this->assertEquals($one->ID, $twoCopy->one()->ID, |
||
| 118 | "Match between relation of copy and the original"); |
||
| 119 | $this->assertCount(0, $oneCopy->twos(), |
||
| 120 | "Many-to-one relation not copied (has_many)"); |
||
| 121 | $this->assertContains( |
||
| 122 | $three->ID, |
||
| 123 | $oneCopy->threes()->column('ID'), |
||
| 124 | "Match between relation of copy and the original" |
||
| 125 | ); |
||
| 126 | $this->assertContains( |
||
| 127 | $one->ID, |
||
| 128 | $threeCopy->ones()->column('ID'), |
||
| 129 | "Match between relation of copy and the original" |
||
| 130 | ); |
||
| 131 | $this->assertContains( |
||
| 132 | 'three', |
||
| 133 | $oneCopy->threes()->column('TestExtra'), |
||
| 134 | "Match between extra field of copy and the original" |
||
| 135 | ); |
||
| 136 | } |
||
| 137 | |||
| 192 |
Since your code implements the magic setter
_set, this function will be called for any write access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.