Conditions | 13 |
Paths | 26 |
Total Lines | 40 |
Code Lines | 27 |
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 |
||
64 | protected function createContext(Scope $scope, Token $token, $badLine, Index $index) |
||
65 | { |
||
66 | $context = new Context($scope, $token); |
||
67 | $nodes = $this->parser->parse($this->prepareLine($badLine)); |
||
68 | |||
69 | if ($token->isObjectOperator() || $token->isStaticOperator() || $token->isMethodCall()) { |
||
70 | if (is_array($nodes)) { |
||
71 | $workingNode = array_pop($nodes); |
||
72 | } else { |
||
73 | $workingNode = $nodes; |
||
74 | } |
||
75 | $isThis = false; |
||
76 | if ($workingNode instanceof Variable && $workingNode->name === 'this') { |
||
77 | $isThis = true; |
||
78 | } |
||
79 | if ($workingNode instanceof Name) { |
||
80 | $nodeFQCN = $this->useParser->getFQCN($workingNode); |
||
81 | if ($scope->getFQCN() instanceof FQCN |
||
|
|||
82 | && $nodeFQCN->toString() === $scope->getFQCN()->toString() |
||
83 | ) { |
||
84 | $isThis = true; |
||
85 | } |
||
86 | } |
||
87 | $types = $this->typeResolver->getChainType($workingNode, $index, $scope); |
||
88 | $context->setData([ |
||
89 | array_pop($types), |
||
90 | $isThis, |
||
91 | $types, |
||
92 | $workingNode |
||
93 | ]); |
||
94 | } |
||
95 | if ($token->isUseOperator() |
||
96 | || $token->isNamespaceOperator() |
||
97 | || $token->isNewOperator() |
||
98 | ) { |
||
99 | $context->setData(trim($token->getSymbol())); |
||
100 | } |
||
101 | |||
102 | return $context; |
||
103 | } |
||
104 | |||
140 |
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: