| Conditions | 4 |
| Paths | 27 |
| Total Lines | 159 |
| Code Lines | 102 |
| 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 |
||
| 268 | public function testMatchConcatenation() |
||
| 269 | { |
||
| 270 | // Case 1: There is no statement at the beginning |
||
| 271 | |||
| 272 | $mockParser = $this->getMockBuilder('Netdudes\DataSourceryBundle\UQL\Parser') |
||
| 273 | ->setMethods(['matchStatement']) |
||
| 274 | ->getMock(); |
||
| 275 | $mockParser->expects($this->any()) |
||
| 276 | ->method('matchStatement') |
||
| 277 | ->will($this->returnValue(false)); |
||
| 278 | |||
| 279 | try { |
||
| 280 | $mockParser->matchConcatenation(); |
||
| 281 | $this->fail('Should fail on testing concatenation if doesn\'t start with a statement'); |
||
| 282 | } catch (UQLSyntaxError $e) { |
||
| 283 | // Caught the exception, pass. |
||
| 284 | } |
||
| 285 | |||
| 286 | // Case 2: There is only one element and no logic. |
||
| 287 | |||
| 288 | $mockParser = $this->getMockBuilder('Netdudes\DataSourceryBundle\UQL\Parser') |
||
| 289 | ->setMethods(['matchStatement', 'matchLogic']) |
||
| 290 | ->getMock(); |
||
| 291 | $mockParser->expects($this->any()) |
||
| 292 | ->method('matchStatement') |
||
| 293 | ->will($this->returnValue('MockValidStatement')); |
||
| 294 | $mockParser->expects($this->any()) |
||
| 295 | ->method('matchLogic') |
||
| 296 | ->will($this->returnValue(false)); |
||
| 297 | |||
| 298 | $result = $mockParser->matchConcatenation(); |
||
| 299 | $this->assertEquals('MockValidStatement', $result, "Concatenations with only one member should return the only statement"); |
||
| 300 | |||
| 301 | // Case 3: There isn't a statement after the first logic |
||
| 302 | |||
| 303 | $mockParser = $this->getMockBuilder('Netdudes\DataSourceryBundle\UQL\Parser') |
||
| 304 | ->setMethods(['matchStatement', 'matchLogic', 'getCurrentToken']) |
||
| 305 | ->getMock(); |
||
| 306 | $mockParser->expects($this->any()) |
||
| 307 | ->method('getCurrentToken') |
||
| 308 | ->will( |
||
| 309 | $this->returnValue( |
||
| 310 | [ |
||
| 311 | 'token' => 'T_INVALID', |
||
| 312 | 'match' => 'invalid', |
||
| 313 | ] |
||
| 314 | ) |
||
| 315 | ); |
||
| 316 | $mockParser->expects($this->at(0)) |
||
| 317 | ->method('matchStatement') |
||
| 318 | ->will($this->returnValue('MockValidStatement')); |
||
| 319 | $mockParser->expects($this->at(2)) |
||
| 320 | ->method('matchStatement') |
||
| 321 | ->will($this->returnValue(false)); |
||
| 322 | $mockParser->expects($this->any()) |
||
| 323 | ->method('matchLogic') |
||
| 324 | ->will( |
||
| 325 | $this->returnValue( |
||
| 326 | [ |
||
| 327 | 'token' => 'MockValidLogic', |
||
| 328 | ] |
||
| 329 | ) |
||
| 330 | ); |
||
| 331 | |||
| 332 | try { |
||
| 333 | $mockParser->matchConcatenation(); |
||
| 334 | $this->fail('Should throw exception if no statement after logic.'); |
||
| 335 | } catch (UQLSyntaxError $e) { |
||
| 336 | // caught. Pass. |
||
| 337 | } |
||
| 338 | // Case 4: Mismatching logics in concatenation |
||
| 339 | |||
| 340 | $mockParser = $this->getMockBuilder('Netdudes\DataSourceryBundle\UQL\Parser') |
||
| 341 | ->setMethods(['matchStatement', 'matchLogic', 'getCurrentToken']) |
||
| 342 | ->getMock(); |
||
| 343 | $mockParser->expects($this->any()) |
||
| 344 | ->method('getCurrentToken') |
||
| 345 | ->will( |
||
| 346 | $this->returnValue( |
||
| 347 | [ |
||
| 348 | 'token' => 'T_INVALID', |
||
| 349 | 'match' => 'invalid', |
||
| 350 | ] |
||
| 351 | ) |
||
| 352 | ); |
||
| 353 | $mockParser->expects($this->any()) |
||
| 354 | ->method('matchStatement') |
||
| 355 | ->will($this->returnValue('MockValidStatement')); |
||
| 356 | $mockParser->expects($this->at(0)) |
||
| 357 | ->method('matchLogic') |
||
| 358 | ->will( |
||
| 359 | $this->returnValue( |
||
| 360 | [ |
||
| 361 | 'token' => 'T_LOGIC_AND', |
||
| 362 | ] |
||
| 363 | ) |
||
| 364 | ); |
||
| 365 | $mockParser->expects($this->at(1)) |
||
| 366 | ->method('matchLogic') |
||
| 367 | ->will( |
||
| 368 | $this->returnValue( |
||
| 369 | [ |
||
| 370 | 'token' => 'T_LOGIC_OR', |
||
| 371 | ] |
||
| 372 | ) |
||
| 373 | ); |
||
| 374 | |||
| 375 | try { |
||
| 376 | $mockParser->matchConcatenation(); |
||
| 377 | $this->fail('Should throw exception with mismatching logic in concatenation.'); |
||
| 378 | } catch (UQLSyntaxError $e) { |
||
| 379 | // caught. Pass. |
||
| 380 | } |
||
| 381 | |||
| 382 | // Case 5: Correct |
||
| 383 | |||
| 384 | $mockParser = $this->getMockBuilder('Netdudes\DataSourceryBundle\UQL\Parser') |
||
| 385 | ->setMethods(['matchStatement', 'matchLogic', 'getCurrentToken']) |
||
| 386 | ->getMock(); |
||
| 387 | $mockParser->expects($this->any()) |
||
| 388 | ->method('getCurrentToken') |
||
| 389 | ->will( |
||
| 390 | $this->returnValue( |
||
| 391 | [ |
||
| 392 | 'token' => 'T_INVALID', |
||
| 393 | 'match' => 'invalid', |
||
| 394 | ] |
||
| 395 | ) |
||
| 396 | ); |
||
| 397 | $mockParser->expects($this->at(0)) |
||
| 398 | ->method('matchStatement') |
||
| 399 | ->will($this->returnValue('MockValidStatement0')); |
||
| 400 | $mockParser->expects($this->at(2)) |
||
| 401 | ->method('matchStatement') |
||
| 402 | ->will($this->returnValue('MockValidStatement1')); |
||
| 403 | $mockParser->expects($this->at(1)) |
||
| 404 | ->method('matchLogic') |
||
| 405 | ->will( |
||
| 406 | $this->returnValue( |
||
| 407 | [ |
||
| 408 | 'token' => 'T_LOGIC_AND', |
||
| 409 | ] |
||
| 410 | ) |
||
| 411 | ); |
||
| 412 | $mockParser->expects($this->at(3)) |
||
| 413 | ->method('matchLogic') |
||
| 414 | ->will($this->returnValue(false)); |
||
| 415 | |||
| 416 | $result = $mockParser->matchConcatenation(); |
||
| 417 | $this->assertTrue($result instanceof ASTGroup); |
||
| 418 | $this->assertEquals( |
||
| 419 | [ |
||
| 420 | 'MockValidStatement0', |
||
| 421 | 'MockValidStatement1', |
||
| 422 | ], |
||
| 423 | $result->getElements() |
||
| 424 | ); |
||
| 425 | $this->assertEquals('T_LOGIC_AND', $result->getLogic()); |
||
| 426 | } |
||
| 427 | |||
| 452 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.