Conditions | 6 |
Paths | 13 |
Total Lines | 115 |
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 |
||
131 | public function testAuthenticate($token, $fail, $version) |
||
132 | { |
||
133 | // Already got some credentials stored? |
||
134 | if (!\is_null($token)) |
||
135 | { |
||
136 | $this->object->setToken($token); |
||
137 | $result = $this->object->authenticate(); |
||
138 | $this->assertEquals($result, $token); |
||
139 | } |
||
140 | else |
||
141 | { |
||
142 | $this->object->setOption('requestTokenURL', 'https://example.com/request_token'); |
||
143 | $this->object->setOption('authoriseURL', 'https://example.com/authorize'); |
||
144 | $this->object->setOption('accessTokenURL', 'https://example.com/access_token'); |
||
145 | |||
146 | // Request token. |
||
147 | $returnData = new \stdClass; |
||
148 | $returnData->code = 200; |
||
149 | $returnData->body = 'oauth_token=token&oauth_token_secret=secret&oauth_callback_confirmed=true'; |
||
150 | |||
151 | $this->client->expects($this->at(0)) |
||
152 | ->method('post') |
||
153 | ->with($this->object->getOption('requestTokenURL')) |
||
154 | ->will($this->returnValue($returnData)); |
||
155 | |||
156 | $input = TestHelper::getValue($this->object, 'input'); |
||
157 | $input->set('oauth_verifier', null); |
||
158 | TestHelper::setValue($this->object, 'input', $input); |
||
159 | |||
160 | if (strcmp($version, '1.0a') === 0) |
||
161 | { |
||
162 | $this->object->setOption('callback', 'TEST_URL'); |
||
163 | } |
||
164 | |||
165 | $this->object->authenticate(); |
||
166 | |||
167 | $token = $this->object->getToken(); |
||
168 | $this->assertEquals($token['key'], 'token'); |
||
169 | $this->assertEquals($token['secret'], 'secret'); |
||
170 | |||
171 | // Access token. |
||
172 | $input = TestHelper::getValue($this->object, 'input'); |
||
173 | |||
174 | if (strcmp($version, '1.0a') === 0) |
||
175 | { |
||
176 | TestHelper::setValue($this->object, 'version', $version); |
||
177 | $data = array('oauth_verifier' => 'verifier', 'oauth_token' => 'token'); |
||
178 | } |
||
179 | else |
||
180 | { |
||
181 | TestHelper::setValue($this->object, 'version', $version); |
||
182 | $data = array('oauth_token' => 'token'); |
||
183 | } |
||
184 | |||
185 | TestHelper::setValue($input, 'data', $data); |
||
186 | |||
187 | // Get mock session |
||
188 | $mockSession = $this->getMockBuilder('Joomla\\Session\\Session') |
||
189 | ->disableOriginalConstructor() |
||
190 | ->getMock(); |
||
191 | |||
192 | if ($fail) |
||
193 | { |
||
194 | $mockSession->expects($this->at(0)) |
||
195 | ->method('get') |
||
196 | ->with('oauth_token.key', null) |
||
197 | ->will($this->returnValue('bad')); |
||
198 | |||
199 | $mockSession->expects($this->at(1)) |
||
200 | ->method('get') |
||
201 | ->with('oauth_token.secret', null) |
||
202 | ->will($this->returnValue('session')); |
||
203 | |||
204 | $this->application->setSession($mockSession); |
||
205 | |||
206 | // expectException was added in PHPUnit 5.2 and setExpectedException removed in 6.0 |
||
207 | if (method_exists($this, 'expectException')) |
||
208 | { |
||
209 | $this->expectException('DomainException'); |
||
210 | } |
||
211 | else |
||
212 | { |
||
213 | $this->setExpectedException('DomainException'); |
||
214 | } |
||
215 | |||
216 | $result = $this->object->authenticate(); |
||
217 | } |
||
218 | |||
219 | $mockSession->expects($this->at(0)) |
||
220 | ->method('get') |
||
221 | ->with('oauth_token.key', null) |
||
222 | ->will($this->returnValue('token')); |
||
223 | |||
224 | $mockSession->expects($this->at(1)) |
||
225 | ->method('get') |
||
226 | ->with('oauth_token.secret', null) |
||
227 | ->will($this->returnValue('secret')); |
||
228 | |||
229 | $this->application->setSession($mockSession); |
||
230 | |||
231 | $returnData = new \stdClass; |
||
232 | $returnData->code = 200; |
||
233 | $returnData->body = 'oauth_token=token_key&oauth_token_secret=token_secret'; |
||
234 | |||
235 | $this->client->expects($this->at(0)) |
||
236 | ->method('post') |
||
237 | ->with($this->object->getOption('accessTokenURL')) |
||
238 | ->will($this->returnValue($returnData)); |
||
239 | |||
240 | $result = $this->object->authenticate(); |
||
241 | |||
242 | $this->assertEquals($result['key'], 'token_key'); |
||
243 | $this->assertEquals($result['secret'], 'token_secret'); |
||
244 | } |
||
245 | } |
||
246 | |||
346 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.