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 NumberValidatorCallback implements CallbackInterface, CallbackFactoryInterface |
||
| 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 default locale. |
||
| 46 | * |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | protected $locale = 'en_US'; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Initializes the callback with the loader instance. |
||
| 53 | * |
||
| 54 | * @param boolean $nullable The flag to decide whether or not the value can be empty |
||
| 55 | */ |
||
| 56 | public function __construct($nullable = false) |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Will be invoked by the callback visitor when a factory has been defined to create the callback instance. |
||
| 63 | * |
||
| 64 | * @param \TechDivision\Import\Subjects\SubjectInterface $subject The subject instance |
||
| 65 | * |
||
| 66 | * @return \TechDivision\Import\Callbacks\CallbackInterface The callback instance |
||
| 67 | */ |
||
| 68 | public function createCallback(SubjectInterface $subject) |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Query whether or not the passed value IS empty and empty values are allowed. |
||
| 80 | * |
||
| 81 | * @param string $attributeValue The attribute value to query for |
||
| 82 | * |
||
| 83 | * @return boolean TRUE if empty values are allowed and the passed value IS empty |
||
| 84 | */ |
||
| 85 | protected function isNullable($attributeValue) |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Query whether or not the passed value IS a valid number. |
||
| 92 | * |
||
| 93 | * @param string $attributeValue The attribute value to query for |
||
| 94 | * |
||
| 95 | * @return boolean TRUE if the passed value IS a valid number |
||
| 96 | */ |
||
| 97 | protected function isNumber($attributeValue) |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Will be invoked by the observer it has been registered for. |
||
| 104 | * |
||
| 105 | * @param string|null $attributeCode The code of the attribute that has to be validated |
||
| 106 | * @param string|null $attributeValue The attribute value to be validated |
||
| 107 | * |
||
| 108 | * @return mixed The modified value |
||
| 109 | */ |
||
| 110 | View Code Duplication | public function handle($attributeCode = null, $attributeValue = null) |
|
| 127 | } |
||
| 128 |
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.