| Total Complexity | 44 |
| Total Lines | 303 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like BCGLabel 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 BCGLabel, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class BCGLabel { |
||
| 14 | const POSITION_TOP = 0; |
||
| 15 | const POSITION_RIGHT = 1; |
||
| 16 | const POSITION_BOTTOM = 2; |
||
| 17 | const POSITION_LEFT = 3; |
||
| 18 | |||
| 19 | const ALIGN_LEFT = 0; |
||
| 20 | const ALIGN_TOP = 0; |
||
| 21 | const ALIGN_CENTER = 1; |
||
| 22 | const ALIGN_RIGHT = 2; |
||
| 23 | const ALIGN_BOTTOM = 2; |
||
| 24 | |||
| 25 | private $font; |
||
| 26 | private $text; |
||
| 27 | private $position; |
||
| 28 | private $alignment; |
||
| 29 | private $offset; |
||
| 30 | private $spacing; |
||
| 31 | private $rotationAngle; |
||
| 32 | private $backgroundColor; |
||
| 33 | private $foregroundColor; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Constructor. |
||
| 37 | * |
||
| 38 | * @param string $text |
||
| 39 | * @param BCGFont $font |
||
| 40 | * @param int $position |
||
| 41 | * @param int $alignment |
||
| 42 | */ |
||
| 43 | public function __construct($text = '', $font = null, $position = self::POSITION_BOTTOM, $alignment = self::ALIGN_CENTER) { |
||
| 44 | $font = $font === null ? new BCGFontPhp(5) : $font; |
||
| 45 | $this->setFont($font); |
||
| 46 | $this->setText($text); |
||
| 47 | $this->setPosition($position); |
||
| 48 | $this->setAlignment($alignment); |
||
| 49 | $this->setSpacing(4); |
||
| 50 | $this->setOffset(0); |
||
| 51 | $this->setRotationAngle(0); |
||
| 52 | $this->setBackgroundColor(new BCGColor('white')); |
||
| 53 | $this->setForegroundColor(new BCGColor('black')); |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Gets the text. |
||
| 58 | * |
||
| 59 | * @return string |
||
| 60 | */ |
||
| 61 | public function getText() { |
||
| 62 | return $this->font->getText(); |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Sets the text. |
||
| 67 | * |
||
| 68 | * @param string $text |
||
| 69 | */ |
||
| 70 | public function setText($text) { |
||
| 71 | $this->text = $text; |
||
| 72 | $this->font->setText($this->text); |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Gets the font. |
||
| 77 | * |
||
| 78 | * @return BCGFont |
||
| 79 | */ |
||
| 80 | public function getFont() { |
||
| 81 | return $this->font; |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Sets the font. |
||
| 86 | * |
||
| 87 | * @param BCGFont $font |
||
| 88 | */ |
||
| 89 | public function setFont($font) { |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Gets the text position for drawing. |
||
| 103 | * |
||
| 104 | * @return int |
||
| 105 | */ |
||
| 106 | public function getPosition() { |
||
| 107 | return $this->position; |
||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Sets the text position for drawing. |
||
| 112 | * |
||
| 113 | * @param int $position |
||
| 114 | */ |
||
| 115 | public function setPosition($position) { |
||
| 116 | $position = intval($position); |
||
| 117 | if ($position !== self::POSITION_TOP && $position !== self::POSITION_RIGHT && $position !== self::POSITION_BOTTOM && $position !== self::POSITION_LEFT) { |
||
| 118 | throw new BCGArgumentException('The text position must be one of a valid constant.', 'position'); |
||
| 119 | } |
||
| 120 | |||
| 121 | $this->position = $position; |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Gets the text alignment for drawing. |
||
| 126 | * |
||
| 127 | * @return int |
||
| 128 | */ |
||
| 129 | public function getAlignment() { |
||
| 130 | return $this->alignment; |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Sets the text alignment for drawing. |
||
| 135 | * |
||
| 136 | * @param int $alignment |
||
| 137 | */ |
||
| 138 | public function setAlignment($alignment) { |
||
| 139 | $alignment = intval($alignment); |
||
| 140 | if ($alignment !== self::ALIGN_LEFT && $alignment !== self::ALIGN_TOP && $alignment !== self::ALIGN_CENTER && $alignment !== self::ALIGN_RIGHT && $alignment !== self::ALIGN_BOTTOM) { |
||
| 141 | throw new BCGArgumentException('The text alignment must be one of a valid constant.', 'alignment'); |
||
| 142 | } |
||
| 143 | |||
| 144 | $this->alignment = $alignment; |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Gets the offset. |
||
| 149 | * |
||
| 150 | * @return int |
||
| 151 | */ |
||
| 152 | public function getOffset() { |
||
| 153 | return $this->offset; |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Sets the offset. |
||
| 158 | * |
||
| 159 | * @param int $offset |
||
| 160 | */ |
||
| 161 | public function setOffset($offset) { |
||
| 162 | $this->offset = intval($offset); |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Gets the spacing. |
||
| 167 | * |
||
| 168 | * @return int |
||
| 169 | */ |
||
| 170 | public function getSpacing() { |
||
| 171 | return $this->spacing; |
||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Sets the spacing. |
||
| 176 | * |
||
| 177 | * @param int $spacing |
||
| 178 | */ |
||
| 179 | public function setSpacing($spacing) { |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Gets the rotation angle in degree. |
||
| 185 | * |
||
| 186 | * @return int |
||
| 187 | */ |
||
| 188 | public function getRotationAngle() { |
||
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Sets the rotation angle in degree. |
||
| 194 | * |
||
| 195 | * @param int $rotationAngle |
||
| 196 | */ |
||
| 197 | public function setRotationAngle($rotationAngle) { |
||
| 198 | $this->rotationAngle = intval($rotationAngle); |
||
| 199 | $this->font->setRotationAngle($this->rotationAngle); |
||
| 200 | } |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Gets the background color in case of rotation. |
||
| 204 | * |
||
| 205 | * @return BCGColor |
||
| 206 | */ |
||
| 207 | public function getBackgroundColor() { |
||
| 208 | return $this->backgroundColor; |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Sets the background color in case of rotation. |
||
| 213 | * |
||
| 214 | * @param BCGColor $backgroundColor |
||
| 215 | */ |
||
| 216 | public /*internal*/ function setBackgroundColor($backgroundColor) { |
||
| 217 | $this->backgroundColor = $backgroundColor; |
||
| 218 | $this->font->setBackgroundColor($this->backgroundColor); |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Gets the foreground color. |
||
| 223 | * |
||
| 224 | * @return BCGColor |
||
| 225 | */ |
||
| 226 | public function getForegroundColor() { |
||
| 227 | return $this->font->getForegroundColor(); |
||
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Sets the foreground color. |
||
| 232 | * |
||
| 233 | * @param BCGColor $foregroundColor |
||
| 234 | */ |
||
| 235 | public function setForegroundColor($foregroundColor) { |
||
| 236 | $this->foregroundColor = $foregroundColor; |
||
| 237 | $this->font->setForegroundColor($this->foregroundColor); |
||
| 238 | } |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Gets the dimension taken by the label, including the spacing and offset. |
||
| 242 | * [0]: width |
||
| 243 | * [1]: height |
||
| 244 | * |
||
| 245 | * @return int[] |
||
| 246 | */ |
||
| 247 | public function getDimension() { |
||
| 248 | $w = 0; |
||
|
|
|||
| 249 | $h = 0; |
||
| 250 | |||
| 251 | $dimension = $this->font->getDimension(); |
||
| 252 | $w = $dimension[0]; |
||
| 253 | $h = $dimension[1]; |
||
| 254 | |||
| 255 | if ($this->position === self::POSITION_TOP || $this->position === self::POSITION_BOTTOM) { |
||
| 256 | $h += $this->spacing; |
||
| 257 | $w += max(0, $this->offset); |
||
| 258 | } else { |
||
| 259 | $w += $this->spacing; |
||
| 260 | $h += max(0, $this->offset); |
||
| 261 | } |
||
| 262 | |||
| 263 | return array($w, $h); |
||
| 264 | } |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Draws the text. |
||
| 268 | * The coordinate passed are the positions of the barcode. |
||
| 269 | * $x1 and $y1 represent the top left corner. |
||
| 270 | * $x2 and $y2 represent the bottom right corner. |
||
| 271 | * |
||
| 272 | * @param resource $im |
||
| 273 | * @param int $x1 |
||
| 274 | * @param int $y1 |
||
| 275 | * @param int $x2 |
||
| 276 | * @param int $y2 |
||
| 277 | */ |
||
| 278 | public /*internal*/ function draw($im, $x1, $y1, $x2, $y2) { |
||
| 316 | } |
||
| 317 | } |
||
| 318 | ?> |
||