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 |
||
52 | class Flow extends Run |
||
53 | { |
||
54 | /** |
||
55 | * @inheritdoc |
||
56 | */ |
||
57 | 44 | View Code Duplication | public function flow(NodeInterface $node) |
|
|||
58 | { |
||
59 | 44 | $this->log(LogLevel::NOTICE, "Running through {count} flows", ['count' => count($this->items)]); |
|
60 | |||
61 | 44 | $current = $node; |
|
62 | 44 | foreach ($this->items as $flow) { |
|
63 | 44 | $current = $flow->flow($current); |
|
64 | 44 | } |
|
65 | 44 | return $current; |
|
66 | } |
||
67 | |||
68 | /** |
||
69 | * @var FlowBuilderInterface |
||
70 | */ |
||
71 | protected static $builder; |
||
72 | |||
73 | /** |
||
74 | * @return FlowBuilderInterface |
||
75 | */ |
||
76 | 47 | protected static function getBuilder() |
|
77 | { |
||
78 | 47 | if (!static::$builder instanceof FlowBuilderInterface) { |
|
79 | 2 | static::$builder = new Builder(); |
|
80 | 2 | } |
|
81 | 47 | return static::$builder; |
|
82 | } |
||
83 | |||
84 | /** |
||
85 | * @param FlowBuilderInterface $builder |
||
86 | */ |
||
87 | 2 | public static function setBuilder(FlowBuilderInterface $builder) |
|
91 | |||
92 | /** |
||
93 | * @param LoggerInterface $logger |
||
94 | * |
||
95 | * @return null|void |
||
96 | */ |
||
97 | 1 | public static function useLogger(LoggerInterface $logger) |
|
98 | { |
||
99 | 1 | $builder = static::getBuilder(); |
|
100 | |||
101 | 1 | if ($builder instanceof LoggerAwareInterface) { |
|
102 | 1 | $builder->setLogger($logger); |
|
103 | 1 | } |
|
104 | 1 | } |
|
105 | |||
106 | /** |
||
107 | * @param string $namespace |
||
108 | */ |
||
109 | 1 | public static function with($namespace) |
|
113 | |||
114 | /** |
||
115 | * @param string $flowName |
||
116 | * @param array $arguments |
||
117 | * |
||
118 | * @return Flow |
||
119 | */ |
||
120 | 44 | public static function __callStatic($flowName, $arguments) |
|
125 | |||
126 | /** |
||
127 | * @param string $flowSpec |
||
128 | * @param array $arguments |
||
129 | * |
||
130 | * @return Flow |
||
131 | */ |
||
132 | 45 | public static function buildFlow($flowSpec, $arguments = []) |
|
136 | |||
137 | /** |
||
138 | * @param string $method |
||
139 | * @param array $arguments |
||
140 | * |
||
141 | * @return self |
||
142 | */ |
||
143 | 45 | public function __call($method, $arguments) |
|
147 | |||
148 | /** |
||
149 | * Create instance validator. |
||
150 | * |
||
151 | * @return Flow |
||
152 | */ |
||
153 | 1 | public static function create() |
|
158 | } |
||
159 |
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.