| Conditions | 1 |
| Paths | 1 |
| Total Lines | 55 |
| Code Lines | 33 |
| 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 |
||
| 104 | public function testSetValueByMethod() |
||
| 105 | { |
||
| 106 | $facade = new ObjectFacade( |
||
| 107 | new class() { |
||
| 108 | private $foo = 42; |
||
| 109 | private $bar; |
||
| 110 | |||
| 111 | public function setFoo(int $foo) |
||
| 112 | { |
||
| 113 | $this->foo = $foo; |
||
| 114 | } |
||
| 115 | |||
| 116 | public function setFooBar() |
||
| 117 | { |
||
| 118 | $this->foo = 1; |
||
| 119 | $this->bar = 2; |
||
| 120 | } |
||
| 121 | |||
| 122 | public function getFoo(): int |
||
| 123 | { |
||
| 124 | return $this->foo; |
||
| 125 | } |
||
| 126 | |||
| 127 | public function setBar(int $bar = null) |
||
| 128 | { |
||
| 129 | $this->bar = $bar; |
||
| 130 | } |
||
| 131 | |||
| 132 | public function getBar() |
||
| 133 | { |
||
| 134 | return $this->bar; |
||
| 135 | } |
||
| 136 | } |
||
| 137 | ); |
||
| 138 | |||
| 139 | $this->assertEquals(42, $facade->getValueByMethod('foo')); |
||
| 140 | $facade->setValueByMethod('foo', 23); |
||
| 141 | $this->assertEquals(23, $facade->getValueByMethod('foo')); |
||
| 142 | $facade->setValueByMethod('foo', null); |
||
| 143 | $this->assertEquals(23, $facade->getValueByMethod('foo')); |
||
| 144 | $facade->setValueByMethod('foo', 'abc'); |
||
| 145 | $this->assertEquals(23, $facade->getValueByMethod('foo')); |
||
| 146 | $facade->setValue('foo', 3537); |
||
| 147 | $this->assertEquals(3537, $facade->getValue('foo')); |
||
| 148 | |||
| 149 | $this->assertNull($facade->getValueByMethod('bar')); |
||
| 150 | $facade->setValueByMethod('bar', 1337); |
||
| 151 | $this->assertEquals(1337, $facade->getValueByMethod('bar')); |
||
| 152 | $facade->setValueByMethod('bar', null); |
||
| 153 | $this->assertNull($facade->getValueByMethod('bar')); |
||
| 154 | |||
| 155 | $facade->setValueByMethod('foobar', uniqid()); |
||
| 156 | $this->assertEquals(1, $facade->getValueByMethod('foo')); |
||
| 157 | $this->assertEquals(2, $facade->getValueByMethod('bar')); |
||
| 158 | } |
||
| 159 | |||
| 187 | } |