| Total Complexity | 45 |
| Total Lines | 250 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like AlignmentPatternFinder often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AlignmentPatternFinder, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | final class AlignmentPatternFinder{ |
||
| 32 | |||
| 33 | private BitMatrix $matrix; |
||
| 34 | private float $moduleSize; |
||
| 35 | /** @var \chillerlan\QRCode\Detector\AlignmentPattern[] */ |
||
| 36 | private array $possibleCenters; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Creates a finder that will look in a portion of the whole image. |
||
| 40 | * |
||
| 41 | * @param \chillerlan\QRCode\Decoder\BitMatrix $matrix image to search |
||
| 42 | * @param float $moduleSize estimated module size so far |
||
| 43 | */ |
||
| 44 | public function __construct(BitMatrix $matrix, float $moduleSize){ |
||
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * This method attempts to find the bottom-right alignment pattern in the image. It is a bit messy since |
||
| 52 | * it's pretty performance-critical and so is written to be fast foremost. |
||
| 53 | * |
||
| 54 | * @param int $startX left column from which to start searching |
||
| 55 | * @param int $startY top row from which to start searching |
||
| 56 | * @param int $width width of region to search |
||
| 57 | * @param int $height height of region to search |
||
| 58 | * |
||
| 59 | * @return \chillerlan\QRCode\Detector\AlignmentPattern|null |
||
| 60 | */ |
||
| 61 | public function find(int $startX, int $startY, int $width, int $height):?AlignmentPattern{ |
||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @param int[] $stateCount count of black/white/black pixels just read |
||
| 148 | * |
||
| 149 | * @return bool true if the proportions of the counts is close enough to the 1/1/1 ratios |
||
| 150 | * used by alignment patterns to be considered a match |
||
| 151 | */ |
||
| 152 | private function foundPatternCross(array $stateCount):bool{ |
||
| 153 | $maxVariance = ($this->moduleSize / 2.0); |
||
| 154 | |||
| 155 | for($i = 0; $i < 3; $i++){ |
||
| 156 | if(abs($this->moduleSize - $stateCount[$i]) >= $maxVariance){ |
||
| 157 | return false; |
||
| 158 | } |
||
| 159 | } |
||
| 160 | |||
| 161 | return true; |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * This is called when a horizontal scan finds a possible alignment pattern. It will |
||
| 166 | * cross-check with a vertical scan, and if successful, will see if this pattern had been |
||
| 167 | * found on a previous horizontal scan. If so, we consider it confirmed and conclude we have |
||
| 168 | * found the alignment pattern. |
||
| 169 | * |
||
| 170 | * @param int[] $stateCount reading state module counts from horizontal scan |
||
| 171 | * @param int $i row where alignment pattern may be found |
||
| 172 | * @param int $j end of possible alignment pattern in row |
||
| 173 | * |
||
| 174 | * @return \chillerlan\QRCode\Detector\AlignmentPattern|null if we have found the same pattern twice, or null if not |
||
| 175 | */ |
||
| 176 | private function handlePossibleCenter(array $stateCount, int $i, int $j):?AlignmentPattern{ |
||
| 177 | $stateCountTotal = ($stateCount[0] + $stateCount[1] + $stateCount[2]); |
||
| 178 | $centerJ = $this->centerFromEnd($stateCount, $j); |
||
| 179 | $centerI = $this->crossCheckVertical($i, (int)$centerJ, (2 * $stateCount[1]), $stateCountTotal); |
||
| 180 | |||
| 181 | if($centerI !== null){ |
||
| 182 | $estimatedModuleSize = (($stateCount[0] + $stateCount[1] + $stateCount[2]) / 3.0); |
||
| 183 | |||
| 184 | foreach($this->possibleCenters as $center){ |
||
| 185 | // Look for about the same center and module size: |
||
| 186 | if($center->aboutEquals($estimatedModuleSize, $centerI, $centerJ)){ |
||
| 187 | return $center->combineEstimate($centerI, $centerJ, $estimatedModuleSize); |
||
| 188 | } |
||
| 189 | } |
||
| 190 | |||
| 191 | // Hadn't found this before; save it |
||
| 192 | $point = new AlignmentPattern($centerJ, $centerI, $estimatedModuleSize); |
||
| 193 | $this->possibleCenters[] = $point; |
||
| 194 | } |
||
| 195 | |||
| 196 | return null; |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Given a count of black/white/black pixels just seen and an end position, |
||
| 201 | * figures the location of the center of this black/white/black run. |
||
| 202 | * |
||
| 203 | * @param int[] $stateCount |
||
| 204 | * @param int $end |
||
| 205 | * |
||
| 206 | * @return float |
||
| 207 | */ |
||
| 208 | private function centerFromEnd(array $stateCount, int $end):float{ |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * After a horizontal scan finds a potential alignment pattern, this method |
||
| 214 | * "cross-checks" by scanning down vertically through the center of the possible |
||
| 215 | * alignment pattern to see if the same proportion is detected. |
||
| 216 | * |
||
| 217 | * @param int $startI row where an alignment pattern was detected |
||
| 218 | * @param int $centerJ center of the section that appears to cross an alignment pattern |
||
| 219 | * @param int $maxCount maximum reasonable number of modules that should be |
||
| 220 | * observed in any reading state, based on the results of the horizontal scan |
||
| 221 | * @param int $originalStateCountTotal |
||
| 222 | * |
||
| 223 | * @return float|null vertical center of alignment pattern, or null if not found |
||
| 224 | */ |
||
| 225 | private function crossCheckVertical(int $startI, int $centerJ, int $maxCount, int $originalStateCountTotal):?float{ |
||
| 281 | } |
||
| 282 | |||
| 284 |