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 |
||
13 | class Describe |
||
14 | { |
||
15 | /** |
||
16 | * @var \Rougin\Describe\Driver\DriverInterface |
||
17 | */ |
||
18 | protected $driver; |
||
19 | |||
20 | /** |
||
21 | * @param \Rougin\Describe\Driver\DriverInterface $driver |
||
22 | */ |
||
23 | 84 | public function __construct(Driver\DriverInterface $driver) |
|
24 | { |
||
25 | 84 | $this->driver = $driver; |
|
26 | 84 | } |
|
27 | |||
28 | /** |
||
29 | * Returns a listing of columns from the specified table. |
||
30 | * |
||
31 | * @param string $tableName |
||
32 | * @return array |
||
33 | * @throws \Rougin\Describe\Exceptions\TableNameNotFoundException |
||
34 | */ |
||
35 | 69 | public function getColumns($tableName) |
|
36 | { |
||
37 | 69 | $table = $this->driver->getTable($tableName); |
|
38 | |||
39 | 69 | if (empty($table) || is_null($table)) { |
|
40 | 6 | throw new Exceptions\TableNameNotFoundException; |
|
41 | } |
||
42 | |||
43 | 63 | return $table; |
|
44 | } |
||
45 | |||
46 | /** |
||
47 | * Gets the primary key in the specified table. |
||
48 | * |
||
49 | * @param string $tableName |
||
50 | * @param boolean $object |
||
51 | * @return string |
||
52 | */ |
||
53 | 15 | public function getPrimaryKey($tableName, $object = false) |
|
54 | { |
||
55 | 15 | $columns = $this->getColumns($tableName); |
|
56 | 15 | $result = ''; |
|
57 | |||
58 | 15 | foreach ($columns as $column) { |
|
59 | 15 | if ($column->isPrimaryKey()) { |
|
60 | 15 | $result = $column; |
|
61 | 15 | } |
|
62 | 15 | } |
|
63 | |||
64 | 15 | return ($object === true) ? $result : $result->getField(); |
|
65 | } |
||
66 | |||
67 | /** |
||
68 | * Returns a listing of columns from the specified table. |
||
69 | * NOTE: To be removed in v2.0.0. |
||
70 | * |
||
71 | * @param string $tableName |
||
72 | * @return array |
||
73 | * @throws \Rougin\Describe\Exceptions\TableNameNotFoundException |
||
74 | */ |
||
75 | 54 | public function getTable($tableName) |
|
76 | { |
||
77 | 54 | return $this->getColumns($tableName); |
|
78 | } |
||
79 | |||
80 | /** |
||
81 | * Returns a listing of tables from the specified database. |
||
82 | * |
||
83 | * @return array |
||
84 | */ |
||
85 | 15 | public function getTableNames() |
|
86 | { |
||
87 | 15 | return $this->driver->showTables(); |
|
88 | } |
||
89 | |||
90 | /** |
||
91 | * Shows the list of tables. |
||
92 | * NOTE: To be removed in v2.0.0. |
||
93 | * |
||
94 | * @return array |
||
95 | */ |
||
96 | 15 | public function showTables() |
|
100 | |||
101 | /** |
||
102 | * Calls methods from this class in underscore case. |
||
103 | * |
||
104 | * @param string $method |
||
105 | * @param mixed $parameters |
||
106 | * @return mixed |
||
107 | */ |
||
108 | 3 | View Code Duplication | public function __call($method, $parameters) |
119 | } |
||
120 |
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.