Conditions | 5 |
Paths | 6 |
Total Lines | 63 |
Code Lines | 27 |
Lines | 35 |
Ratio | 55.56 % |
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 |
||
110 | protected function generateCode(TypedListList $assertionLists, $functionName) |
||
111 | { |
||
112 | // We only use contracting if we're not inside another contract already |
||
113 | $code = '/* BEGIN OF PRECONDITION ENFORCEMENT */ |
||
114 | if (' . ReservedKeywords::CONTRACT_CONTEXT . ') { |
||
115 | ' . ReservedKeywords::PASSED_ASSERTION_FLAG . ' = false; |
||
116 | ' . ReservedKeywords::FAILURE_VARIABLE . ' = array(); |
||
117 | ' . ReservedKeywords::UNWRAPPED_FAILURE_VARIABLE . ' = array(); |
||
118 | '; |
||
119 | |||
120 | // We need a counter to check how much conditions we got |
||
121 | $conditionCounter = 0; |
||
122 | $listIterator = $assertionLists->getIterator(); |
||
123 | View Code Duplication | for ($i = 0; $i < $listIterator->count(); $i++) { |
|
124 | // Create the inner loop for the different assertions |
||
125 | $assertionIterator = $listIterator->current()->getIterator(); |
||
126 | |||
127 | // Only act if we got actual entries |
||
128 | if ($assertionIterator->count() === 0) { |
||
129 | // increment the outer loop |
||
130 | $listIterator->next(); |
||
131 | continue; |
||
132 | } |
||
133 | |||
134 | // create a wrap around assuring that inherited conditions get or-combined |
||
135 | $code .= ' |
||
136 | if (' . ReservedKeywords::PASSED_ASSERTION_FLAG . ' === false) { |
||
137 | '; |
||
138 | |||
139 | // iterate through the conditions for this certain instance |
||
140 | for ($j = 0; $j < $assertionIterator->count(); $j++) { |
||
141 | $conditionCounter++; |
||
142 | |||
143 | // Code to catch failed assertions |
||
144 | $code .= $assertionIterator->current()->toCode(); |
||
145 | $assertionIterator->next(); |
||
146 | } |
||
147 | |||
148 | // close the or-combined wrap |
||
149 | $code .= ' if (empty(' . ReservedKeywords::FAILURE_VARIABLE . ') && empty(' . ReservedKeywords::UNWRAPPED_FAILURE_VARIABLE . ')) { |
||
150 | ' . ReservedKeywords::PASSED_ASSERTION_FLAG . ' = true; |
||
151 | } |
||
152 | } |
||
153 | '; |
||
154 | |||
155 | // increment the outer loop |
||
156 | $listIterator->next(); |
||
157 | } |
||
158 | |||
159 | // Preconditions need or-ed conditions so we make sure only one condition list gets checked |
||
160 | $code .= 'if (' . ReservedKeywords::PASSED_ASSERTION_FLAG . ' === false){ |
||
161 | ' . Placeholders::ENFORCEMENT . $functionName . 'precondition' . Placeholders::PLACEHOLDER_CLOSE . ' |
||
162 | } |
||
163 | } |
||
164 | /* END OF PRECONDITION ENFORCEMENT */'; |
||
165 | |||
166 | // If there were no assertions we will just return a comment |
||
167 | if ($conditionCounter === 0) { |
||
168 | return '/* No preconditions for this function/method */'; |
||
169 | } |
||
170 | |||
171 | return $code; |
||
172 | } |
||
173 | } |
||
174 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.