| Conditions | 3 |
| Paths | 4 |
| Total Lines | 102 |
| Code Lines | 78 |
| 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 |
||
| 95 | public function testDocumentTranslation() |
||
| 96 | { |
||
| 97 | $doc = new DOMDocument('1.0'); |
||
| 98 | $doc->load(__DIR__ . '/xml/test1.xml'); |
||
| 99 | |||
| 100 | $translator = new Translator(); |
||
| 101 | $elements = $translator->translateDocument($doc); |
||
| 102 | |||
| 103 | $this->assertCount(1, $elements); |
||
| 104 | $this->assertEquals('soap-env', $elements[0]->getName()); |
||
| 105 | $this->assertCount(2, $elements[0]->getChildren()); |
||
|
|
|||
| 106 | |||
| 107 | $child1 = $elements[0]->getChildren()[0]; |
||
| 108 | $child2 = $elements[0]->getChildren()[1]; |
||
| 109 | |||
| 110 | $this->assertInstanceOf(XmlNode::class, $child1); |
||
| 111 | $this->assertInstanceOf(XmlNode::class, $child2); |
||
| 112 | |||
| 113 | $this->assertEquals('person', $child1->getName()); |
||
| 114 | $this->assertTrue($child1->hasChildren()); |
||
| 115 | $this->assertCount(4, $child1->getChildren()); |
||
| 116 | $this->assertTrue($child1->hasAttributes()); |
||
| 117 | $this->assertCount(1, $child1->getAttributes()); |
||
| 118 | |||
| 119 | $this->assertEquals('name', $child1->getAttributes()[0]->getName()); |
||
| 120 | $this->assertEquals('Max Musterman', $child1->getAttributes()[0]->getValue()); |
||
| 121 | |||
| 122 | $this->assertInstanceOf(XmlElement::class, $child1->getChildren()[0]); |
||
| 123 | $this->assertEquals('car', $child1->getChildren()[0]->getName()); |
||
| 124 | $this->assertFalse($child1->getChildren()[0]->hasValue()); |
||
| 125 | $this->assertCount(2, $child1->getChildren()[0]->getAttributes()); |
||
| 126 | $this->assertEquals('marke', $child1->getChildren()[0]->getAttributes()[0]->getName()); |
||
| 127 | $this->assertEquals('BMW', $child1->getChildren()[0]->getAttributes()[0]->getValue()); |
||
| 128 | |||
| 129 | $this->assertInstanceOf(XmlElement::class, $child1->getChildren()[1]); |
||
| 130 | $this->assertEquals('phone', $child1->getChildren()[1]->getName()); |
||
| 131 | $this->assertEquals(9, $child1->getChildren()[1]->getValue()); |
||
| 132 | $this->assertCount(1, $child1->getChildren()[1]->getAttributes()); |
||
| 133 | $this->assertEquals('name', $child1->getChildren()[1]->getAttributes()[0]->getName()); |
||
| 134 | $this->assertEquals('iPhone', $child1->getChildren()[1]->getAttributes()[0]->getValue()); |
||
| 135 | |||
| 136 | $this->assertInstanceOf(XmlElement::class, $child1->getChildren()[2]); |
||
| 137 | $this->assertEquals('birth-place', $child1->getChildren()[2]->getName()); |
||
| 138 | $this->assertEquals('Hamburg', $child1->getChildren()[2]->getValue()); |
||
| 139 | $this->assertFalse($child1->getChildren()[2]->hasAttributes()); |
||
| 140 | |||
| 141 | $this->assertInstanceOf(XmlNode::class, $child1->getChildren()[3]); |
||
| 142 | $this->assertEquals('address', $child1->getChildren()[3]->getName()); |
||
| 143 | $this->assertFalse($child1->getChildren()[3]->hasValue()); |
||
| 144 | $this->assertFalse($child1->getChildren()[3]->hasAttributes()); |
||
| 145 | $this->assertCount(2, $child1->getChildren()[3]->getChildren()); |
||
| 146 | |||
| 147 | $i = 0; |
||
| 148 | foreach (['street' => 'Hauptstraße 1', 'plz' => '245698'] as $name => $value) { |
||
| 149 | $this->assertEquals($name, $child1->getChildren()[3]->getChildren()[$i]->getName()); |
||
| 150 | $this->assertEquals($value, $child1->getChildren()[3]->getChildren()[$i]->getValue()); |
||
| 151 | |||
| 152 | $i++; |
||
| 153 | } |
||
| 154 | |||
| 155 | $this->assertEquals('person', $child2->getName()); |
||
| 156 | $this->assertTrue($child2->hasAttributes()); |
||
| 157 | $this->assertCount(1, $child2->getAttributes()); |
||
| 158 | $this->assertTrue($child2->hasChildren()); |
||
| 159 | $this->assertCount(4, $child2->getChildren()); |
||
| 160 | |||
| 161 | $this->assertEquals('name', $child2->getAttributes()[0]->getName()); |
||
| 162 | $this->assertEquals('Dr. Dolittle', $child2->getAttributes()[0]->getValue()); |
||
| 163 | |||
| 164 | $this->assertInstanceOf(XmlElement::class, $child2->getChildren()[0]); |
||
| 165 | $this->assertEquals('car', $child2->getChildren()[0]->getName()); |
||
| 166 | $this->assertFalse($child2->getChildren()[0]->hasValue()); |
||
| 167 | $this->assertCount(2, $child2->getChildren()[0]->getAttributes()); |
||
| 168 | $this->assertEquals('marke', $child2->getChildren()[0]->getAttributes()[0]->getName()); |
||
| 169 | $this->assertEquals('Audi', $child2->getChildren()[0]->getAttributes()[0]->getValue()); |
||
| 170 | |||
| 171 | $this->assertInstanceOf(XmlElement::class, $child2->getChildren()[1]); |
||
| 172 | $this->assertEquals('phone', $child2->getChildren()[1]->getName()); |
||
| 173 | $this->assertEquals('Xperia Z3', $child2->getChildren()[1]->getValue()); |
||
| 174 | $this->assertCount(1, $child2->getChildren()[1]->getAttributes()); |
||
| 175 | $this->assertEquals('name', $child2->getChildren()[1]->getAttributes()[0]->getName()); |
||
| 176 | $this->assertEquals('Sony', $child2->getChildren()[1]->getAttributes()[0]->getValue()); |
||
| 177 | |||
| 178 | $this->assertInstanceOf(XmlElement::class, $child2->getChildren()[2]); |
||
| 179 | $this->assertEquals('birth-place', $child2->getChildren()[2]->getName()); |
||
| 180 | $this->assertEquals('München', $child2->getChildren()[2]->getValue()); |
||
| 181 | $this->assertFalse($child2->getChildren()[2]->hasAttributes()); |
||
| 182 | |||
| 183 | $this->assertInstanceOf(XmlNode::class, $child2->getChildren()[3]); |
||
| 184 | $this->assertEquals('address', $child2->getChildren()[3]->getName()); |
||
| 185 | $this->assertFalse($child2->getChildren()[3]->hasValue()); |
||
| 186 | $this->assertFalse($child2->getChildren()[3]->hasAttributes()); |
||
| 187 | $this->assertCount(2, $child2->getChildren()[3]->getChildren()); |
||
| 188 | |||
| 189 | $i = 0; |
||
| 190 | foreach (['street' => 'Partkstraße', 'plz' => '365494'] as $name => $value) { |
||
| 191 | $this->assertEquals($name, $child2->getChildren()[3]->getChildren()[$i]->getName()); |
||
| 192 | $this->assertEquals($value, $child2->getChildren()[3]->getChildren()[$i]->getValue()); |
||
| 193 | |||
| 194 | $i++; |
||
| 195 | } |
||
| 196 | } |
||
| 197 | } |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: