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 | ||
| 8 | final class DecoderManager | ||
| 9 | { | ||
| 10 | /** | ||
| 11 | * @var ParserInterface[] | ||
| 12 | */ | ||
| 13 | private $decoders = []; | ||
| 14 | |||
| 15 | /** | ||
| 16 | * @param DecoderInterface[] $decoders | ||
| 17 | * @param string $defaultType | ||
| 18 | */ | ||
| 19 | 118 | public function __construct(array $decoders = [], $defaultType = null) | |
| 38 | |||
| 39 | /** | ||
| 40 | * Register a DecoderInterface for the given MIME-types. | ||
| 41 | * | ||
| 42 | * @param string $type | ||
| 43 | * @param DecoderInterface $decoder | ||
| 44 | */ | ||
| 45 | 118 | public function registerDecoder($type, DecoderInterface $decoder = null) | |
| 53 | |||
| 54 | /** | ||
| 55 | * Get all registered decoders, keyed by the extensions they are registered to decode schemas for. | ||
| 56 | * | ||
| 57 | * @return DecoderInterface[] | ||
| 58 | */ | ||
| 59 | 26 | public function getDecoders() | |
| 63 | |||
| 64 | /** | ||
| 65 | * Set the default type for unknown files | ||
| 66 | * | ||
| 67 | * @param string defaultType | ||
| 68 | */ | ||
| 69 | 108 | public function setDefaultType($defaultType = null) | |
| 73 | |||
| 74 | /** | ||
| 75 | * Get the decoder for the given extension. | ||
| 76 | * | ||
| 77 | * @param string $type | ||
| 78 | * | ||
| 79 | * @return DecoderInterface | ||
| 80 | * @throws \InvalidArgumentException | ||
| 81 | */ | ||
| 82 | 116 | public function getDecoder($type) | |
| 96 | |||
| 97 | /** | ||
| 98 | * @param string $type | ||
| 99 | * | ||
| 100 | * @return bool | ||
| 101 | */ | ||
| 102 | 118 | public function hasDecoder($type) | |
| 106 | |||
| 107 | /** | ||
| 108 | * Register the default decoders | ||
| 109 | */ | ||
| 110 | 108 | private function registerDefaultDecoders() | |
| 114 | |||
| 115 | /** | ||
| 116 | * @param DecoderInterface $decoder | ||
| 117 | */ | ||
| 118 | 108 | View Code Duplication | public function registerJsonDecoder(DecoderInterface $decoder = null) | 
| 127 | |||
| 128 | /** | ||
| 129 | * @param DecoderInterface $decoder | ||
| 130 | */ | ||
| 131 | 2 | View Code Duplication | public function registerYamlDecoder(DecoderInterface $decoder = null) | 
| 141 | } | ||
| 142 | 
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: