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 |
||
26 | class Login |
||
27 | { |
||
28 | /* |
||
29 | |-------------------------------------------------------------------------- |
||
30 | | Login Class |
||
31 | |-------------------------------------------------------------------------- |
||
32 | | |
||
33 | | For Login. |
||
34 | | |
||
35 | */ |
||
36 | |||
37 | protected $flag; |
||
38 | protected $error; |
||
39 | protected $connect; |
||
40 | |||
41 | /** |
||
42 | * Create a new controller instance. |
||
43 | * |
||
44 | * @return void |
||
45 | */ |
||
46 | public function __construct() |
||
52 | |||
53 | /** |
||
54 | * Credentials check for allowing user to login |
||
55 | * |
||
56 | * @param array $data Contains the User Credentials |
||
57 | * |
||
58 | * @return json |
||
59 | */ |
||
60 | public function authLogin($data) |
||
118 | |||
119 | /** |
||
120 | * For generating Error array by key value pair |
||
121 | * |
||
122 | * @param string $key Contains key |
||
123 | * @param string $value Contains the Value for the key |
||
124 | * |
||
125 | * @return void |
||
126 | */ |
||
127 | View Code Duplication | public function onError($key, $value) |
|
140 | |||
141 | /** |
||
142 | * For checking whether the credentials are empty or not |
||
143 | * |
||
144 | * @param array $data Contains the Credentials |
||
145 | * |
||
146 | * @return array |
||
147 | */ |
||
148 | public function emptyValue($data) |
||
164 | } |
||
165 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.