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 |
||
7 | class GenericObjectAction extends ObjectAction { |
||
8 | |||
9 | private $execute; |
||
10 | private $fill; |
||
11 | private $caption; |
||
12 | private $description; |
||
13 | private $paramMap = []; |
||
14 | |||
15 | public function __construct($class, TypeFactory $types, CommentParser $parser, callable $execute) { |
||
19 | |||
20 | public function setCaption($caption) { |
||
24 | |||
25 | public function caption() { |
||
28 | |||
29 | public function setDescription($description) { |
||
33 | |||
34 | public function description() { |
||
37 | |||
38 | public function setExecute(callable $execute) { |
||
42 | |||
43 | public function setFill(callable $fill) { |
||
47 | |||
48 | /** |
||
49 | * @param callable $callback Filters the return value of execute |
||
50 | */ |
||
51 | public function setAfterExecute(callable $callback) { |
||
57 | |||
58 | /** |
||
59 | * @param string $name |
||
60 | * @param callable $map Receives Parameter and returns Parameter |
||
61 | * @return static |
||
62 | */ |
||
63 | public function mapParameter($name, callable $map) { |
||
67 | |||
68 | /** |
||
69 | * @return \rtens\domin\Parameter[] |
||
70 | * @throws \Exception |
||
71 | */ |
||
72 | View Code Duplication | public function parameters() { |
|
80 | |||
81 | /** |
||
82 | * Called by execute() with the instantiated object |
||
83 | * |
||
84 | * @param object $object |
||
85 | * @return mixed |
||
86 | */ |
||
87 | protected function executeWith($object) { |
||
90 | |||
91 | View Code Duplication | public function fill(array $parameters) { |
|
98 | } |
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.