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 |
||
26 | abstract class RoutesLoader implements LoaderInterface |
||
27 | { |
||
28 | /** |
||
29 | * Array which contains the |
||
30 | * routes configuration. |
||
31 | * |
||
32 | * @var array |
||
33 | */ |
||
34 | protected $config; |
||
35 | |||
36 | /** |
||
37 | * Boolean that checks if the |
||
38 | * routes are already loaded or not. |
||
39 | * |
||
40 | * @var bool |
||
41 | */ |
||
42 | protected $loaded; |
||
43 | |||
44 | /** |
||
45 | * Collection of routes. |
||
46 | * |
||
47 | * @var RouteCollection |
||
48 | */ |
||
49 | protected $routes; |
||
50 | |||
51 | /** |
||
52 | * Constructor. |
||
53 | * |
||
54 | * @param array $config Array which contains the routes configuration |
||
55 | */ |
||
56 | public function __construct(array $config = []) |
||
61 | |||
62 | /** |
||
63 | * {@inheritdoc} |
||
64 | */ |
||
65 | public function load($resource, $type = null) |
||
96 | |||
97 | /** |
||
98 | * {@inheritdoc} |
||
99 | */ |
||
100 | public function getResolver() |
||
103 | |||
104 | /** |
||
105 | * {@inheritdoc} |
||
106 | */ |
||
107 | public function setResolver(LoaderResolverInterface $resolver) |
||
110 | |||
111 | /** |
||
112 | * Sanitizes and validates the given specification name. |
||
113 | * |
||
114 | * @param string $specificationName The specification name |
||
115 | * |
||
116 | * @return string |
||
117 | */ |
||
118 | protected function sanitize($specificationName) |
||
122 | |||
123 | /** |
||
124 | * Registers a new route inside route |
||
125 | * collection with the given params. |
||
126 | * |
||
127 | * @param string $file The user name |
||
128 | * @param array $config The user configuration array |
||
129 | */ |
||
130 | abstract protected function register($file, array $config); |
||
131 | } |
||
132 |
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.