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 |
||
| 10 | class YamlEncode implements EncoderInterface |
||
| 11 | { |
||
| 12 | const OPTION_OBJECT = 'yaml_encode_object'; |
||
| 13 | const OPTION_EXCEPTION_ON_INVALID_TYPE = 'yaml_encode_exception_on_invalid_type'; |
||
| 14 | const OPTION_OBJECT_FOR_MAP = 'yaml_encode_object_for_map'; |
||
| 15 | const OPTION_MULTI_LINE_LITERAL_BLOCK = 'yaml_encode_multi_line_literal_block'; |
||
| 16 | const OPTION_INLINE = 'yaml_encode_inline'; |
||
| 17 | const OPTION_INDENT = 'yaml_encode_indent'; |
||
| 18 | const SUPPORTED_ENCODING_YAML = 'yaml'; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var bool |
||
| 22 | */ |
||
| 23 | private $multiLineLiteralBlock; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var bool |
||
| 27 | */ |
||
| 28 | private $exceptionOnInvalidType; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var bool |
||
| 32 | */ |
||
| 33 | private $objectForMap; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var bool |
||
| 37 | */ |
||
| 38 | private $object; |
||
| 39 | /** |
||
| 40 | * @var int |
||
| 41 | */ |
||
| 42 | private $indent; |
||
| 43 | /** |
||
| 44 | * @var int |
||
| 45 | */ |
||
| 46 | private $inline; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Constructs a new YamlDecode instance. |
||
| 50 | * |
||
| 51 | * @param bool $object |
||
| 52 | * @param bool $exceptionOnInvalidType |
||
| 53 | * @param bool $objectForMap |
||
| 54 | * @param bool $multiLineLiteralBlock |
||
| 55 | * @param int $inline |
||
| 56 | * @param int $indent |
||
| 57 | */ |
||
| 58 | public function __construct( |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Encodes data into the given format. |
||
| 77 | * |
||
| 78 | * The only supported is yaml |
||
| 79 | * |
||
| 80 | * @param mixed $data Data to encode |
||
| 81 | * @param string $format Format name |
||
| 82 | * @param array $context options that normalizers/encoders have access to |
||
| 83 | * |
||
| 84 | * @return string |
||
| 85 | * |
||
| 86 | * @throws UnexpectedValueException |
||
| 87 | */ |
||
| 88 | public function encode($data, $format, array $context = []) |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Merges the default options of the Yaml Encoder with the passed context. |
||
| 117 | * |
||
| 118 | * @param array $context |
||
| 119 | * |
||
| 120 | * @return array |
||
| 121 | */ |
||
| 122 | private function resolveContext(array $context) |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Convert the context to options understood by the parser |
||
| 138 | * |
||
| 139 | * @param array $options |
||
| 140 | * |
||
| 141 | * @return int |
||
| 142 | */ |
||
| 143 | View Code Duplication | private function contextToOptions(array $options) |
|
| 162 | |||
| 163 | |||
| 164 | /** |
||
| 165 | * Checks whether the serializer can encode to given format. |
||
| 166 | * |
||
| 167 | * The only supported format is yaml |
||
| 168 | * |
||
| 169 | * @param string $format format name |
||
| 170 | * |
||
| 171 | * @return bool |
||
| 172 | */ |
||
| 173 | public function supportsEncoding($format) |
||
| 177 | |||
| 178 | private function isYamlOldStyleInterface() |
||
| 182 | } |
||
| 183 |
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.