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 |
||
17 | class Session implements SessionHandlerInterface |
||
18 | { |
||
19 | const TABLENAME = 'Sessions'; |
||
20 | |||
21 | /** |
||
22 | * @var \Pimple\Container |
||
23 | */ |
||
24 | private $app; |
||
25 | |||
26 | /** |
||
27 | * Starts the session using this handler. |
||
28 | * |
||
29 | * @param Session $handler |
||
30 | * |
||
31 | * @return bool |
||
32 | */ |
||
33 | public static function registerHandler(Session $handler) |
||
37 | |||
38 | /** |
||
39 | * Creates a new session handler. |
||
40 | * |
||
41 | * @param \Pimple\Container $app |
||
42 | */ |
||
43 | public function __construct(Container $app) |
||
47 | |||
48 | /** |
||
49 | * Installs schema for handling sessions in a database. |
||
50 | * |
||
51 | * @return bool success |
||
52 | */ |
||
53 | View Code Duplication | public function install() |
|
61 | |||
62 | /** |
||
63 | * Reads a session. |
||
64 | * |
||
65 | * @param int $id session ID |
||
66 | * |
||
67 | * @return string data |
||
68 | */ |
||
69 | public function read($id) |
||
77 | |||
78 | /** |
||
79 | * Writes a session. |
||
80 | * |
||
81 | * @param int $id session ID |
||
82 | * @param string $data session data |
||
83 | * |
||
84 | * @return bool success |
||
85 | */ |
||
86 | public function write($id, $data) |
||
101 | |||
102 | /** |
||
103 | * Destroys a session. |
||
104 | * |
||
105 | * @param int $id session ID |
||
106 | * |
||
107 | * @return bool success |
||
108 | */ |
||
109 | public function destroy($id) |
||
116 | |||
117 | /** |
||
118 | * Performs garbage collection on sessions. |
||
119 | * |
||
120 | * @param int $max maximum number of seconds a session can live |
||
121 | * |
||
122 | * @return bool success |
||
123 | */ |
||
124 | View Code Duplication | public function gc($max) |
|
134 | |||
135 | /** |
||
136 | * These functions are all noops for various reasons... |
||
137 | * open() and close() have no practical meaning in terms of database connections. |
||
138 | */ |
||
139 | public function open($path, $name) |
||
143 | |||
144 | public function close() |
||
148 | } |
||
149 |