Conditions | 20 |
Paths | 46 |
Total Lines | 84 |
Code Lines | 57 |
Lines | 18 |
Ratio | 21.43 % |
Tests | 58 |
CRAP Score | 20.2002 |
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 |
||
114 | 5 | private function examineSubset($line) |
|
115 | { |
||
116 | 5 | $subset = []; |
|
117 | 5 | $nextSrc = null; |
|
118 | 5 | $hasInitializer = false; |
|
119 | |||
120 | 5 | if (1 == $this->linecount && 0 === strpos(trim($line), "'")) { |
|
121 | 2 | $hasInitializer = true; |
|
122 | 2 | } |
|
123 | |||
124 | // subset |
||
125 | 5 | foreach (explode(self::SOURCE_TARGET_SEPARATOR, $line) as $definition) { |
|
126 | 5 | $resolved = []; |
|
127 | |||
128 | 5 | if (!$hasInitializer) { |
|
129 | 5 | $resolved = $this->examineDefinition($definition); |
|
130 | 5 | } |
|
131 | |||
132 | 5 | $hasInport = $this->hasValue($resolved, self::INPORT_LABEL); |
|
133 | 5 | $hasOutport = $this->hasValue($resolved, self::OUTPORT_LABEL); |
|
134 | |||
135 | //define states |
||
136 | 5 | switch (true) { |
|
137 | 5 | View Code Duplication | case !empty($step[self::DATA_LABEL]) && ($hasInport && $hasOutport): |
1 ignored issue
–
show
|
|||
138 | // initializer + inport |
||
139 | 1 | $nextSrc = $resolved; |
|
140 | 1 | $step[self::TARGET_LABEL] = $this->addPort($resolved, self::INPORT_LABEL); |
|
141 | // multi def oneliner initializer resolved |
||
142 | 1 | array_push($this->definition[self::INITIALIZERS_LABEL], $step); |
|
143 | 1 | $step = []; |
|
144 | 1 | break; |
|
145 | 5 | case !empty($nextSrc) && ($hasInport && $hasOutport): |
|
146 | // if there was an initializer, we get a full touple with this iteration |
||
147 | $step = [ |
||
148 | 1 | self::SOURCE_LABEL => $this->addPort($nextSrc, self::OUTPORT_LABEL), |
|
149 | 1 | self::TARGET_LABEL => $this->addPort($resolved, self::INPORT_LABEL), |
|
150 | 1 | ]; |
|
151 | 1 | $nextSrc = $resolved; |
|
152 | 1 | array_push($subset, $step); |
|
153 | 1 | $step = []; |
|
154 | 1 | break; |
|
155 | 5 | View Code Duplication | case $hasInport && $hasOutport: |
1 ignored issue
–
show
|
|||
156 | // tgt + multi def |
||
157 | 2 | $nextSrc = $resolved; |
|
158 | 2 | $step[self::TARGET_LABEL] = $this->addPort($resolved, self::INPORT_LABEL); |
|
159 | // check if we've already got the touple ready |
||
160 | 2 | if (!empty($step[self::SOURCE_LABEL])) { |
|
161 | 2 | array_push($subset, $step); |
|
162 | 2 | $step = []; |
|
163 | 2 | } |
|
164 | 2 | break; |
|
165 | 5 | case $hasInport && $nextSrc: |
|
166 | // use orevious OUT as src to fill touple |
||
167 | 2 | $step[self::SOURCE_LABEL] = $this->addPort($nextSrc, self::OUTPORT_LABEL); |
|
168 | 2 | $nextSrc = null; |
|
1 ignored issue
–
show
|
|||
169 | 5 | case $hasInport: |
|
170 | 5 | $step[self::TARGET_LABEL] = $this->addPort($resolved, self::INPORT_LABEL); |
|
171 | // resolved touple |
||
172 | 5 | if (empty($step[self::DATA_LABEL])) { |
|
173 | 5 | array_push($subset, $step); |
|
174 | 5 | } else { |
|
175 | 1 | array_push($this->definition[self::INITIALIZERS_LABEL], $step); |
|
176 | } |
||
177 | 5 | $nextSrc = null; |
|
178 | 5 | $step = []; |
|
179 | 5 | break; |
|
180 | 5 | case $hasOutport: |
|
181 | // simplest case OUT -> IN |
||
182 | 5 | $step[self::SOURCE_LABEL] = $this->addPort($resolved, self::OUTPORT_LABEL); |
|
183 | 5 | break; |
|
184 | 2 | case $hasInitializer: |
|
185 | // initialization value: at the moment we only support one |
||
186 | 2 | $step[self::DATA_LABEL] = trim($definition, " '"); |
|
187 | 2 | $hasInitializer = false; // reset |
|
188 | 2 | break; |
|
189 | default: |
||
190 | throw new ParserDefinitionException( |
||
191 | "Line ({$this->linecount}) {$line} does not contain in or out ports!" |
||
192 | ); |
||
193 | } |
||
194 | 5 | } |
|
195 | |||
196 | 5 | return $subset; |
|
197 | } |
||
198 | |||
279 |
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.