| Conditions | 2 |
| Paths | 2 |
| Total Lines | 62 |
| Code Lines | 21 |
| 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 |
||
| 58 | private function convertToFontCode($barcode) |
||
| 59 | { |
||
| 60 | /** |
||
| 61 | * The barcode is split into the following sections: |
||
| 62 | * First digit |
||
| 63 | * First group of 6 digits (2nd-7th) |
||
| 64 | * Last group of 6 digits (8th-13th) |
||
| 65 | * e.g. 1234567890128 |
||
| 66 | * becomes 1 234567 890128 |
||
| 67 | * |
||
| 68 | * The font converts the first digit to a char between q-z |
||
| 69 | * |
||
| 70 | * The first group of digits are converted to chars starting at either A or Q, |
||
| 71 | * the first digit affects the format of the first group as follows: |
||
| 72 | * 1st digit First group format |
||
| 73 | * 0 AAAAAA |
||
| 74 | * 1 AAQAQQ |
||
| 75 | * 2 AAQQAQ |
||
| 76 | * 3 AAQQQA |
||
| 77 | * 4 AQAAQQ |
||
| 78 | * 5 AQQAAQ |
||
| 79 | * 6 AQQQAA |
||
| 80 | * 7 AQAQAQ |
||
| 81 | * 8 AQAQQA |
||
| 82 | * 9 AQQAQA |
||
| 83 | * |
||
| 84 | * The last group of digits remain unchanged |
||
| 85 | * |
||
| 86 | * The actual format used by the font is: |
||
| 87 | * firstDigit {{{{{| firstGroup {|{ secondGroup | |
||
| 88 | */ |
||
| 89 | |||
| 90 | // convert first digit |
||
| 91 | $firstDigit = (int)substr($barcode, 0, 1); |
||
| 92 | $firstDigitChar = chr(113 + $firstDigit); |
||
| 93 | |||
| 94 | // convert first group |
||
| 95 | $firstGroup = ''; |
||
| 96 | $firstGroupFormats = [ |
||
| 97 | 'AAAAAA', |
||
| 98 | 'AAQAQQ', |
||
| 99 | 'AAQQAQ', |
||
| 100 | 'AAQQQA', |
||
| 101 | 'AQAAQQ', |
||
| 102 | 'AQQAAQ', |
||
| 103 | 'AQQQAA', |
||
| 104 | 'AQAQAQ', |
||
| 105 | 'AQAQQA', |
||
| 106 | 'AQQAQA' |
||
| 107 | ]; |
||
| 108 | $firstGroupFormat = $firstGroupFormats[$firstDigit]; |
||
| 109 | for ($i = 1; $i <= 6; $i++) { |
||
| 110 | $digit = (int)substr($barcode, $i, 1); |
||
| 111 | $startingChar = substr($firstGroupFormat, $i - 1, 1); |
||
| 112 | $firstGroup .= chr(ord($startingChar) + $digit); |
||
| 113 | } |
||
| 114 | |||
| 115 | // extract second group |
||
| 116 | $secondGroup = substr($barcode, 7); |
||
| 117 | |||
| 118 | // combine the barcode with the special chars used by the font |
||
| 119 | return $firstDigitChar.'{{{{{|'.$firstGroup.'{|{'.$secondGroup.'|'; |
||
| 120 | } |
||
| 122 |