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 |
||
| 34 | class UpstreamXmlConfiguration implements UpstreamConfigurationInterface |
||
| 35 | { |
||
| 36 | /** |
||
| 37 | * Holds the name of the upstream node |
||
| 38 | * |
||
| 39 | * @var string |
||
| 40 | */ |
||
| 41 | public $name; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Holds the type of the upstream node |
||
| 45 | * |
||
| 46 | * @var string |
||
| 47 | */ |
||
| 48 | public $type; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Holds the servers for the upstream node |
||
| 52 | * |
||
| 53 | * @var array |
||
| 54 | */ |
||
| 55 | public $servers; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Constructs config |
||
| 59 | * |
||
| 60 | * @param \SimpleXMLElement $node The simple xml element used to build config |
||
| 61 | */ |
||
| 62 | public function __construct(\SimpleXMLElement $node) |
||
| 63 | { |
||
| 64 | // prepare properties |
||
| 65 | $this->name = (string)$node->attributes()->name; |
||
| 66 | $this->type = (string)$node->attributes()->type; |
||
| 67 | |||
| 68 | if (isset($node->attributes()->channel)) { |
||
| 69 | $this->channel = (string)$node->attributes()->channel; |
||
|
|
|||
| 70 | } |
||
| 71 | |||
| 72 | // prepare handlers |
||
| 73 | $this->servers = $this->prepareServers($node); |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Prepares server nodes configured for upstream |
||
| 78 | * |
||
| 79 | * @param \SimpleXMLElement $node The xml node for servers to prepare |
||
| 80 | * |
||
| 81 | * @return array |
||
| 82 | */ |
||
| 83 | protected function prepareServers(\SimpleXMLElement $node) |
||
| 84 | { |
||
| 85 | $servers = array(); |
||
| 86 | foreach ($node->servers->server as $serverNode) { |
||
| 87 | $name = (string)$serverNode->attributes()->name; |
||
| 88 | $type = (string)$serverNode->attributes()->type; |
||
| 89 | $params = array(); |
||
| 90 | View Code Duplication | foreach ($serverNode->params->param as $paramNode) { |
|
| 91 | $paramName = (string)$paramNode->attributes()->name; |
||
| 92 | $paramType = (string)$paramNode->attributes()->type; |
||
| 93 | $paramValue = (string)$paramNode; |
||
| 94 | // check if type boolen and transform true and false strings to int |
||
| 95 | if ($paramType === 'boolean') { |
||
| 96 | $paramValue = str_replace(array('true', 'false', '1', '0'), array(1,0,1,0 ), $paramValue); |
||
| 97 | } |
||
| 98 | // set correct value type |
||
| 99 | settype($paramValue, $paramType); |
||
| 100 | $params[$paramName] = $paramValue; |
||
| 101 | } |
||
| 102 | $servers[$name] = array( |
||
| 103 | 'name' => $name, |
||
| 104 | 'type' => $type, |
||
| 105 | 'params' => $params |
||
| 106 | ); |
||
| 107 | } |
||
| 108 | return $servers; |
||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Returns name |
||
| 113 | * |
||
| 114 | * @return string |
||
| 115 | */ |
||
| 116 | public function getName() |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Returns type |
||
| 123 | * |
||
| 124 | * @return string |
||
| 125 | */ |
||
| 126 | public function getType() |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Returns servers |
||
| 133 | * |
||
| 134 | * @return array |
||
| 135 | */ |
||
| 136 | public function getServers() |
||
| 140 | } |
||
| 141 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: