| Conditions | 1 |
| Paths | 1 |
| Total Lines | 167 |
| Code Lines | 132 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 testFindOneBy() |
||
| 269 | { |
||
| 270 | $this->classMetadata |
||
| 271 | ->expects($this->once()) |
||
| 272 | ->method('getFieldNames') |
||
| 273 | ->will($this->returnValue([ |
||
| 274 | $translatableField = 'translatable_field', |
||
| 275 | $bothField = 'both_field', |
||
| 276 | ])); |
||
| 277 | |||
| 278 | $this->classMetadata |
||
| 279 | ->expects($this->once()) |
||
| 280 | ->method('getAssociationMapping') |
||
| 281 | ->with($this->identicalTo('translations')) |
||
| 282 | ->will($this->returnValue(['targetEntity' => $translationClass = TranslationTest::class])); |
||
| 283 | |||
| 284 | $translationClassMetadata = $this->createClassMetadataMock(); |
||
| 285 | $translationClassMetadata |
||
| 286 | ->expects($this->once()) |
||
| 287 | ->method('getFieldNames') |
||
| 288 | ->will($this->returnValue([ |
||
| 289 | $translationField = 'translation_field', |
||
| 290 | $bothField, |
||
| 291 | ])); |
||
| 292 | |||
| 293 | $this->entityManager |
||
| 294 | ->expects($this->once()) |
||
| 295 | ->method('getClassMetadata') |
||
| 296 | ->with($this->identicalTo($translationClass)) |
||
| 297 | ->will($this->returnValue($translationClassMetadata)); |
||
| 298 | |||
| 299 | $this->resource |
||
| 300 | ->expects($this->once()) |
||
| 301 | ->method('getName') |
||
| 302 | ->will($this->returnValue($name = 'resource')); |
||
| 303 | |||
| 304 | $this->entityManager |
||
| 305 | ->expects($this->once()) |
||
| 306 | ->method('createQueryBuilder') |
||
| 307 | ->will($this->returnValue($queryBuilder = $this->createQueryBuilderMock())); |
||
| 308 | |||
| 309 | $queryBuilder |
||
| 310 | ->expects($this->once()) |
||
| 311 | ->method('select') |
||
| 312 | ->with($this->identicalTo($name)) |
||
| 313 | ->will($this->returnSelf()); |
||
| 314 | |||
| 315 | $queryBuilder |
||
| 316 | ->expects($this->once()) |
||
| 317 | ->method('from') |
||
| 318 | ->with( |
||
| 319 | $this->identicalTo($this->class), |
||
| 320 | $this->identicalTo($name), |
||
| 321 | $this->isNull() |
||
| 322 | ) |
||
| 323 | ->will($this->returnSelf()); |
||
| 324 | |||
| 325 | $queryBuilder |
||
| 326 | ->expects($this->once()) |
||
| 327 | ->method('addSelect') |
||
| 328 | ->with($this->identicalTo('resource_translation')) |
||
| 329 | ->will($this->returnSelf()); |
||
| 330 | |||
| 331 | $queryBuilder |
||
| 332 | ->expects($this->exactly(5)) |
||
| 333 | ->method('getRootAliases') |
||
| 334 | ->will($this->returnValue([$name])); |
||
| 335 | |||
| 336 | $queryBuilder |
||
| 337 | ->expects($this->once()) |
||
| 338 | ->method('leftJoin') |
||
| 339 | ->with( |
||
| 340 | $this->identicalTo($name.'.translations'), |
||
| 341 | $this->identicalTo($translation = 'resource_translation'), |
||
| 342 | $this->isNull(), |
||
| 343 | $this->isNull(), |
||
| 344 | $this->isNull() |
||
| 345 | ) |
||
| 346 | ->will($this->returnSelf()); |
||
| 347 | |||
| 348 | $queryBuilder |
||
| 349 | ->expects($this->exactly(3)) |
||
| 350 | ->method('expr') |
||
| 351 | ->will($this->returnValue($expr = $this->createExprMock())); |
||
| 352 | |||
| 353 | $expr |
||
| 354 | ->expects($this->at(0)) |
||
| 355 | ->method('eq') |
||
| 356 | ->with( |
||
| 357 | $this->identicalTo($name.'.'.$translatableField), |
||
| 358 | $this->matchesRegularExpression('/:'.$name.'_'.$translatableField.'_[a-z0-9]{22}/') |
||
| 359 | ) |
||
| 360 | ->will($this->returnValue($translatableWhere = 'translatable_where')); |
||
| 361 | |||
| 362 | $expr |
||
| 363 | ->expects($this->at(1)) |
||
| 364 | ->method('eq') |
||
| 365 | ->with( |
||
| 366 | $this->identicalTo($translation.'.'.$translationField), |
||
| 367 | $this->matchesRegularExpression('/:'.$translation.'_'.$translationField.'_[a-z0-9]{22}/') |
||
| 368 | ) |
||
| 369 | ->will($this->returnValue($translationWhere = 'translation_where')); |
||
| 370 | |||
| 371 | $expr |
||
| 372 | ->expects($this->at(2)) |
||
| 373 | ->method('eq') |
||
| 374 | ->with( |
||
| 375 | $this->identicalTo($name.'.'.$bothField), |
||
| 376 | $this->matchesRegularExpression('/:'.$name.'_'.$bothField.'_[a-z0-9]{22}/') |
||
| 377 | ) |
||
| 378 | ->will($this->returnValue($bothWhere = 'both_where')); |
||
| 379 | |||
| 380 | $queryBuilder |
||
| 381 | ->expects($this->exactly(3)) |
||
| 382 | ->method('andWhere') |
||
| 383 | ->will($this->returnValueMap([ |
||
| 384 | [$translatableWhere, $queryBuilder], |
||
| 385 | [$translationWhere, $queryBuilder], |
||
| 386 | [$bothWhere, $queryBuilder], |
||
| 387 | ])); |
||
| 388 | |||
| 389 | $queryBuilder |
||
| 390 | ->expects($this->at(9)) |
||
| 391 | ->method('setParameter') |
||
| 392 | ->with( |
||
| 393 | $this->matchesRegularExpression('/'.$name.'_'.$translatableField.'_[a-z0-9]{22}/'), |
||
| 394 | $this->identicalTo($translatableValue = 'translatable_value'), |
||
| 395 | $this->isNull() |
||
| 396 | ) |
||
| 397 | ->will($this->returnSelf()); |
||
| 398 | |||
| 399 | $queryBuilder |
||
| 400 | ->expects($this->at(13)) |
||
| 401 | ->method('setParameter') |
||
| 402 | ->with( |
||
| 403 | $this->matchesRegularExpression('/'.$translation.'_'.$translationField.'_[a-z0-9]{22}/'), |
||
| 404 | $this->identicalTo($translationValue = 'translation_value'), |
||
| 405 | $this->isNull() |
||
| 406 | ) |
||
| 407 | ->will($this->returnSelf()); |
||
| 408 | |||
| 409 | $queryBuilder |
||
| 410 | ->expects($this->at(17)) |
||
| 411 | ->method('setParameter') |
||
| 412 | ->with( |
||
| 413 | $this->matchesRegularExpression('/'.$name.'_'.$bothField.'_[a-z0-9]{22}/'), |
||
| 414 | $this->identicalTo($bothValue = 'both_value'), |
||
| 415 | $this->isNull() |
||
| 416 | ) |
||
| 417 | ->will($this->returnSelf()); |
||
| 418 | |||
| 419 | $queryBuilder |
||
| 420 | ->expects($this->once()) |
||
| 421 | ->method('getQuery') |
||
| 422 | ->will($this->returnValue($query = $this->createQueryMock())); |
||
| 423 | |||
| 424 | $query |
||
| 425 | ->expects($this->once()) |
||
| 426 | ->method('getOneOrNullResult') |
||
| 427 | ->will($this->returnValue($result = 'result')); |
||
| 428 | |||
| 429 | $this->assertSame($result, $this->translatableRepository->findOneBy([ |
||
| 430 | $translatableField => $translatableValue, |
||
| 431 | $translationField => $translationValue, |
||
| 432 | $bothField => $bothValue, |
||
| 433 | ])); |
||
| 434 | } |
||
| 435 | |||
| 510 |
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: