Conditions | 16 |
Paths | 3 |
Total Lines | 59 |
Code Lines | 46 |
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 |
||
87 | public function getChainType($node, Index $index, Scope $scope) |
||
88 | { |
||
89 | /** @var FQCN */ |
||
90 | $type = null; |
||
91 | $types = []; |
||
92 | $chain = $this->createChain($node); |
||
93 | $block = $chain; |
||
94 | while ($block instanceof Chain) { |
||
95 | $this->logger->debug('looking for type of ' . $block->getName()); |
||
96 | $event = new TypeResolveEvent($block, $type); |
||
97 | $this->dispatcher->dispatch(self::BLOCK_START, $event); |
||
98 | if ($block->getType() === 'var') { |
||
99 | $type = $this->getVarType($block->getName(), $scope); |
||
100 | } elseif ($block->getType() === 'method') { |
||
101 | if (!($type instanceof FQN)) { |
||
102 | $types[] = null; |
||
103 | break; |
||
104 | } |
||
105 | $type = $this->getMethodType($block->getName(), $type, $index); |
||
106 | } elseif ($block->getType() === 'property') { |
||
107 | if (!($type instanceof FQN)) { |
||
108 | $types[] = null; |
||
109 | break; |
||
110 | } |
||
111 | $type = $this->getPropertyType($block->getName(), $type, $index); |
||
112 | } elseif ($block->getType() === 'class') { |
||
113 | $type = $block->getName(); |
||
114 | if ($type instanceof FQCN) { |
||
115 | if ($type instanceof FQCN && ( |
||
116 | $type->getClassName() === 'self' |
||
117 | || $type->getClassName() === 'static' |
||
118 | ) |
||
119 | ) { |
||
120 | $type = $scope->getFQCN(); |
||
|
|||
121 | } elseif ($type->getClassName() === 'parent' |
||
122 | && $scope->getFQCN() instanceof FQCN |
||
123 | ) { |
||
124 | $type = $this->getParentType($scope->getFQCN(), $index); |
||
125 | } |
||
126 | } |
||
127 | } elseif ($block->getType() === 'function') { |
||
128 | $name = $block->getName(); |
||
129 | $function = $index->findFunctionByName($name->toString()); |
||
130 | if (empty($function)) { |
||
131 | $type = null; |
||
132 | } else { |
||
133 | $type = $function->return; |
||
134 | } |
||
135 | } |
||
136 | $event = new TypeResolveEvent($block, $type); |
||
137 | $this->dispatcher->dispatch(self::BLOCK_END, $event); |
||
138 | $type = $event->getType(); |
||
139 | $block = $block->getChild(); |
||
140 | $types[] = $type; |
||
141 | } |
||
142 | $event = new TypeResolveEvent($chain, $type); |
||
143 | $this->dispatcher->dispatch(self::TYPE_RESOLVED, $event); |
||
144 | return $types; |
||
145 | } |
||
146 | |||
250 |
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: