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 |
||
14 | class Helper |
||
15 | { |
||
16 | /** |
||
17 | * Log Object |
||
18 | */ |
||
19 | protected static $_logObject=null; // @todo get rid of this |
||
20 | |||
21 | /** |
||
22 | * Serve your site |
||
23 | * Loads routes based off of context and serves up the current request |
||
24 | * |
||
25 | * @param string $context optional context defaults to getenv('ERDIKO_CONTEXT') |
||
26 | */ |
||
27 | public static function serve($context = null) |
||
35 | |||
36 | /** |
||
37 | * Load a view from the current theme with the given data |
||
38 | * |
||
39 | * @param string $viewName |
||
40 | * @param array $data |
||
41 | */ |
||
42 | View Code Duplication | public static function getView($viewName, $data = null, $templateRootFolder = null) |
|
51 | |||
52 | /** |
||
53 | * Read JSON config file and return array |
||
54 | * |
||
55 | * @param string $file |
||
56 | * @return array $config |
||
57 | */ |
||
58 | public static function getConfigFile($filename) |
||
75 | |||
76 | /** |
||
77 | * Get configuration |
||
78 | */ |
||
79 | View Code Duplication | public static function getConfig($name = 'application', $context = null) |
|
87 | |||
88 | /** |
||
89 | * Get the compiled application routes from the config files |
||
90 | * |
||
91 | * @todo cache the loaded/compiled routes |
||
92 | */ |
||
93 | View Code Duplication | public static function getRoutes($context = null) |
|
102 | |||
103 | /** |
||
104 | * Send email |
||
105 | * @todo add ways to swap out ways of sending |
||
106 | */ |
||
107 | public static function sendEmail($toEmail, $subject, $body, $fromEmail) |
||
115 | |||
116 | /** |
||
117 | * log message to log file |
||
118 | * If you enter null for level it will default to 'debug' |
||
119 | * |
||
120 | * @usage \Erdiko::log('debug',"Message here...", array()) |
||
121 | * |
||
122 | * @param string $level |
||
123 | * @param string $message |
||
124 | * @param array $context |
||
125 | * @return bool $success |
||
126 | * @todo refactor how logging is used, eventually remove from helper |
||
127 | */ |
||
128 | public static function log($level, $message, array $context = array()) |
||
145 | |||
146 | /** |
||
147 | * log debug message to log file |
||
148 | * |
||
149 | * @param string $message |
||
150 | * @param array $context |
||
151 | * @return bool $success |
||
152 | * @todo refactor how logging is used, eventually remove from helper |
||
153 | */ |
||
154 | public static function debug($message, array $context = array()) |
||
158 | |||
159 | /** |
||
160 | * log error message to log file |
||
161 | * |
||
162 | * @param string $message |
||
163 | * @param array $context |
||
164 | * @return bool $success |
||
165 | * @todo refactor how logging is used, eventually remove from helper |
||
166 | */ |
||
167 | public static function error($message, array $context = array()) |
||
171 | |||
172 | /** |
||
173 | * Get the configured cache instance using name |
||
174 | * |
||
175 | * @return cache $cache returns the instance of the cache type |
||
176 | */ |
||
177 | public static function getCache($cacheType = "default") |
||
190 | } |
||
191 |
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.