Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 9 | class CAsciiArt |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * Character set to use. |
||
| 13 | */ |
||
| 14 | private $characterSet = array( |
||
| 15 | 'one' => "#0XT|:,.' ", |
||
| 16 | 'two' => "@%#*+=-:. ", |
||
| 17 | 'three' => "$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,\"^`'. " |
||
| 18 | ); |
||
| 19 | |||
| 20 | |||
| 21 | |||
| 22 | /** |
||
| 23 | * Current character set. |
||
| 24 | */ |
||
| 25 | private $characters = null; |
||
| 26 | |||
| 27 | |||
| 28 | |||
| 29 | /** |
||
| 30 | * Length of current character set. |
||
| 31 | */ |
||
| 32 | private $charCount = null; |
||
| 33 | |||
| 34 | |||
| 35 | |||
| 36 | /** |
||
| 37 | * Scale of the area to swap to a character. |
||
| 38 | */ |
||
| 39 | private $scale = null; |
||
| 40 | |||
| 41 | |||
| 42 | |||
| 43 | /** |
||
| 44 | * Strategy to calculate luminance. |
||
| 45 | */ |
||
| 46 | private $luminanceStrategy = null; |
||
| 47 | |||
| 48 | |||
| 49 | |||
| 50 | /** |
||
| 51 | * Constructor which sets default options. |
||
| 52 | */ |
||
| 53 | public function __construct() |
||
| 57 | |||
| 58 | |||
| 59 | |||
| 60 | /** |
||
| 61 | * Add a custom character set. |
||
| 62 | * |
||
| 63 | * @param string $key for the character set. |
||
| 64 | * @param string $value for the character set. |
||
| 65 | * |
||
| 66 | * @return $this |
||
| 67 | */ |
||
| 68 | public function addCharacterSet($key, $value) |
||
| 73 | |||
| 74 | |||
| 75 | |||
| 76 | /** |
||
| 77 | * Set options for processing, defaults are available. |
||
| 78 | * |
||
| 79 | * @param array $options to use as default settings. |
||
| 80 | * |
||
| 81 | * @return $this |
||
| 82 | */ |
||
| 83 | public function setOptions($options = array()) |
||
| 105 | |||
| 106 | |||
| 107 | |||
| 108 | /** |
||
| 109 | * Create an Ascii image from an image file. |
||
| 110 | * |
||
| 111 | * @param string $filename of the image to use. |
||
| 112 | * |
||
| 113 | * @return string $ascii with the ASCII image. |
||
| 114 | */ |
||
| 115 | public function createFromFile($filename) |
||
| 136 | |||
| 137 | |||
| 138 | |||
| 139 | /** |
||
| 140 | * Get the luminance from a region of an image using average color value. |
||
| 141 | * |
||
| 142 | * @param string $img the image. |
||
| 143 | * @param integer $x1 the area to get pixels from. |
||
| 144 | * @param integer $y1 the area to get pixels from. |
||
| 145 | * @param integer $x2 the area to get pixels from. |
||
| 146 | * @param integer $y2 the area to get pixels from. |
||
| 147 | * |
||
| 148 | * @return integer $luminance with a value between 0 and 100. |
||
| 149 | */ |
||
| 150 | public function luminanceAreaAverage($img, $x1, $y1, $x2, $y2) |
||
| 167 | |||
| 168 | |||
| 169 | |||
| 170 | /** |
||
| 171 | * Calculate luminance value with different strategies. |
||
| 172 | * |
||
| 173 | * @param integer $red The color red. |
||
| 174 | * @param integer $green The color green. |
||
| 175 | * @param integer $blue The color blue. |
||
| 176 | * |
||
| 177 | * @return float $luminance with a value between 0 and 1. |
||
| 178 | */ |
||
| 179 | public function getLuminance($red, $green, $blue) |
||
| 198 | |||
| 199 | |||
| 200 | |||
| 201 | /** |
||
| 202 | * Translate the luminance value to a character. |
||
| 203 | * |
||
| 204 | * @param string $position a value between 0-100 representing the |
||
| 205 | * luminance. |
||
| 206 | * |
||
| 207 | * @return string with the ascii character. |
||
| 208 | */ |
||
| 209 | public function luminance2character($luminance) |
||
| 215 | } |
||
| 216 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: