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 |
||
| 11 | class RulerZ |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * @var array<CompilationTarget> $compilationTargets |
||
| 15 | */ |
||
| 16 | private $compilationTargets = []; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Constructor. |
||
| 20 | * |
||
| 21 | * @param Compiler $compiler The compiler. |
||
| 22 | * @param array $compilationTargets A list of target compilers, each one handles a specific target (an array, a DoctrineQueryBuilder, ...) |
||
| 23 | */ |
||
| 24 | public function __construct(Compiler $compiler, array $compilationTargets = []) |
||
| 25 | { |
||
| 26 | $this->compiler = $compiler; |
||
| 27 | |||
| 28 | foreach ($compilationTargets as $targetCompiler) { |
||
| 29 | $this->registerCompilationTarget($targetCompiler); |
||
| 30 | } |
||
| 31 | } |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Registers a new target compiler. |
||
| 35 | * |
||
| 36 | * @param CompilationTarget $compilationTarget The target compiler to register. |
||
| 37 | */ |
||
| 38 | public function registerCompilationTarget(CompilationTarget $compilationTarget) |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Apply the filters on the target using the given rule and parameters. |
||
| 45 | * The target compiler to use is determined at runtime using the registered ones. |
||
| 46 | * |
||
| 47 | * @param mixed $target The target to filter. |
||
| 48 | * @param string $rule The rule to apply. |
||
| 49 | * @param array $parameters The parameters used in the rule. |
||
| 50 | * @param array $executionContext The execution context. |
||
| 51 | * |
||
| 52 | * @return mixed |
||
| 53 | */ |
||
| 54 | View Code Duplication | public function applyFilter($target, $rule, array $parameters = [], array $executionContext = []) |
|
| 62 | |||
| 63 | /** |
||
| 64 | * Filters a target using the given rule and parameters. |
||
| 65 | * The target compiler to use is determined at runtime using the registered ones. |
||
| 66 | * |
||
| 67 | * @param mixed $target The target to filter. |
||
| 68 | * @param string $rule The rule to apply. |
||
| 69 | * @param array $parameters The parameters used in the rule. |
||
| 70 | * @param array $executionContext The execution context. |
||
| 71 | * |
||
| 72 | * @return \Traversable The filtered target. |
||
| 73 | */ |
||
| 74 | View Code Duplication | public function filter($target, $rule, array $parameters = [], array $executionContext = []) |
|
| 82 | |||
| 83 | /** |
||
| 84 | * Filters a target using the given specification. |
||
| 85 | * The targetCompiler to use is determined at runtime using the registered ones. |
||
| 86 | * |
||
| 87 | * @param mixed $target The target to filter. |
||
| 88 | * @param Specification $spec The specification to apply. |
||
| 89 | * @param array $executionContext The execution context. |
||
| 90 | * |
||
| 91 | * @return mixed The filtered target. |
||
| 92 | */ |
||
| 93 | public function filterSpec($target, Specification $spec, array $executionContext = []) |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Apply the filters on a target using the given specification. |
||
| 100 | * The targetCompiler to use is determined at runtime using the registered ones. |
||
| 101 | * |
||
| 102 | * @param mixed $target The target to filter. |
||
| 103 | * @param Specification $spec The specification to apply. |
||
| 104 | * @param array $executionContext The execution context. |
||
| 105 | */ |
||
| 106 | public function applyFilterSpec($target, Specification $spec, array $executionContext = []) |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Tells if a target satisfies the given rule and parameters. |
||
| 113 | * The target compiler to use is determined at runtime using the registered ones. |
||
| 114 | * |
||
| 115 | * @param mixed $target The target. |
||
| 116 | * @param string $rule The rule to test. |
||
| 117 | * @param array $parameters The parameters used in the rule. |
||
| 118 | * @param array $executionContext The execution context. |
||
| 119 | * |
||
| 120 | * @return boolean |
||
| 121 | */ |
||
| 122 | View Code Duplication | public function satisfies($target, $rule, array $parameters = [], array $executionContext = []) |
|
| 129 | |||
| 130 | /** |
||
| 131 | * Tells if a target satisfies the given specification. |
||
| 132 | * The target compiler to use is determined at runtime using the registered ones. |
||
| 133 | * |
||
| 134 | * @param mixed $target The target. |
||
| 135 | * @param Specification $spec The specification to use. |
||
| 136 | * @param array $executionContext The execution context. |
||
| 137 | * |
||
| 138 | * @return boolean |
||
| 139 | */ |
||
| 140 | public function satisfiesSpec($target, Specification $spec, array $executionContext = []) |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Finds a target compiler supporting the given target. |
||
| 147 | * |
||
| 148 | * @param mixed $target The target to filter. |
||
| 149 | * @param string $mode The execution mode (MODE_FILTER or MODE_SATISFIES). |
||
| 150 | * |
||
| 151 | * @throws TargetUnsupportedException |
||
| 152 | * |
||
| 153 | * @return CompilationTarget |
||
| 154 | */ |
||
| 155 | private function findTargetCompiler($target, $mode) |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Ensures positional parameters are prefixed with a string |
||
| 169 | * |
||
| 170 | * @param array $params The parameters to normalize |
||
| 171 | * |
||
| 172 | * @return array |
||
| 173 | */ |
||
| 174 | private function normalizeParameters(array $params) |
||
| 186 | } |
||
| 187 |
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.