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 |
||
25 | class Validate |
||
26 | { |
||
27 | /* |
||
28 | |-------------------------------------------------------------------------- |
||
29 | | Validate Class |
||
30 | |-------------------------------------------------------------------------- |
||
31 | | |
||
32 | | For validation the Email and Username in DB. |
||
33 | | |
||
34 | */ |
||
35 | |||
36 | protected $connect; |
||
37 | |||
38 | /** |
||
39 | * Create a new controller instance. |
||
40 | * |
||
41 | * @return void |
||
42 | */ |
||
43 | public function __construct() |
||
47 | |||
48 | /** |
||
49 | * For checking whether the credentials are empty or not |
||
50 | * |
||
51 | * @param string $email Contains the email for checking |
||
52 | * |
||
53 | * @return 0 | 1 |
||
54 | */ |
||
55 | View Code Duplication | public function validateEmailInDb($email) |
|
65 | |||
66 | /** |
||
67 | * For checking whether the credentials are empty or not |
||
68 | * |
||
69 | * @param string $username Contains the username for checking |
||
70 | * |
||
71 | * @return 0 | 1 |
||
72 | */ |
||
73 | View Code Duplication | public function validateUsernameInDb($username) |
|
83 | } |
||
84 |
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.