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 |
||
9 | class YamlDecode implements DecoderInterface |
||
10 | { |
||
11 | const OPTION_EXCEPTION_ON_INVALID_TYPE = 'yaml_decode_exception_on_invalid_type'; |
||
12 | const OPTION_OBJECT = 'yaml_decode_object'; |
||
13 | const OPTION_OBJECT_FOR_MAP = 'yaml_decode_object_for_map'; |
||
14 | const OPTION_DATE_TIME = 'yaml_decode_date_time'; |
||
15 | const SUPPORTED_ENCODING_YAML = "yaml"; |
||
16 | /** |
||
17 | * @var bool |
||
18 | */ |
||
19 | private $exceptionOnInvalidType; |
||
20 | /** |
||
21 | * @var bool |
||
22 | */ |
||
23 | private $object; |
||
24 | /** |
||
25 | * @var bool |
||
26 | */ |
||
27 | private $objectForMap; |
||
28 | /** |
||
29 | * @var bool |
||
30 | */ |
||
31 | private $dateTime; |
||
32 | |||
33 | /** |
||
34 | * Constructs a new YamlDecode instance. |
||
35 | * |
||
36 | * @param bool $exceptionOnInvalidType |
||
37 | * @param bool $object |
||
38 | * @param bool $objectForMap |
||
39 | * @param bool $dateTime |
||
40 | */ |
||
41 | public function __construct( |
||
52 | |||
53 | /** |
||
54 | * Decodes a string into PHP data. |
||
55 | * |
||
56 | * @param string $data Data to decode |
||
57 | * @param string $format Format name |
||
58 | * @param array $context options that decoders have access to |
||
59 | * |
||
60 | * The format parameter specifies which format the data is in; valid values |
||
61 | * depend on the specific implementation. The only format we support is 'yaml' |
||
62 | * |
||
63 | * @return mixed |
||
64 | * |
||
65 | * @throws UnexpectedValueException |
||
66 | */ |
||
67 | public function decode($data, $format, array $context = []) |
||
86 | |||
87 | /** |
||
88 | * Checks whether the deserializer can decode from given format. |
||
89 | * |
||
90 | * We only support yaml. |
||
91 | * |
||
92 | * @param string $format format name |
||
93 | * |
||
94 | * @return bool |
||
95 | */ |
||
96 | public function supportsDecoding($format) |
||
100 | |||
101 | /** |
||
102 | * Merges the default options of the Yaml Decoder with the passed context. |
||
103 | * |
||
104 | * @param array $context |
||
105 | * |
||
106 | * @return array |
||
107 | */ |
||
108 | private function resolveContext(array $context) |
||
119 | |||
120 | /** |
||
121 | * Convert the context to options understood by the parser |
||
122 | * |
||
123 | * @param array $options |
||
124 | * |
||
125 | * @return int |
||
126 | */ |
||
127 | View Code Duplication | private function contextToOptions(array $options) |
|
146 | |||
147 | private function isYamlOldStyleInterface() |
||
151 | } |
||
152 |
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.