Conditions | 8 |
Paths | 10 |
Total Lines | 68 |
Code Lines | 59 |
Lines | 9 |
Ratio | 13.24 % |
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 |
||
76 | public function testBatchRequestWithMultipleChildren() |
||
77 | { |
||
78 | $idA = 123; |
||
79 | $idC = 'abc'; |
||
80 | $idD = 'def'; |
||
81 | $methodA = 'concat'; |
||
82 | $methodB = 'nofify'; |
||
83 | $methodC = 'sum'; |
||
84 | $methodD = 'bar'; |
||
85 | $paramsA = ['foo'=>'abc', 'bar'=>'def']; |
||
86 | $paramsB = ['foo'=>false]; |
||
87 | $paramsC = ['foo'=>123, 'bar'=>456]; |
||
88 | $paramsD = ['foo'=>123, 'bar'=>456]; |
||
89 | $requestA = $this->client->request($idA, $methodA, $paramsA); |
||
90 | $requestB = $this->client->notification($methodB, $paramsB); |
||
91 | $requestC = $this->client->request($idC, $methodC, $paramsC); |
||
92 | $requestD = $this->client->request($idD, $methodD, $paramsD); |
||
93 | $responses = $this->client->sendAll([$requestA, $requestB, $requestC, $requestD]); |
||
94 | |||
95 | $this->assertEquals(ClientInterface::SPEC, $requestA->getRpcVersion()); |
||
96 | $this->assertEquals($idA, $requestA->getRpcId()); |
||
97 | $this->assertEquals($methodA, $requestA->getRpcMethod()); |
||
98 | $this->assertEquals($paramsA, $requestA->getRpcParams()); |
||
99 | $this->assertEquals(ClientInterface::SPEC, $requestB->getRpcVersion()); |
||
100 | $this->assertEquals(null, $requestB->getRpcId()); |
||
101 | $this->assertEquals($methodB, $requestB->getRpcMethod()); |
||
102 | $this->assertEquals($paramsB, $requestB->getRpcParams()); |
||
103 | $this->assertEquals(ClientInterface::SPEC, $requestC->getRpcVersion()); |
||
104 | $this->assertEquals($idC, $requestC->getRpcId()); |
||
105 | $this->assertEquals($methodC, $requestC->getRpcMethod()); |
||
106 | $this->assertEquals($paramsC, $requestC->getRpcParams()); |
||
107 | $this->assertEquals(ClientInterface::SPEC, $requestD->getRpcVersion()); |
||
108 | $this->assertEquals($idD, $requestD->getRpcId()); |
||
109 | $this->assertEquals($methodD, $requestD->getRpcMethod()); |
||
110 | $this->assertEquals($paramsD, $requestD->getRpcParams()); |
||
111 | |||
112 | $this->assertTrue(is_array($responses)); |
||
113 | $this->assertEquals(3, count($responses)); |
||
114 | |||
115 | View Code Duplication | foreach ($responses as $response) { |
|
116 | if ($response->getRpcId() === $idA) { |
||
117 | $responseA = $response; |
||
118 | } elseif ($response->getRpcId() === $idC) { |
||
119 | $responseC = $response; |
||
120 | } elseif ($response->getRpcId() === $idD) { |
||
121 | $responseD = $response; |
||
122 | } |
||
123 | } |
||
124 | if (!isset($responseA) || !isset($responseC) || !isset($responseD)) { |
||
125 | $this->fail('Invalid responses'); |
||
126 | } |
||
127 | |||
128 | $this->assertEquals(ClientInterface::SPEC, $responseA->getRpcVersion()); |
||
129 | $this->assertEquals(implode('', $paramsA), $responseA->getRpcResult()); |
||
130 | $this->assertEquals($idA, $responseA->getRpcId()); |
||
131 | $this->assertEquals(null, $responseA->getRpcErrorCode()); |
||
132 | $this->assertEquals(null, $responseA->getRpcErrorMessage()); |
||
133 | $this->assertEquals(ClientInterface::SPEC, $responseC->getRpcVersion()); |
||
134 | $this->assertEquals(array_sum($paramsC), $responseC->getRpcResult()); |
||
135 | $this->assertEquals($idC, $responseC->getRpcId()); |
||
136 | $this->assertEquals(null, $responseC->getRpcErrorCode()); |
||
137 | $this->assertEquals(null, $responseC->getRpcErrorMessage()); |
||
138 | $this->assertEquals(ClientInterface::SPEC, $responseD->getRpcVersion()); |
||
139 | $this->assertEquals(null, $responseD->getRpcResult()); |
||
140 | $this->assertEquals($idD, $responseD->getRpcId()); |
||
141 | $this->assertTrue(is_int($responseD->getRpcErrorCode())); |
||
142 | $this->assertTrue(is_string($responseD->getRpcErrorMessage())); |
||
143 | } |
||
144 | |||
216 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: