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:
Complex classes like YamlDumper often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use YamlDumper, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 30 | class YamlDumper extends Dumper |
||
| 31 | { |
||
| 32 | private $dumper; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Dumps the service container as an YAML string. |
||
| 36 | * |
||
| 37 | * @param array $options An array of options |
||
| 38 | * |
||
| 39 | * @return string A YAML string representing of the service container |
||
| 40 | * |
||
| 41 | * @api |
||
| 42 | */ |
||
| 43 | public function dump(array $options = array()) |
||
| 44 | { |
||
| 45 | if (!class_exists('Symfony\Component\Yaml\Dumper')) { |
||
| 46 | throw new RuntimeException('Unable to dump the container as the Symfony Yaml Component is not installed.'); |
||
| 47 | } |
||
| 48 | |||
| 49 | if (null === $this->dumper) { |
||
| 50 | $this->dumper = new YmlDumper(); |
||
| 51 | } |
||
| 52 | |||
| 53 | return $this->addParameters()."\n".$this->addServices(); |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Adds a service. |
||
| 58 | * |
||
| 59 | * @param string $id |
||
| 60 | * @param Definition $definition |
||
| 61 | * |
||
| 62 | * @return string |
||
| 63 | */ |
||
| 64 | private function addService($id, $definition) |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Adds a service alias. |
||
| 156 | * |
||
| 157 | * @param string $alias |
||
| 158 | * @param Alias $id |
||
| 159 | * |
||
| 160 | * @return string |
||
| 161 | */ |
||
| 162 | private function addServiceAlias($alias, $id) |
||
| 163 | { |
||
| 164 | if ($id->isPublic()) { |
||
| 165 | return sprintf(" %s: @%s\n", $alias, $id); |
||
| 166 | } else { |
||
| 167 | return sprintf(" %s:\n alias: %s\n public: false", $alias, $id); |
||
| 168 | } |
||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Adds services. |
||
| 173 | * |
||
| 174 | * @return string |
||
| 175 | */ |
||
| 176 | private function addServices() |
||
| 177 | { |
||
| 178 | if (!$this->container->getDefinitions()) { |
||
| 179 | return ''; |
||
| 180 | } |
||
| 181 | |||
| 182 | $code = "services:\n"; |
||
| 183 | foreach ($this->container->getDefinitions() as $id => $definition) { |
||
| 184 | $code .= $this->addService($id, $definition); |
||
| 185 | } |
||
| 186 | |||
| 187 | $aliases = $this->container->getAliases(); |
||
| 188 | View Code Duplication | foreach ($aliases as $alias => $id) { |
|
| 189 | while (isset($aliases[(string) $id])) { |
||
| 190 | $id = $aliases[(string) $id]; |
||
| 191 | } |
||
| 192 | $code .= $this->addServiceAlias($alias, $id); |
||
| 193 | } |
||
| 194 | |||
| 195 | return $code; |
||
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Adds parameters. |
||
| 200 | * |
||
| 201 | * @return string |
||
| 202 | */ |
||
| 203 | private function addParameters() |
||
| 204 | { |
||
| 205 | if (!$this->container->getParameterBag()->all()) { |
||
| 206 | return ''; |
||
| 207 | } |
||
| 208 | |||
| 209 | $parameters = $this->prepareParameters($this->container->getParameterBag()->all(), $this->container->isFrozen()); |
||
| 210 | |||
| 211 | return $this->dumper->dump(array('parameters' => $parameters), 2); |
||
| 212 | } |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Dumps callable to YAML format |
||
| 216 | * |
||
| 217 | * @param callable $callable |
||
| 218 | * |
||
| 219 | * @return callable |
||
| 220 | */ |
||
| 221 | private function dumpCallable($callable) |
||
| 222 | { |
||
| 223 | if (is_array($callable)) { |
||
| 224 | if ($callable[0] instanceof Reference) { |
||
| 225 | $callable = array($this->getServiceCall((string) $callable[0], $callable[0]), $callable[1]); |
||
| 226 | } else { |
||
| 227 | $callable = array($callable[0], $callable[1]); |
||
| 228 | } |
||
| 229 | } |
||
| 230 | |||
| 231 | return $callable; |
||
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Dumps the value to YAML format. |
||
| 236 | * |
||
| 237 | * @param mixed $value |
||
| 238 | * |
||
| 239 | * @return mixed |
||
| 240 | * |
||
| 241 | * @throws RuntimeException When trying to dump object or resource |
||
| 242 | */ |
||
| 243 | private function dumpValue($value) |
||
| 244 | { |
||
| 245 | if (is_array($value)) { |
||
| 246 | $code = array(); |
||
| 247 | foreach ($value as $k => $v) { |
||
| 248 | $code[$k] = $this->dumpValue($v); |
||
| 249 | } |
||
| 250 | |||
| 251 | return $code; |
||
| 252 | } elseif ($value instanceof Reference) { |
||
| 253 | return $this->getServiceCall((string) $value, $value); |
||
| 254 | } elseif ($value instanceof Parameter) { |
||
| 255 | return $this->getParameterCall((string) $value); |
||
| 256 | } elseif ($value instanceof Expression) { |
||
| 257 | return $this->getExpressionCall((string) $value); |
||
| 258 | } elseif (is_object($value) || is_resource($value)) { |
||
| 259 | throw new RuntimeException('Unable to dump a service container if a parameter is an object or a resource.'); |
||
| 260 | } |
||
| 261 | |||
| 262 | return $value; |
||
| 263 | } |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Gets the service call. |
||
| 267 | * |
||
| 268 | * @param string $id |
||
| 269 | * @param Reference $reference |
||
| 270 | * |
||
| 271 | * @return string |
||
| 272 | */ |
||
| 273 | View Code Duplication | private function getServiceCall($id, Reference $reference = null) |
|
| 274 | { |
||
| 275 | if (null !== $reference && ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE !== $reference->getInvalidBehavior()) { |
||
| 276 | return sprintf('@?%s', $id); |
||
| 277 | } |
||
| 278 | |||
| 279 | return sprintf('@%s', $id); |
||
| 280 | } |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Gets parameter call. |
||
| 284 | * |
||
| 285 | * @param string $id |
||
| 286 | * |
||
| 287 | * @return string |
||
| 288 | */ |
||
| 289 | private function getParameterCall($id) |
||
| 293 | |||
| 294 | private function getExpressionCall($expression) |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Prepares parameters. |
||
| 301 | * |
||
| 302 | * @param array $parameters |
||
| 303 | * @param bool $escape |
||
| 304 | * |
||
| 305 | * @return array |
||
| 306 | */ |
||
| 307 | private function prepareParameters($parameters, $escape = true) |
||
| 308 | { |
||
| 309 | $filtered = array(); |
||
| 310 | foreach ($parameters as $key => $value) { |
||
| 311 | if (is_array($value)) { |
||
| 312 | $value = $this->prepareParameters($value, $escape); |
||
| 313 | } elseif ($value instanceof Reference || is_string($value) && 0 === strpos($value, '@')) { |
||
| 314 | $value = '@'.$value; |
||
| 315 | } |
||
| 316 | |||
| 317 | $filtered[$key] = $value; |
||
| 318 | } |
||
| 319 | |||
| 320 | return $escape ? $this->escape($filtered) : $filtered; |
||
| 321 | } |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Escapes arguments. |
||
| 325 | * |
||
| 326 | * @param array $arguments |
||
| 327 | * |
||
| 328 | * @return array |
||
| 329 | */ |
||
| 330 | View Code Duplication | private function escape($arguments) |
|
| 345 | } |
||
| 346 |
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: