| Conditions | 20 |
| Paths | 120 |
| Total Lines | 106 |
| Code Lines | 59 |
| 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 |
||
| 59 | public function extractData() |
||
| 60 | { |
||
| 61 | $analysis = []; |
||
| 62 | |||
| 63 | // First pass, we categorize each line |
||
| 64 | foreach ($this->parsedFile as $lineNb => $line) { |
||
| 65 | if (Utils::stringStart('#', $line)) { |
||
| 66 | $analysis[$lineNb] = [ |
||
| 67 | 'comment', |
||
| 68 | trim(substr($line, 1)) |
||
| 69 | ]; |
||
| 70 | |||
| 71 | continue; |
||
| 72 | } |
||
| 73 | |||
| 74 | // Property name, check for escaped equal sign |
||
| 75 | if (substr_count($line, '=') > substr_count($line, '\=')) { |
||
| 76 | $temp = explode('=', $line, 2); |
||
| 77 | $temp = Utils::trimArrayElements($temp); |
||
| 78 | |||
| 79 | if (count($temp) === 2) { |
||
| 80 | $temp[1] = Utils::removeQuotes($temp[1]); |
||
| 81 | |||
| 82 | $analysis[$lineNb] = [ |
||
| 83 | 'property', |
||
| 84 | $temp[0], |
||
| 85 | $temp[1] |
||
| 86 | ]; |
||
| 87 | } |
||
| 88 | |||
| 89 | unset($temp); |
||
| 90 | |||
| 91 | continue; |
||
| 92 | } else { |
||
| 93 | break; |
||
| 94 | } |
||
| 95 | |||
| 96 | // Multiline data |
||
| 97 | if (substr_count($line, '=') === 0) { |
||
| 98 | $analysis[$lineNb] = [ |
||
| 99 | 'multiline', |
||
| 100 | '', |
||
| 101 | $line |
||
| 102 | ]; |
||
| 103 | |||
| 104 | continue; |
||
| 105 | } |
||
| 106 | } |
||
| 107 | |||
| 108 | // Second pass, we associate comments to entities |
||
| 109 | $counter = Utils::getNumberLinesMatching('comment', $analysis); |
||
| 110 | |||
| 111 | while ($counter > 0) { |
||
| 112 | foreach ($analysis as $lineNb => $line) { |
||
| 113 | if ($line[0] === 'comment' && |
||
| 114 | isset($analysis[$lineNb + 1][0]) && |
||
| 115 | $analysis[$lineNb + 1][0] === 'comment') { |
||
| 116 | $analysis[$lineNb][1] .= ' ' . $analysis[$lineNb + 1][1]; |
||
| 117 | $analysis[$lineNb + 1][0] = 'erase'; |
||
| 118 | |||
| 119 | break; |
||
| 120 | } elseif ($line[0] === 'comment' && |
||
| 121 | isset($analysis[$lineNb + 1][0]) && |
||
| 122 | $analysis[$lineNb + 1][0] === 'property') { |
||
| 123 | $analysis[$lineNb + 1][3] = $line[1]; |
||
| 124 | $analysis[$lineNb][0] = 'erase'; |
||
| 125 | } |
||
| 126 | } |
||
| 127 | |||
| 128 | $counter = Utils::getNumberLinesMatching('comment', $analysis); |
||
| 129 | $analysis = $this->deleteFields('erase', $analysis); |
||
| 130 | } |
||
| 131 | |||
| 132 | // Third pass, we merge multiline strings |
||
| 133 | |||
| 134 | // We remove the backslashes at end of strings if they exist |
||
| 135 | $analysis = Utils::stripBackslashes($analysis); |
||
| 136 | |||
| 137 | // Count # of multilines |
||
| 138 | $counter = Utils::getNumberLinesMatching('multiline', $analysis); |
||
| 139 | |||
| 140 | while ($counter > 0) { |
||
| 141 | foreach ($analysis as $lineNb => $line) { |
||
| 142 | if ($line[0] === 'multiline' |
||
| 143 | && isset($analysis[$lineNb - 1][0]) |
||
| 144 | && $analysis[$lineNb - 1][0] === 'property') { |
||
| 145 | $analysis[$lineNb - 1][2] .= ' ' . trim($line[2]); |
||
| 146 | $analysis[$lineNb][0] = 'erase'; |
||
| 147 | break; |
||
| 148 | } |
||
| 149 | } |
||
| 150 | |||
| 151 | $counter = Utils::getNumberLinesMatching('multiline', $analysis); |
||
| 152 | $analysis = $this->deleteFields('erase', $analysis); |
||
| 153 | } |
||
| 154 | |||
| 155 | // Step 4, we clean up strings from escaped characters in properties |
||
| 156 | $analysis = $this->unescapeProperties($analysis); |
||
| 157 | |||
| 158 | // Step 5, we only have properties now, remove redondant field 0 |
||
| 159 | foreach ($analysis as $key => $value) { |
||
| 160 | array_splice($analysis[$key], 0, 1); |
||
| 161 | } |
||
| 162 | |||
| 163 | return $analysis; |
||
| 164 | } |
||
| 165 | |||
| 253 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.