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 |
||
| 20 | class LocalizedString implements \JsonSerializable, JsonDeserializeInterface |
||
| 21 | { |
||
| 22 | use ContextTrait; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var array |
||
| 26 | */ |
||
| 27 | protected $values = []; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @param array $values |
||
| 31 | * @param Context|callable $context |
||
| 32 | */ |
||
| 33 | 229 | public function __construct(array $values, $context = null) |
|
| 40 | |||
| 41 | /** |
||
| 42 | * @param $locale |
||
| 43 | * @return string |
||
| 44 | */ |
||
| 45 | 48 | public function __get($locale) |
|
| 51 | |||
| 52 | /** |
||
| 53 | * @param Context $context |
||
| 54 | * @return string |
||
| 55 | */ |
||
| 56 | 2 | public function getLocalized(Context $context = null) |
|
| 60 | /** |
||
| 61 | * @param Context $context |
||
| 62 | * @return string |
||
| 63 | */ |
||
| 64 | 85 | protected function getLanguage(Context $context) |
|
| 82 | |||
| 83 | /** |
||
| 84 | * @param Context $context |
||
| 85 | * @return string |
||
| 86 | */ |
||
| 87 | 85 | View Code Duplication | public function get(Context $context = null) |
| 101 | |||
| 102 | /** |
||
| 103 | * @param string $locale |
||
| 104 | * @param string $value |
||
| 105 | * @return $this |
||
| 106 | */ |
||
| 107 | 229 | public function add($locale, $value) |
|
| 114 | |||
| 115 | 8 | public function merge(LocalizedString $localizedString) |
|
| 119 | |||
| 120 | 17 | public function __toString() |
|
| 124 | |||
| 125 | /** |
||
| 126 | * @return array |
||
| 127 | */ |
||
| 128 | 179 | public function toArray() |
|
| 138 | |||
| 139 | /** |
||
| 140 | * @return array |
||
| 141 | */ |
||
| 142 | 169 | public function jsonSerialize() |
|
| 146 | |||
| 147 | /** |
||
| 148 | * @param Context|callable $context |
||
| 149 | * @return $this |
||
| 150 | */ |
||
| 151 | public static function of($context = null) |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @param array $data |
||
| 158 | * @param Context|callable $context |
||
| 159 | * @return static |
||
| 160 | */ |
||
| 161 | 108 | public static function fromArray(array $data, $context = null) |
|
| 165 | |||
| 166 | /** |
||
| 167 | * @param string $language |
||
| 168 | * @param string $text |
||
| 169 | * @param Context|callable $context |
||
| 170 | * @return LocalizedString |
||
| 171 | */ |
||
| 172 | 147 | public static function ofLangAndText($language, $text, $context = null) |
|
| 176 | } |
||
| 177 |
PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.
Let’s take a look at an example:
If we look at the
getEmail()method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:On the hand, if we look at the
setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call: