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 |
||
34 | class ArrayValidatorCallback extends AbstractValidatorCallback |
||
35 | { |
||
36 | |||
37 | /** |
||
38 | * The flag to query whether or not the value can be empty. |
||
39 | * |
||
40 | * @var boolean |
||
41 | */ |
||
42 | protected $nullable = false; |
||
43 | |||
44 | /** |
||
45 | * The flag to query whether or not the value has to be validated on the main row only. |
||
46 | * |
||
47 | * @var boolean |
||
48 | */ |
||
49 | protected $mainRowOnly = false; |
||
50 | |||
51 | /** |
||
52 | * Initializes the callback with the loader instance. |
||
53 | * |
||
54 | * @param \TechDivision\Import\Loaders\LoaderInterface $loader The loader instance to load the validations with |
||
55 | * @param boolean $nullable The flag to decide whether or not the value can be empty |
||
56 | * @param boolean $mainRowOnly The flag to decide whether or not the value has to be validated on the main row only |
||
57 | */ |
||
58 | public function __construct(LoaderInterface $loader, $nullable = false, $mainRowOnly = false) |
||
68 | |||
69 | /** |
||
70 | * Will be invoked by a observer it has been registered for. |
||
71 | * |
||
72 | * @param string|null $attributeCode The code of the attribute that has to be validated |
||
73 | * @param string|null $attributeValue The attribute value to be validated |
||
74 | * |
||
75 | * @return mixed The modified value |
||
76 | */ |
||
77 | public function handle($attributeCode = null, $attributeValue = null) |
||
105 | |||
106 | /** |
||
107 | * Query whether or not the passed value IS empty and empty values are allowed. |
||
108 | * |
||
109 | * @param string $attributeValue The attribute value to query for |
||
110 | * |
||
111 | * @return boolean TRUE if empty values are allowed and the passed value IS empty |
||
112 | */ |
||
113 | protected function isNullable($attributeValue) |
||
142 | } |
||
143 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.