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 |
||
28 | class YamlParser implements ParserInterface |
||
29 | { |
||
30 | |||
31 | /** Default form class to create */ |
||
32 | const DEFAULT_FORM_CLASS = 'Slick\Form\Form'; |
||
33 | |||
34 | /** |
||
35 | * @var array The YAML parser data |
||
36 | */ |
||
37 | protected $parsedData; |
||
38 | |||
39 | /** |
||
40 | * @var WorkerInterface[] |
||
41 | */ |
||
42 | protected static $workers = [ |
||
43 | 'formId' => FormId::class, |
||
44 | 'addElements' => AddElements::class |
||
45 | ]; |
||
46 | |||
47 | /** |
||
48 | * Crates a definition parser |
||
49 | * |
||
50 | * @param string $definitions The full path for definitions file |
||
51 | */ |
||
52 | 28 | public function __construct($definitions) |
|
56 | |||
57 | /** |
||
58 | * Create form using provided definitions |
||
59 | * |
||
60 | * @param string $definitions |
||
61 | * |
||
62 | * @return FormInterface |
||
63 | */ |
||
64 | 16 | public static function create($definitions) |
|
69 | |||
70 | /** |
||
71 | * Gets the resulting form object |
||
72 | * |
||
73 | * @return FormInterface |
||
74 | */ |
||
75 | 20 | public function getForm() |
|
81 | |||
82 | /** |
||
83 | * Updates the form with definitions from parsed data |
||
84 | * |
||
85 | * @param FormInterface $form |
||
86 | */ |
||
87 | 12 | protected function workout(FormInterface $form) |
|
96 | |||
97 | /** |
||
98 | * Creates the form class |
||
99 | * |
||
100 | * @return FormInterface |
||
101 | */ |
||
102 | 20 | protected function createForm() |
|
111 | |||
112 | 14 | protected function checkClass($name) |
|
113 | { |
||
114 | 14 | if (! class_exists($name)) { |
|
115 | 4 | throw new InvalidArgumentException( |
|
116 | 4 | "The form class '{$name}' does not exists." |
|
117 | 4 | ); |
|
118 | } |
||
119 | |||
120 | 10 | if (! is_subclass_of($name, FormInterface::class)) { |
|
121 | 4 | throw new InvalidArgumentException( |
|
122 | 4 | "The class '{$name}' does not implements the " . |
|
123 | "FormInterface interface" |
||
124 | 4 | ); |
|
125 | } |
||
126 | 6 | return $name; |
|
127 | } |
||
128 | |||
129 | /** |
||
130 | * Sets definition data for form creation |
||
131 | * |
||
132 | * @param mixed $definitions |
||
133 | * |
||
134 | * @return self|$this|ParserInterface |
||
135 | */ |
||
136 | 14 | View Code Duplication | public function setData($definitions) |
159 | } |
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.