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 |
||
| 63 | class BaseElementAction |
||
| 64 | { |
||
| 65 | use VisibleElementFinder; |
||
| 66 | |||
| 67 | const SINGLE_TARGET = 1; |
||
| 68 | const PLURAL_TARGET = 2; |
||
| 69 | |||
| 70 | protected $countTypes = array( |
||
| 71 | "any" => "any", |
||
| 72 | "several" => "several", |
||
| 73 | "no" => 0, |
||
| 74 | "zero" => 0, |
||
| 75 | "one" => 1, |
||
| 76 | "a" => 1, |
||
| 77 | "an" => 2, |
||
| 78 | "two" => 2, |
||
| 79 | "three" => 3, |
||
| 80 | "four" => 4, |
||
| 81 | "five" => 5, |
||
| 82 | "six" => 6, |
||
| 83 | "seven" => 7, |
||
| 84 | "eight" => 8, |
||
| 85 | "nine" => 9, |
||
| 86 | "ten" => 10, |
||
| 87 | "eleven" => 11, |
||
| 88 | "twelve" => 12, |
||
| 89 | "thirteen" => 13, |
||
| 90 | "fourteen" => 14, |
||
| 91 | "fifteen" => 15, |
||
| 92 | "sixteen" => 16, |
||
| 93 | "seventeen" => 17, |
||
| 94 | "eighteen" => 18, |
||
| 95 | "nineteen" => 19, |
||
| 96 | "twenty" => 20, |
||
| 97 | ); |
||
| 98 | |||
| 99 | protected $indexTypes = array( |
||
| 100 | "first" => 0, |
||
| 101 | "second" => 1, |
||
| 102 | "third" => 2, |
||
| 103 | "fourth" => 3, |
||
| 104 | "fifth" => 4, |
||
| 105 | "sixth" => 5, |
||
| 106 | "seventh" => 6, |
||
| 107 | "eighth" => 7, |
||
| 108 | "ninth" => 8, |
||
| 109 | "tenth" => 9, |
||
| 110 | "eleventh" => 10, |
||
| 111 | "twelfth" => 11, |
||
| 112 | "thirteenth" => 12, |
||
| 113 | "fourteenth" => 13, |
||
| 114 | "fifteenth" => 14, |
||
| 115 | "sixteenth" => 15, |
||
| 116 | "seventeenth" => 16, |
||
| 117 | "eighteenth" => 17, |
||
| 118 | "nineteenth" => 18, |
||
| 119 | "twentieth" => 19, |
||
| 120 | ); |
||
| 121 | |||
| 122 | protected $tagTypes = array( |
||
| 123 | 'button' => array('input', 'button'), |
||
| 124 | 'buttons' => array('input','button'), |
||
| 125 | 'cell' => 'td', |
||
| 126 | 'cells' => 'td', |
||
| 127 | 'heading' => array('h1', 'h2', 'h3', 'h4', 'h5','h6'), |
||
| 128 | 'headings' => array('h1', 'h2', 'h3', 'h4', 'h5','h6'), |
||
| 129 | 'link' => 'a', |
||
| 130 | 'links' => 'a', |
||
| 131 | 'orderedlist' => 'ol', |
||
| 132 | 'unorderedlist' => 'ul' |
||
| 133 | ); |
||
| 134 | |||
| 135 | protected $targetTypes = array( |
||
| 136 | 'box' => self::SINGLE_TARGET, |
||
| 137 | 'boxes' => self::PLURAL_TARGET, |
||
| 138 | 'button' => self::SINGLE_TARGET, |
||
| 139 | 'buttons' => self::PLURAL_TARGET, |
||
| 140 | 'cell' => self::SINGLE_TARGET, |
||
| 141 | 'cells' => self::PLURAL_TARGET, |
||
| 142 | 'dropdown' => self::SINGLE_TARGET, |
||
| 143 | 'dropdowns' => self::PLURAL_TARGET, |
||
| 144 | 'element' => self::SINGLE_TARGET, |
||
| 145 | 'elements' => self::PLURAL_TARGET, |
||
| 146 | 'field' => self::SINGLE_TARGET, |
||
| 147 | 'fields' => self::PLURAL_TARGET, |
||
| 148 | 'heading' => self::SINGLE_TARGET, |
||
| 149 | 'headings' => self::PLURAL_TARGET, |
||
| 150 | 'link' => self::SINGLE_TARGET, |
||
| 151 | 'links' => self::PLURAL_TARGET, |
||
| 152 | 'orderedlist' => self::SINGLE_TARGET, |
||
| 153 | 'span' => self::SINGLE_TARGET, |
||
| 154 | 'unorderedlist' => self::SINGLE_TARGET |
||
| 155 | ); |
||
| 156 | |||
| 157 | protected $searchTypes = array ( |
||
| 158 | 'id' => 'ById', |
||
| 159 | 'label' => 'ByLabel', |
||
| 160 | 'labelled' => 'ByLabel', |
||
| 161 | 'named' => 'ByName', |
||
| 162 | 'name' => 'ByName', |
||
| 163 | 'text' => 'ByText', |
||
| 164 | 'class' => 'ByClass', |
||
| 165 | 'placeholder' => 'ByPlaceholder', |
||
| 166 | 'title' => 'ByTitle', |
||
| 167 | 'labelidortext' => 'ByLabelIdText', |
||
| 168 | ); |
||
| 169 | |||
| 170 | protected $baseElement; |
||
| 171 | |||
| 172 | public function __construct($baseElement) |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @param string $methodName |
||
| 179 | * camelCase string to parse |
||
| 180 | * @return array<string> |
||
| 181 | * $methodName broken into individual words, all lower-cased |
||
| 182 | */ |
||
| 183 | protected function convertMethodNameToWords($methodName) |
||
| 191 | |||
| 192 | /** |
||
| 193 | * @param array<string> $words |
||
| 194 | * a list of words to examine |
||
| 195 | * @return int|null |
||
| 196 | */ |
||
| 197 | protected function determineCountType($words) |
||
| 208 | |||
| 209 | /** |
||
| 210 | * @param array<string> $words |
||
| 211 | * a list of words to examine |
||
| 212 | * @return int|null |
||
| 213 | */ |
||
| 214 | View Code Duplication | protected function determineIndexType($words) |
|
| 225 | |||
| 226 | /** |
||
| 227 | * @param array<string> $words |
||
| 228 | * a list of words to examine |
||
| 229 | * @return string|null |
||
| 230 | */ |
||
| 231 | View Code Duplication | protected function determineSearchType($words) |
|
| 242 | |||
| 243 | /** |
||
| 244 | * @param array<string> $words |
||
| 245 | * a list of words to examine |
||
| 246 | * @return string |
||
| 247 | */ |
||
| 248 | protected function determineTargetType($words) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @param string $targetType |
||
| 262 | * @return string |
||
| 263 | */ |
||
| 264 | protected function determineTagType($targetType) |
||
| 274 | |||
| 275 | /** |
||
| 276 | * @param string $targetType |
||
| 277 | * @return bool |
||
| 278 | */ |
||
| 279 | protected function isPluralTarget($targetType) |
||
| 294 | |||
| 295 | protected function retrieveElement($methodName, $methodArgs) |
||
| 310 | |||
| 311 | /** |
||
| 312 | * @param string $methodName |
||
| 313 | * @param array<mixed> $methodArgs |
||
| 314 | * @return array |
||
| 315 | */ |
||
| 316 | protected function retrieveElements($methodName, $methodArgs) |
||
| 343 | } |
||
| 344 |