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 |
||
24 | class Model extends DatabaseQuery implements ModelInterface |
||
25 | { |
||
26 | //Inject the inflector trait |
||
27 | use Inflector; |
||
28 | |||
29 | /** |
||
30 | * stripclassName() |
||
31 | * |
||
32 | * @return string |
||
33 | */ |
||
34 | public static function stripclassName() |
||
40 | |||
41 | /** |
||
42 | * getClassName() |
||
43 | * |
||
44 | * @return string |
||
45 | */ |
||
46 | public static function getClassName() |
||
50 | |||
51 | /** |
||
52 | * getTableName() |
||
53 | * |
||
54 | * @return string |
||
55 | */ |
||
56 | public static function getTableName($connection) |
||
60 | |||
61 | /** |
||
62 | * getALL() |
||
63 | * Get all record from the database |
||
64 | * |
||
65 | * @return object |
||
66 | */ |
||
67 | public function getALL($dbConnection = NULL) |
||
80 | |||
81 | /** |
||
82 | * where($data, $condition) |
||
83 | * Get data from database where $data = $condition |
||
84 | * |
||
85 | * @return object |
||
86 | */ |
||
87 | public function where($data, $condition = NULL, $dbConnection = NULL) |
||
101 | |||
102 | /** |
||
103 | * find($value) |
||
104 | * Find data from database where id = $value |
||
105 | * |
||
106 | * @return array |
||
107 | */ |
||
108 | public static function find($value, $dbConnection = NULL) |
||
124 | |||
125 | /** |
||
126 | * save() |
||
127 | * Insert data into database |
||
128 | * |
||
129 | * @return bool |
||
130 | */ |
||
131 | View Code Duplication | public function save($dbConnection = NULL) |
|
132 | { |
||
133 | $connection = DatabaseQuery::checkConnection($dbConnection); |
||
134 | |||
135 | $query = $this->insertQuery(self::getTableName($connection)); |
||
136 | $statement = $connection->prepare($query); |
||
137 | if( $statement->execute() ) |
||
138 | { |
||
139 | return true; |
||
140 | } |
||
141 | throw new SaveUserExistException(); |
||
142 | |||
143 | } |
||
144 | |||
145 | /** |
||
146 | * update() |
||
147 | * Update details in database after ::find(2) |
||
148 | * |
||
149 | * @return bool |
||
150 | */ |
||
151 | View Code Duplication | public function update($dbConnection = NULL) |
|
163 | |||
164 | /** |
||
165 | * destroy($value) |
||
166 | * Delete data from database |
||
167 | * |
||
168 | * @return bool |
||
169 | */ |
||
170 | public function destroy($value, $dbConnection = NULL) |
||
183 | } |
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.