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 |
||
61 | class NotificationManager |
||
62 | { |
||
63 | |||
64 | /** |
||
65 | * Defaults registered messages. |
||
66 | * |
||
67 | * @var array |
||
68 | */ |
||
69 | protected static $_messages = [ |
||
70 | 'welcome' => 'User\\Notification\\Message\\WelcomeMessage', |
||
71 | 'activated' => 'User\\Notification\\Message\\ActivatedMessage', |
||
72 | 'blocked' => 'User\\Notification\Message\BlockedMessage', |
||
73 | 'cancelRequest' => 'User\\Notification\\Message\\CancelRequestMessage', |
||
74 | 'canceled' => 'User\\Notification\\Message\\CanceledMessage', |
||
75 | 'passwordRequest' => 'User\\Notification\\Message\\PasswordRequestMessage', |
||
76 | ]; |
||
77 | |||
78 | /** |
||
79 | * Magic method for dispatching to message handlers. |
||
80 | * |
||
81 | * @param string $method Name of the method |
||
82 | * @param array $arguments Arguments for the invoked method |
||
83 | * @return mixed |
||
84 | */ |
||
85 | public static function __callStatic($method, $arguments) |
||
115 | |||
116 | /** |
||
117 | * Gets a list of all registered messages. |
||
118 | * |
||
119 | * @return array |
||
120 | */ |
||
121 | public static function messages() |
||
125 | |||
126 | /** |
||
127 | * Looks for variables tags in the given message and replaces with their |
||
128 | * corresponding values. For example, "[site:name] will be replaced with user's |
||
129 | * real name. |
||
130 | * |
||
131 | * @param string $name CamelizedName for when using this message |
||
132 | * @return string|\User\Notification\Message\BaseMessage Message handler. It can |
||
133 | * be either a string for a class name to instance, or a constructed message |
||
134 | * handler object. |
||
135 | */ |
||
136 | public static function addMessage($name, $handler) |
||
140 | } |
||
141 |