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 |
||
| 7 | class XmlExtractor implements ExtractorInterface |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * Path to the XML file. |
||
| 11 | * @var array |
||
| 12 | */ |
||
| 13 | protected $file; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Parsed results |
||
| 17 | * @var \SimpleXMLElement|null |
||
| 18 | */ |
||
| 19 | protected $config; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Was config parsed? |
||
| 23 | * @var bool |
||
| 24 | */ |
||
| 25 | protected $isConfigParsed; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @param array $options |
||
| 29 | */ |
||
| 30 | 10 | View Code Duplication | public function __construct(array $options) |
| 42 | |||
| 43 | /** |
||
| 44 | * {@inheritdoc} |
||
| 45 | */ |
||
| 46 | 3 | public function getConstructorInjections($fqcn) |
|
| 47 | { |
||
| 48 | 3 | $this->isConfigParsed($fqcn); |
|
| 49 | |||
| 50 | 3 | if (empty($this->config)) { |
|
| 51 | 1 | return null; |
|
| 52 | } |
||
| 53 | |||
| 54 | 2 | $values = []; |
|
| 55 | 2 | foreach ($this->config->xpath('constructor/inject') as $spec) { |
|
| 56 | 1 | $values[] = $this->createInjectionObject($spec); |
|
| 57 | 2 | } |
|
| 58 | |||
| 59 | 2 | if (!count($values)) { |
|
| 60 | 1 | return null; |
|
| 61 | } |
||
| 62 | |||
| 63 | 1 | $injections = new InjectParams(); |
|
| 64 | 1 | $injections->value = $values; |
|
| 65 | |||
| 66 | 1 | return $injections; |
|
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * {@inheritdoc} |
||
| 71 | */ |
||
| 72 | 3 | public function getMethodsInjections($fqcn) |
|
| 92 | |||
| 93 | /** |
||
| 94 | * {@inheritdoc} |
||
| 95 | */ |
||
| 96 | 3 | public function getPropertiesInjections($fqcn) |
|
| 97 | { |
||
| 98 | 3 | $this->isConfigParsed($fqcn); |
|
| 99 | |||
| 100 | 3 | if (empty($this->config)) { |
|
| 101 | 1 | return []; |
|
| 102 | } |
||
| 103 | |||
| 104 | 2 | $injections = []; |
|
| 105 | 2 | foreach ($this->config->xpath('properties/property') as $spec) { |
|
| 106 | 1 | $injections[(string)$spec['name']] = $this->createInjectionObject($spec->xpath('inject')[0]); |
|
| 107 | 2 | } |
|
| 108 | |||
| 109 | 2 | return $injections; |
|
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * {@inheritdoc} |
||
| 114 | */ |
||
| 115 | 1 | public function getChangeSet($fqcn) |
|
| 116 | { |
||
| 117 | 1 | $this->isConfigParsed($fqcn); |
|
| 118 | |||
| 119 | 1 | if (!empty((string)$this->config['fqcn'])) { |
|
| 120 | $fqcn = (string)$this->config['fqcn']; |
||
| 121 | } |
||
| 122 | |||
| 123 | 1 | return new ChangeSet($this, $fqcn); |
|
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @return array |
||
| 128 | */ |
||
| 129 | 1 | public function getFile() |
|
| 133 | |||
| 134 | /** |
||
| 135 | * Parse the XML. |
||
| 136 | * @param string $fqcn |
||
| 137 | */ |
||
| 138 | 6 | private function isConfigParsed($fqcn) |
|
| 155 | |||
| 156 | /** |
||
| 157 | * @param \SimpleXMLElement $spec |
||
| 158 | * @return AnnotationInterface |
||
| 159 | */ |
||
| 160 | 3 | private function createInjectionObject(\SimpleXMLElement $spec) |
|
| 161 | { |
||
| 162 | 3 | $injectionFqcn = (string)$spec['type']; |
|
| 163 | 3 | $injection = new $injectionFqcn; |
|
| 164 | |||
| 165 | 3 | foreach ($spec->xpath('param') as $param) { |
|
| 166 | 3 | $this->createProperty($injection, $param); |
|
| 167 | 3 | } |
|
| 168 | |||
| 169 | 3 | return $injection; |
|
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @param object $injection |
||
| 174 | * @param \SimpleXMLElement $param |
||
| 175 | */ |
||
| 176 | 3 | private function createProperty($injection, $param) |
|
| 189 | } |
||
| 190 |
According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.
}
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.