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 |
||
13 | trait Events { |
||
|
|||
14 | |||
15 | protected static $_listeners = []; |
||
16 | |||
17 | public static function on($name,callable $listener){ |
||
20 | |||
21 | public static function onSingle($name,callable $listener){ |
||
24 | |||
25 | View Code Duplication | public static function off($name,callable $listener = null){ |
|
33 | |||
34 | public static function alias($source,$alias){ |
||
37 | |||
38 | public static function trigger($name, ...$args){ |
||
47 | |||
48 | public static function triggerOnce($name){ |
||
53 | |||
54 | } |
||
55 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.