Complex classes like ImageUrlBuilder 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 ImageUrlBuilder, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | final class ImageUrlBuilder |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * mode合法范围值 |
||
| 15 | * |
||
| 16 | * @var array |
||
| 17 | */ |
||
| 18 | protected $modeArr = array(0, 1, 2, 3, 4, 5); |
||
| 19 | |||
| 20 | /** |
||
| 21 | * format合法值 |
||
| 22 | * |
||
| 23 | * @var array |
||
| 24 | */ |
||
| 25 | protected $formatArr = array('psd', 'jpeg', 'png', 'gif', 'webp', 'tiff', 'bmp'); |
||
| 26 | |||
| 27 | /** |
||
| 28 | * 水印图片位置合法值 |
||
| 29 | * |
||
| 30 | * @var array |
||
| 31 | */ |
||
| 32 | protected $gravityArr = array('NorthWest', 'North', 'NorthEast', |
||
| 33 | 'West', 'Center', 'East', 'SouthWest', 'South', 'SouthEast'); |
||
| 34 | |||
| 35 | /** |
||
| 36 | * 缩略图链接拼接 |
||
| 37 | * |
||
| 38 | * @param string $url 图片链接 |
||
| 39 | * @param int $mode 缩略模式 |
||
| 40 | * @param int $width 宽度 |
||
| 41 | * @param int $height 长度 |
||
| 42 | * @param string $format 输出类型 |
||
| 43 | * @param int $quality 图片质量 |
||
| 44 | * @param int $interlace 是否支持渐进显示 |
||
| 45 | * @param int $ignoreError 忽略结果 |
||
| 46 | * @return string |
||
| 47 | * @link http://developer.qiniu.com/code/v6/api/kodo-api/image/imageview2.html |
||
| 48 | * @author Sherlock Ren <[email protected]> |
||
| 49 | */ |
||
| 50 | public function thumbnail( |
||
| 104 | |||
| 105 | /** |
||
| 106 | * 图片水印 |
||
| 107 | * |
||
| 108 | * @param string $url 图片链接 |
||
| 109 | * @param string $image 水印图片链接 |
||
| 110 | * @param numeric $dissolve 透明度 |
||
| 111 | * @param string $gravity 水印位置 |
||
| 112 | * @param numeric $dx 横轴边距 |
||
| 113 | * @param numeric $dy 纵轴边距 |
||
| 114 | * @param numeric $watermarkScale 自适应原图的短边比例 |
||
| 115 | * @link http://developer.qiniu.com/code/v6/api/kodo-api/image/watermark.html |
||
| 116 | * @return string |
||
| 117 | * @author Sherlock Ren <[email protected]> |
||
| 118 | */ |
||
| 119 | public function waterImg( |
||
| 173 | |||
| 174 | /** |
||
| 175 | * 文字水印 |
||
| 176 | * |
||
| 177 | * @param string $url 图片链接 |
||
| 178 | * @param string $text 文字 |
||
| 179 | * @param string $font 文字字体 |
||
| 180 | * @param string $fontSize 文字字号 |
||
| 181 | * @param string $fontColor 文字颜色 |
||
| 182 | * @param numeric $dissolve 透明度 |
||
| 183 | * @param string $gravity 水印位置 |
||
| 184 | * @param numeric $dx 横轴边距 |
||
| 185 | * @param numeric $dy 纵轴边距 |
||
| 186 | * @link http://developer.qiniu.com/code/v6/api/kodo-api/image/watermark.html#text-watermark |
||
| 187 | * @return string |
||
| 188 | * @author Sherlock Ren <[email protected]> |
||
| 189 | */ |
||
| 190 | public function waterText( |
||
| 251 | |||
| 252 | /** |
||
| 253 | * 效验url合法性 |
||
| 254 | * |
||
| 255 | * @param string $url url链接 |
||
| 256 | * @return string |
||
| 257 | * @author Sherlock Ren <[email protected]> |
||
| 258 | */ |
||
| 259 | protected function isUrl($url) |
||
| 268 | |||
| 269 | /** |
||
| 270 | * 检测是否有query |
||
| 271 | * |
||
| 272 | * @param string $url url链接 |
||
| 273 | * @return string |
||
| 274 | * @author Sherlock Ren <[email protected]> |
||
| 275 | */ |
||
| 276 | protected function hasQuery($url) |
||
| 282 | } |
||
| 283 |