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 |
||
| 19 | class BuilderFactory |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * Creates a namespace builder. |
||
| 23 | * |
||
| 24 | * @param null|string|Node\Name $name Name of the namespace |
||
| 25 | * |
||
| 26 | * @return Builder\Namespace_ The created namespace builder |
||
| 27 | */ |
||
| 28 | protected function _namespace($name) { |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Creates a class builder. |
||
| 34 | * |
||
| 35 | * @param string $name Name of the class |
||
| 36 | * |
||
| 37 | * @return Builder\Class_ The created class builder |
||
| 38 | */ |
||
| 39 | protected function _class($name) { |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Creates an interface builder. |
||
| 45 | * |
||
| 46 | * @param string $name Name of the interface |
||
| 47 | * |
||
| 48 | * @return Builder\Interface_ The created interface builder |
||
| 49 | */ |
||
| 50 | protected function _interface($name) { |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Creates a trait builder. |
||
| 56 | * |
||
| 57 | * @param string $name Name of the trait |
||
| 58 | * |
||
| 59 | * @return Builder\Trait_ The created trait builder |
||
| 60 | */ |
||
| 61 | protected function _trait($name) { |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Creates a method builder. |
||
| 67 | * |
||
| 68 | * @param string $name Name of the method |
||
| 69 | * |
||
| 70 | * @return Builder\Method The created method builder |
||
| 71 | */ |
||
| 72 | public function method($name) { |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Creates a parameter builder. |
||
| 78 | * |
||
| 79 | * @param string $name Name of the parameter |
||
| 80 | * |
||
| 81 | * @return Builder\Param The created parameter builder |
||
| 82 | */ |
||
| 83 | public function param($name) { |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Creates a property builder. |
||
| 89 | * |
||
| 90 | * @param string $name Name of the property |
||
| 91 | * |
||
| 92 | * @return Builder\Property The created property builder |
||
| 93 | */ |
||
| 94 | public function property($name) { |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Creates a function builder. |
||
| 100 | * |
||
| 101 | * @param string $name Name of the function |
||
| 102 | * |
||
| 103 | * @return Builder\Function_ The created function builder |
||
| 104 | */ |
||
| 105 | protected function _function($name) { |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Creates a namespace/class use builder. |
||
| 111 | * |
||
| 112 | * @param string|Node\Name Name to alias |
||
| 113 | * |
||
| 114 | * @return Builder\Use_ The create use builder |
||
| 115 | */ |
||
| 116 | protected function _use($name) { |
||
| 119 | |||
| 120 | View Code Duplication | public function __call($name, array $args) { |
|
| 127 | } |
||
| 128 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.