Conditions | 15 |
Paths | 6 |
Total Lines | 91 |
Code Lines | 65 |
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 |
||
93 | protected function getNodeRecursive(array &$splitCondition) |
||
94 | { |
||
95 | $node = null; |
||
96 | $leftNode = null; |
||
97 | $operator = null; |
||
98 | $expression = $splitCondition; |
||
99 | $lastOr = null; |
||
100 | |||
101 | while (false === empty($expression)) { |
||
102 | if ($this->result->hasErrors()) { |
||
103 | break; |
||
104 | } |
||
105 | |||
106 | switch ($expression[0]) { |
||
107 | case ')': |
||
108 | $this->result->addError(new Error('Parenthesis closes invalid group.', |
||
109 | 1457969163)); |
||
110 | break; |
||
111 | case '(': |
||
112 | $groupNode = $this->getGroupNode($expression); |
||
113 | $expression = array_slice($expression, |
||
114 | count($groupNode) + 2); |
||
115 | $node = $this->getNodeRecursive($groupNode); |
||
116 | break; |
||
117 | case ConditionParser::LOGICAL_OR: |
||
118 | case ConditionParser::LOGICAL_AND: |
||
119 | if (null === $node) { |
||
120 | $this->result->addError(new Error('Logical operator must be preceded by a valid operation.', |
||
121 | 1457544986)); |
||
122 | } else { |
||
123 | $operator = $expression[0]; |
||
124 | |||
125 | if (ConditionParser::LOGICAL_OR === $operator) { |
||
126 | if (null !== $lastOr) { |
||
127 | $node = $this->getNode( |
||
128 | BooleanNode::class, |
||
129 | [$lastOr, $node, $operator] |
||
130 | ); |
||
131 | } |
||
132 | $lastOr = $node; |
||
133 | } else { |
||
134 | $leftNode = $node; |
||
135 | $node = null; |
||
136 | } |
||
137 | |||
138 | array_shift($expression); |
||
139 | } |
||
140 | break; |
||
141 | default: |
||
142 | $conditionName = $expression[0]; |
||
143 | if (false === $this->condition->hasItem($conditionName)) { |
||
144 | $this->result->addError(new Error('The condition "' . $conditionName . '" does not exist.', |
||
145 | 1457628378)); |
||
146 | } else { |
||
147 | $node = $this->getNode( |
||
148 | ConditionNode::class, |
||
149 | [ |
||
150 | $conditionName, |
||
151 | $this->condition->getItem($conditionName) |
||
152 | ] |
||
153 | ); |
||
154 | array_shift($expression); |
||
155 | } |
||
156 | break; |
||
157 | } |
||
158 | |||
159 | if (null !== $leftNode |
||
160 | && null !== $node |
||
161 | ) { |
||
162 | $node = $this->getNode( |
||
163 | BooleanNode::class, |
||
164 | [$leftNode, $node, $operator] |
||
165 | ); |
||
166 | |||
167 | $leftNode = null; |
||
168 | $operator = null; |
||
169 | } |
||
170 | } |
||
171 | |||
172 | if (null !== $leftNode) { |
||
173 | $this->result->addError(new Error('Logical operator must be followed by a valid operation.', |
||
174 | 1457545071)); |
||
175 | } elseif (null !== $lastOr) { |
||
176 | $node = $this->getNode( |
||
177 | BooleanNode::class, |
||
178 | [$lastOr, $node, ConditionParser::LOGICAL_OR] |
||
179 | ); |
||
180 | } |
||
181 | |||
182 | return $node; |
||
183 | } |
||
184 | |||
255 |