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 |
||
25 | class ConverterClient extends Client |
||
26 | { |
||
27 | protected $converter; |
||
28 | protected $name; |
||
29 | |||
30 | /** |
||
31 | * __construct |
||
32 | * |
||
33 | * Wrap the given converter. |
||
34 | * |
||
35 | * @access public |
||
36 | * @param string $name |
||
37 | * @param ConverterInterface $converter |
||
38 | */ |
||
39 | public function __construct($name, ConverterInterface $converter) |
||
40 | { |
||
41 | $this->name = $name; |
||
42 | $this->converter = $converter; |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * getClientType |
||
47 | * |
||
48 | * @see ClientInterface |
||
49 | */ |
||
50 | public function getClientType() |
||
51 | { |
||
52 | return 'converter'; |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * getClientIdentifier |
||
57 | * |
||
58 | * @see ClientInterface |
||
59 | */ |
||
60 | public function getClientIdentifier() |
||
61 | { |
||
62 | return $this->name; |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * toPg |
||
67 | * |
||
68 | * Trigger converter's toPg conversion method. |
||
69 | * |
||
70 | * @access public |
||
71 | * @param mixed $value |
||
72 | * @param string $type |
||
73 | * @return string |
||
74 | * @see ConverterInterface |
||
75 | */ |
||
76 | View Code Duplication | public function toPg($value, $type = null) |
|
77 | { |
||
78 | return $this->converter->toPg( |
||
79 | $value, |
||
80 | $type === null ? $this->getClientIdentifier() : $type, |
||
81 | $this->getSession() |
||
82 | ); |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * fromPg |
||
87 | * |
||
88 | * Trigger converter's fromPg conversion method. |
||
89 | * |
||
90 | * @access public |
||
91 | * @param mixed $value |
||
92 | * @param string $type |
||
93 | * @return mixed |
||
94 | * @see ConverterInterface |
||
95 | */ |
||
96 | View Code Duplication | public function fromPg($value, $type = null) |
|
97 | { |
||
98 | return $this->converter->fromPg( |
||
99 | $value, |
||
100 | $type === null ? $this->getClientIdentifier() : $type, |
||
101 | $this->getSession() |
||
102 | ); |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * toPgStandardFormat |
||
107 | * |
||
108 | * Export data as CSV representation |
||
109 | * |
||
110 | * @access public |
||
111 | * @param mixed $value |
||
112 | * @param string $type |
||
113 | * @return string |
||
114 | * @see ConverterInterface |
||
115 | */ |
||
116 | View Code Duplication | public function toPgStandardFormat($value, $type = null) |
|
124 | |||
125 | /** |
||
126 | * getConverter |
||
127 | * |
||
128 | * Return the embedded converter. |
||
129 | * |
||
130 | * @access public |
||
131 | * @return ConverterInterface |
||
132 | */ |
||
133 | public function getConverter() |
||
137 | } |
||
138 |