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 |
||
33 | class LockManagerGroup { |
||
34 | /** @var LockManagerGroup[] (domain => LockManagerGroup) */ |
||
35 | protected static $instances = []; |
||
36 | |||
37 | protected $domain; // string; domain (usually wiki ID) |
||
38 | |||
39 | /** @var array Array of (name => ('class' => ..., 'config' => ..., 'instance' => ...)) */ |
||
40 | protected $managers = []; |
||
41 | |||
42 | /** |
||
43 | * @param string $domain Domain (usually wiki ID) |
||
44 | */ |
||
45 | protected function __construct( $domain ) { |
||
48 | |||
49 | /** |
||
50 | * @param bool|string $domain Domain (usually wiki ID). Default: false. |
||
51 | * @return LockManagerGroup |
||
52 | */ |
||
53 | View Code Duplication | public static function singleton( $domain = false ) { |
|
62 | |||
63 | /** |
||
64 | * Destroy the singleton instances |
||
65 | */ |
||
66 | public static function destroySingletons() { |
||
69 | |||
70 | /** |
||
71 | * Register lock managers from the global variables |
||
72 | */ |
||
73 | protected function initFromGlobals() { |
||
78 | |||
79 | /** |
||
80 | * Register an array of file lock manager configurations |
||
81 | * |
||
82 | * @param array $configs |
||
83 | * @throws Exception |
||
84 | */ |
||
85 | protected function register( array $configs ) { |
||
104 | |||
105 | /** |
||
106 | * Get the lock manager object with a given name |
||
107 | * |
||
108 | * @param string $name |
||
109 | * @return LockManager |
||
110 | * @throws Exception |
||
111 | */ |
||
112 | public function get( $name ) { |
||
135 | |||
136 | /** |
||
137 | * Get the config array for a lock manager object with a given name |
||
138 | * |
||
139 | * @param string $name |
||
140 | * @return array |
||
141 | * @throws Exception |
||
142 | */ |
||
143 | public function config( $name ) { |
||
151 | |||
152 | /** |
||
153 | * Get the default lock manager configured for the site. |
||
154 | * Returns NullLockManager if no lock manager could be found. |
||
155 | * |
||
156 | * @return LockManager |
||
157 | */ |
||
158 | public function getDefault() { |
||
163 | |||
164 | /** |
||
165 | * Get the default lock manager configured for the site |
||
166 | * or at least some other effective configured lock manager. |
||
167 | * Throws an exception if no lock manager could be found. |
||
168 | * |
||
169 | * @return LockManager |
||
170 | * @throws Exception |
||
171 | */ |
||
172 | public function getAny() { |
||
177 | } |
||
178 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.