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 |
||
18 | class Suricate |
||
19 | { |
||
20 | |||
21 | const VERSION = '0.1'; |
||
22 | |||
23 | const CONF_DIR = '/conf/'; |
||
24 | |||
25 | protected $router; |
||
26 | |||
27 | private $config; |
||
28 | private $configFile; |
||
29 | |||
30 | private $useAutoloader = false; |
||
31 | |||
32 | private static $servicesContainer; |
||
33 | private static $servicesRepository; |
||
34 | |||
35 | private $servicesList = array( |
||
36 | 'Logger' => '\Suricate\Logger', |
||
37 | 'App' => '\Suricate\App', |
||
38 | 'I18n' => '\Suricate\I18n', |
||
39 | 'Error' => '\Suricate\Error', |
||
40 | 'Router' => '\Suricate\Router', |
||
41 | 'Request' => '\Suricate\Request', |
||
42 | 'Database' => '\Suricate\Database', |
||
43 | 'Cache' => '\Suricate\Cache', |
||
44 | 'CacheMemcache' => '\Suricate\Cache\Memcache', |
||
45 | 'CacheApc' => '\Suricate\Cache\Apc', |
||
46 | 'Curl' => '\Suricate\Curl', |
||
47 | 'Response' => '\Suricate\Request', |
||
48 | 'Session' => '\Suricate\Session', |
||
49 | 'SessionNative' => '\Suricate\Session\Native', |
||
50 | 'SessionCookie' => '\Suricate\Session\Cookie', |
||
51 | 'SessionMemcache' => '\Suricate\Session\Memcache', |
||
52 | ); |
||
53 | |||
54 | |||
55 | public function __construct($paths = array(), $configFile = null) |
||
56 | { |
||
57 | if ($configFile !== null) { |
||
58 | $this->setConfigFile($configFile); |
||
59 | } |
||
60 | |||
61 | // Load helpers |
||
62 | require_once __DIR__ . DIRECTORY_SEPARATOR . 'Helper.php'; |
||
63 | |||
64 | $this->loadConfig(); |
||
65 | $this->setAppPaths($paths); |
||
66 | |||
67 | if ($this->useAutoloader) { |
||
68 | // Configure autoloader |
||
69 | require_once __DIR__ . DIRECTORY_SEPARATOR . 'AutoLoader.php'; |
||
70 | AutoLoader::register(); |
||
71 | } |
||
72 | |||
73 | // Define error handler |
||
74 | set_exception_handler(array('\Suricate\Error', 'handleException')); |
||
75 | set_error_handler(array('\Suricate\Error', 'handleError')); |
||
76 | register_shutdown_function(array('\Suricate\Error', 'handleShutdownError')); |
||
77 | |||
78 | self::$servicesRepository = new Container(); |
||
79 | |||
80 | $this->initServices(); |
||
81 | } |
||
82 | |||
83 | private function setAppPaths($paths = array()) |
||
91 | /** |
||
92 | * Initialize Framework services |
||
93 | * @return null |
||
94 | */ |
||
95 | private function initServices() |
||
127 | |||
128 | private function setConfigFile($configFile) |
||
140 | /** |
||
141 | * Load framework configuration from ini file |
||
142 | * @return null |
||
143 | */ |
||
144 | private function loadConfig() |
||
179 | |||
180 | private function configureAppMode() |
||
215 | |||
216 | /** |
||
217 | * Default setup template |
||
218 | * @return array setup |
||
219 | */ |
||
220 | private function getDefaultConfig() |
||
233 | |||
234 | public function run() |
||
238 | |||
239 | 1 | public static function __callStatic($name, $arguments) |
|
247 | } |
||
248 |
On one hand,
eval
might be exploited by malicious users if they somehow manage to inject dynamic content. On the other hand, with the emergence of faster PHP runtimes like the HHVM,eval
prevents some optimization that they perform.