Conditions | 10 |
Paths | 28 |
Total Lines | 38 |
Code Lines | 28 |
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 |
||
44 | protected function parseDoc(Comment $comment, $text) { |
||
45 | $context = $this->getContext(); |
||
46 | try { |
||
47 | $block = new DocBlock($text, $context); |
||
48 | foreach ($block->getTags() AS $tag) { |
||
49 | switch ($tag->getName()) { |
||
50 | case "param": |
||
51 | $comment->addVar( |
||
52 | $this->createMethodParam($tag) |
||
53 | ); |
||
54 | break; |
||
55 | case "var": |
||
56 | $comment->addVar( |
||
57 | $this->createVar($tag) |
||
58 | ); |
||
59 | break; |
||
60 | case "return": |
||
61 | $comment->setReturn( |
||
62 | $this->getFQCN($tag->getType()) |
||
|
|||
63 | ); |
||
64 | break; |
||
65 | case "property": |
||
66 | case "property-read": |
||
67 | case "property-write": |
||
68 | $comment->addProperty( |
||
69 | $this->createProperty($tag) |
||
70 | ); |
||
71 | break; |
||
72 | case "inheritdoc": |
||
73 | $comment->markInheritDoc(); |
||
74 | break; |
||
75 | } |
||
76 | } |
||
77 | } |
||
78 | catch (\Exception $e) { |
||
79 | |||
80 | } |
||
81 | } |
||
82 | View Code Duplication | protected function createMethodParam(Tag $tag) { |
|
143 |
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 sub-classes 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 parent class: