| Conditions | 1 | 
| Paths | 1 | 
| Total Lines | 60 | 
| 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 | ||
| 23 | public function load(ObjectManager $manager): void | ||
| 24 |     { | ||
| 25 | NodeHelper::createPath($manager->getPhpcrSession(), '/test'); | ||
|  | |||
| 26 | |||
| 27 | NodeHelper::createPath($manager->getPhpcrSession(), '/test/content'); | ||
| 28 | NodeHelper::createPath($manager->getPhpcrSession(), '/test/routes'); | ||
| 29 | |||
| 30 | $contentRoot = $manager->find(null, '/test/content'); | ||
| 31 | $routeRoot = $manager->find(null, '/test/routes'); | ||
| 32 | |||
| 33 | $singleRoute = new Content(); | ||
| 34 |         $singleRoute->setName('route-1'); | ||
| 35 |         $singleRoute->setTitle('Route 1'); | ||
| 36 | $singleRoute->setParentDocument($routeRoot); | ||
| 37 | $manager->persist($singleRoute); | ||
| 38 | |||
| 39 | $routeAlikeA = new Content(); | ||
| 40 |         $routeAlikeA->setName('route-2'); | ||
| 41 |         $routeAlikeA->setTitle('Route 2'); | ||
| 42 | $routeAlikeA->setParentDocument($routeRoot); | ||
| 43 | $manager->persist($routeAlikeA); | ||
| 44 | |||
| 45 | $routeAlikeB = new Content(); | ||
| 46 |         $routeAlikeB->setName('route-3'); | ||
| 47 |         $routeAlikeB->setTitle('Route 3'); | ||
| 48 | $routeAlikeB->setParentDocument($routeRoot); | ||
| 49 | $manager->persist($routeAlikeB); | ||
| 50 | |||
| 51 | $child = new Content(); | ||
| 52 |         $child->setName('child'); | ||
| 53 |         $child->setTitle('Content Child'); | ||
| 54 | |||
| 55 | $content = new Content(); | ||
| 56 |         $content->setName('content-1'); | ||
| 57 |         $content->setTitle('Content 1'); | ||
| 58 | $content->setSingleRoute($singleRoute); | ||
| 59 | $content->addRoute($routeAlikeA); | ||
| 60 | $content->addRoute($routeAlikeB); | ||
| 61 | $content->setChild($child); | ||
| 62 | $content->setParentDocument($contentRoot); | ||
| 63 | $manager->persist($content); | ||
| 64 | |||
| 65 | $childA = new Content(); | ||
| 66 |         $childA->setName('content-3'); | ||
| 67 |         $childA->setTitle('Content Child A'); | ||
| 68 | |||
| 69 | $childB = new Content(); | ||
| 70 |         $childB->setName('content-3'); | ||
| 71 |         $childB->setTitle('Content Child B'); | ||
| 72 | |||
| 73 | $content = new Content(); | ||
| 74 |         $content->setName('content-2'); | ||
| 75 | $content->setParentDocument($contentRoot); | ||
| 76 | $content->addChild($childA); | ||
| 77 | $content->addChild($childB); | ||
| 78 |         $content->setTitle('Content 2'); | ||
| 79 | $manager->persist($content); | ||
| 80 | |||
| 81 | $manager->flush(); | ||
| 82 | } | ||
| 83 | } | ||
| 84 | 
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: