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 |
||
37 | View Code Duplication | abstract class LoggerAppender extends ParameterHolder |
|
|
|||
38 | { |
||
39 | /** |
||
40 | * @var Context An Context instance. |
||
41 | */ |
||
42 | protected $context = null; |
||
43 | |||
44 | /** |
||
45 | * @var LoggerLayout An LoggerLayout instance. |
||
46 | */ |
||
47 | protected $layout = null; |
||
48 | |||
49 | /** |
||
50 | * Initialize the object. |
||
51 | * |
||
52 | * @param Context $context A Context instance. |
||
53 | * @param array $parameters An associative array of initialization parameters. |
||
54 | * |
||
55 | * @author Bob Zoller <[email protected]> |
||
56 | * @author David Zülke <[email protected]> |
||
57 | * @since 0.10.0 |
||
58 | */ |
||
59 | public function initialize(Context $context, array $parameters = array()) |
||
65 | |||
66 | /** |
||
67 | * Retrieve the current application context. |
||
68 | * |
||
69 | * @return Context A Context instance. |
||
70 | * |
||
71 | * @author Sean Kerr <[email protected]> |
||
72 | * @since 0.10.0 |
||
73 | */ |
||
74 | final public function getContext() |
||
78 | |||
79 | /** |
||
80 | * Retrieve the layout. |
||
81 | * |
||
82 | * @return LoggerLayout A Layout instance, if it has been set, |
||
83 | * otherwise null. |
||
84 | * |
||
85 | * @author Sean Kerr <[email protected]> |
||
86 | * @since 0.9.0 |
||
87 | */ |
||
88 | public function getLayout() |
||
92 | |||
93 | /** |
||
94 | * Set the layout. |
||
95 | * |
||
96 | * @param LoggerLayout $layout A Layout instance. |
||
97 | * |
||
98 | * @return LoggerAppender |
||
99 | * |
||
100 | * @author Sean Kerr <[email protected]> |
||
101 | * @since 0.9.0 |
||
102 | */ |
||
103 | public function setLayout(LoggerLayout $layout) |
||
108 | |||
109 | /** |
||
110 | * Execute the shutdown procedure. |
||
111 | * |
||
112 | * @author Sean Kerr <[email protected]> |
||
113 | * @since 0.9.0 |
||
114 | */ |
||
115 | abstract function shutdown(); |
||
116 | |||
117 | /** |
||
118 | * Write log data to this appender. |
||
119 | * |
||
120 | * @param LoggerMessage $message Log data to be written. |
||
121 | * |
||
122 | * @author Sean Kerr <[email protected]> |
||
123 | * @since 0.9.0 |
||
124 | */ |
||
125 | abstract function write(LoggerMessage $message); |
||
126 | } |
||
127 |
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.