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 |
||
12 | class Notifier extends Component { |
||
13 | protected $config; |
||
14 | protected $log; |
||
15 | protected $messageCb; |
||
16 | protected $subjectCb; |
||
17 | |||
18 | /** |
||
19 | * You can provide a Monolog Logger instance to use in the constructor |
||
20 | * @param Logger|null $log Logger instance to use |
||
21 | 54 | */ |
|
22 | 54 | public function __construct(Logger $log = null) { |
|
30 | |||
31 | /** |
||
32 | * Transforms a value into a closure that returns itself when called |
||
33 | * @param callable|string $cb The value that you want to wrap in a closure |
||
34 | * @return callable |
||
35 | 12 | */ |
|
36 | 12 | private function wrapValueInClosure($cb) { |
|
43 | |||
44 | /** |
||
45 | * Set a string or a closure to be called that will generate the message body for the notification |
||
46 | * @param callable|string $cb A closure or string that will be set for the message |
||
47 | 6 | */ |
|
48 | public function setMessage($cb) |
||
53 | |||
54 | /** |
||
55 | * Returns the result of the message closure |
||
56 | * @param Exception $e The Exception instance that you want to build the message around |
||
57 | * @return string The message string |
||
58 | 15 | */ |
|
59 | 15 | public function getMessage(Exception $e) { |
|
70 | |||
71 | /** |
||
72 | * Set a string or a closure to be called that will generate the subject line for the notification |
||
73 | * @param callable|string $cb A closure or string that will be set for the subject line |
||
74 | 6 | */ |
|
75 | public function setSubject($cb) |
||
80 | |||
81 | /** |
||
82 | * Returns the result of the subject closure |
||
83 | * @param Exception $e The Exception instance that you want to build the subject around |
||
84 | * @return string The subject string |
||
85 | 15 | */ |
|
86 | 15 | public function getSubject(Exception $e) { |
|
93 | |||
94 | /** |
||
95 | * Pushes on another Monolog Handler |
||
96 | * @param HandlerInterface $handler The handler instance to add on |
||
97 | * @return Notifier Returns this |
||
98 | 6 | */ |
|
99 | 6 | public function pushHandler(HandlerInterface $handler) { |
|
103 | |||
104 | /** |
||
105 | * Triggers the Monolog Logger instance to log an error to all handlers |
||
106 | * @param Exception $e The exception to use |
||
107 | * @param array $context Additional information that you would like to pass to Monolog |
||
108 | * @return bool |
||
109 | 12 | */ |
|
110 | public function send(Exception $e, array $context = []) { |
||
132 | |||
133 | /** |
||
134 | * Builds a context array to pass to Monolog |
||
135 | * |
||
136 | * @param array $context Additional information that you would like to pass to Monolog |
||
137 | 9 | * @param \Exception $e |
|
138 | 9 | * |
|
139 | 9 | * @return array The modified context array |
|
140 | 9 | */ |
|
141 | 9 | protected function buildContext(array $context = [], Exception $e) |
|
241 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.