Conditions | 1 |
Paths | 1 |
Total Lines | 60 |
Code Lines | 47 |
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 |
||
21 | public function load(ObjectManager $manager) |
||
22 | { |
||
23 | NodeHelper::createPath($manager->getPhpcrSession(), '/test'); |
||
|
|||
24 | |||
25 | NodeHelper::createPath($manager->getPhpcrSession(), '/test/content'); |
||
26 | NodeHelper::createPath($manager->getPhpcrSession(), '/test/routes'); |
||
27 | |||
28 | $contentRoot = $manager->find(null, '/test/content'); |
||
29 | $routeRoot = $manager->find(null, '/test/routes'); |
||
30 | |||
31 | $singleRoute = new Content(); |
||
32 | $singleRoute->setName('route-1'); |
||
33 | $singleRoute->setTitle('Route 1'); |
||
34 | $singleRoute->setParentDocument($routeRoot); |
||
35 | $manager->persist($singleRoute); |
||
36 | |||
37 | $routeAlikeA = new Content(); |
||
38 | $routeAlikeA->setName('route-2'); |
||
39 | $routeAlikeA->setTitle('Route 2'); |
||
40 | $routeAlikeA->setParentDocument($routeRoot); |
||
41 | $manager->persist($routeAlikeA); |
||
42 | |||
43 | $routeAlikeB = new Content(); |
||
44 | $routeAlikeB->setName('route-3'); |
||
45 | $routeAlikeB->setTitle('Route 3'); |
||
46 | $routeAlikeB->setParentDocument($routeRoot); |
||
47 | $manager->persist($routeAlikeB); |
||
48 | |||
49 | $child = new Content(); |
||
50 | $child->setName('child'); |
||
51 | $child->setTitle('Content Child'); |
||
52 | |||
53 | $content = new Content(); |
||
54 | $content->setName('content-1'); |
||
55 | $content->setTitle('Content 1'); |
||
56 | $content->setSingleRoute($singleRoute); |
||
57 | $content->addRoute($routeAlikeA); |
||
58 | $content->addRoute($routeAlikeB); |
||
59 | $content->setChild($child); |
||
60 | $content->setParentDocument($contentRoot); |
||
61 | $manager->persist($content); |
||
62 | |||
63 | $childA = new Content(); |
||
64 | $childA->setName('content-3'); |
||
65 | $childA->setTitle('Content Child A'); |
||
66 | |||
67 | $childB = new Content(); |
||
68 | $childB->setName('content-3'); |
||
69 | $childB->setTitle('Content Child B'); |
||
70 | |||
71 | $content = new Content(); |
||
72 | $content->setName('content-2'); |
||
73 | $content->setParentDocument($contentRoot); |
||
74 | $content->addChild($childA); |
||
75 | $content->addChild($childB); |
||
76 | $content->setTitle('Content 2'); |
||
77 | $manager->persist($content); |
||
78 | |||
79 | $manager->flush(); |
||
80 | } |
||
81 | } |
||
82 |
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: