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 |
||
36 | class TableConfiguration |
||
37 | { |
||
38 | use Traits\Language; |
||
39 | |||
40 | /** |
||
41 | * extensionConfiguration |
||
42 | * |
||
43 | * @var array $tableConfiguration |
||
44 | */ |
||
45 | protected $tableConfiguration; |
||
46 | |||
47 | /** |
||
48 | * table |
||
49 | * |
||
50 | * @var string $name |
||
51 | */ |
||
52 | protected $name; |
||
53 | |||
54 | /** |
||
55 | * ExtensionConfigurationUtility constructor. |
||
56 | * |
||
57 | * @param $tableName |
||
58 | */ |
||
59 | 17 | public function __construct($tableName) |
|
60 | { |
||
61 | 17 | View Code Duplication | if (!isset($GLOBALS['TCA'][$tableName])) { |
|
|||
62 | 1 | throw new \InvalidArgumentException( |
|
63 | 1 | 'Configuration for table ' . $tableName . ' not found!' |
|
64 | ); |
||
65 | } |
||
66 | 17 | $this->name = $tableName; |
|
67 | 17 | $this->tableConfiguration = $GLOBALS['TCA'][$tableName]; |
|
68 | 17 | } |
|
69 | |||
70 | /** |
||
71 | * getName |
||
72 | * |
||
73 | * @return string |
||
74 | */ |
||
75 | 1 | public function getName() |
|
79 | |||
80 | /** |
||
81 | * getName |
||
82 | * |
||
83 | * @return string |
||
84 | */ |
||
85 | 1 | public function getTitle() |
|
89 | |||
90 | /** |
||
91 | * getColumns |
||
92 | * |
||
93 | * @return array |
||
94 | */ |
||
95 | 8 | public function getColumns() |
|
99 | |||
100 | /** |
||
101 | * getColumnConfiguration |
||
102 | * |
||
103 | * @param string $columnName |
||
104 | * @return array |
||
105 | */ |
||
106 | 3 | public function getColumnConfiguration($columnName) |
|
107 | { |
||
108 | 3 | if (!is_string($columnName)) { |
|
109 | 1 | throw new \InvalidArgumentException( |
|
110 | 1 | 'Columns must be type of string. Type of ' . gettype($columnName) . ' given.' |
|
111 | ); |
||
112 | } |
||
113 | 2 | if (!isset($this->tableConfiguration['columns'][$columnName])) { |
|
114 | 1 | throw new \InvalidArgumentException( |
|
115 | 1 | 'Column ' . $columnName . ' does not exist for table ' . $this->name |
|
116 | ); |
||
117 | } |
||
118 | |||
119 | 1 | return array($columnName => $this->tableConfiguration['columns'][$columnName]['config']); |
|
120 | } |
||
121 | |||
122 | /** |
||
123 | * @return array |
||
124 | */ |
||
125 | 1 | public static function getAllTables() |
|
133 | } |
||
134 |
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.