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 |
||
| 6 | class CAsciiArt |
||
| 7 | { |
||
| 8 | /** |
||
| 9 | * Character set to use. |
||
| 10 | */ |
||
| 11 | private $characterSet = array( |
||
| 12 | 'one' => "#0XT|:,.' ", |
||
| 13 | 'two' => "@%#*+=-:. ", |
||
| 14 | 'three' => "$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,\"^`'. " |
||
| 15 | ); |
||
| 16 | |||
| 17 | |||
| 18 | |||
| 19 | /** |
||
| 20 | * Current character set. |
||
| 21 | */ |
||
| 22 | private $characters = null; |
||
| 23 | |||
| 24 | |||
| 25 | |||
| 26 | /** |
||
| 27 | * Length of current character set. |
||
| 28 | */ |
||
| 29 | private $charCount = null; |
||
| 30 | |||
| 31 | |||
| 32 | |||
| 33 | /** |
||
| 34 | * Scale of the area to swap to a character. |
||
| 35 | */ |
||
| 36 | private $scale = null; |
||
| 37 | |||
| 38 | |||
| 39 | |||
| 40 | /** |
||
| 41 | * Strategy to calculate luminance. |
||
| 42 | */ |
||
| 43 | private $luminanceStrategy = null; |
||
| 44 | |||
| 45 | |||
| 46 | |||
| 47 | /** |
||
| 48 | * Constructor which sets default options. |
||
| 49 | */ |
||
| 50 | public function __construct() |
||
| 54 | |||
| 55 | |||
| 56 | |||
| 57 | /** |
||
| 58 | * Add a custom character set. |
||
| 59 | * |
||
| 60 | * @param string $key for the character set. |
||
| 61 | * @param string $value for the character set. |
||
| 62 | * |
||
| 63 | * @return $this |
||
| 64 | */ |
||
| 65 | public function addCharacterSet($key, $value) |
||
| 70 | |||
| 71 | |||
| 72 | |||
| 73 | /** |
||
| 74 | * Set options for processing, defaults are available. |
||
| 75 | * |
||
| 76 | * @param array $options to use as default settings. |
||
| 77 | * |
||
| 78 | * @return $this |
||
| 79 | */ |
||
| 80 | public function setOptions($options = array()) |
||
| 102 | |||
| 103 | |||
| 104 | |||
| 105 | /** |
||
| 106 | * Create an Ascii image from an image file. |
||
| 107 | * |
||
| 108 | * @param string $filename of the image to use. |
||
| 109 | * |
||
| 110 | * @return string $ascii with the ASCII image. |
||
| 111 | */ |
||
| 112 | public function createFromFile($filename) |
||
| 133 | |||
| 134 | |||
| 135 | |||
| 136 | /** |
||
| 137 | * Get the luminance from a region of an image using average color value. |
||
| 138 | * |
||
| 139 | * @param string $img the image. |
||
| 140 | * @param integer $x1 the area to get pixels from. |
||
| 141 | * @param integer $y1 the area to get pixels from. |
||
| 142 | * @param integer $x2 the area to get pixels from. |
||
| 143 | * @param integer $y2 the area to get pixels from. |
||
| 144 | * |
||
| 145 | * @return integer $luminance with a value between 0 and 100. |
||
| 146 | */ |
||
| 147 | public function luminanceAreaAverage($img, $x1, $y1, $x2, $y2) |
||
| 164 | |||
| 165 | |||
| 166 | |||
| 167 | /** |
||
| 168 | * Calculate luminance value with different strategies. |
||
| 169 | * |
||
| 170 | * @param integer $red The color red. |
||
| 171 | * @param integer $green The color green. |
||
| 172 | * @param integer $blue The color blue. |
||
| 173 | * |
||
| 174 | * @return float $luminance with a value between 0 and 1. |
||
| 175 | */ |
||
| 176 | public function getLuminance($red, $green, $blue) |
||
| 195 | |||
| 196 | |||
| 197 | |||
| 198 | /** |
||
| 199 | * Translate the luminance value to a character. |
||
| 200 | * |
||
| 201 | * @param string $position a value between 0-100 representing the |
||
| 202 | * luminance. |
||
| 203 | * |
||
| 204 | * @return string with the ascii character. |
||
| 205 | */ |
||
| 206 | public function luminance2character($luminance) |
||
| 212 | } |
||
| 213 |
$filenamecan contain request data and is used in file inclusion context(s) leading to a potential security vulnerability.General Strategies to prevent injection
In general, it is advisable to prevent any user-data to reach this point. This can be done by white-listing certain values:
if ( ! in_array($value, array('this-is-allowed', 'and-this-too'), true)) { throw new \InvalidArgumentException('This input is not allowed.'); }For numeric data, we recommend to explicitly cast the data: