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 |
||
22 | class RouteDefinition |
||
23 | { |
||
24 | /** |
||
25 | * Pattern of the route. |
||
26 | */ |
||
27 | const PATTERN = 'pattern'; |
||
28 | |||
29 | /** |
||
30 | * A controller class name (with an optional action) or a callable. |
||
31 | */ |
||
32 | const CONTROLLER = 'controller'; |
||
33 | |||
34 | /** |
||
35 | * The controller action. |
||
36 | */ |
||
37 | const ACTION = 'action'; |
||
38 | |||
39 | /** |
||
40 | * An identifier. |
||
41 | */ |
||
42 | const ID = 'id'; |
||
43 | |||
44 | /** |
||
45 | * A redirection target. |
||
46 | */ |
||
47 | const LOCATION = 'location'; |
||
48 | |||
49 | /** |
||
50 | * Request method(s) accepted by the route. |
||
51 | */ |
||
52 | const VIA = 'via'; |
||
53 | |||
54 | /** |
||
55 | * Route constructor, a class name for now. |
||
56 | */ |
||
57 | const CONSTRUCTOR = 'class'; |
||
58 | |||
59 | /** |
||
60 | * Normalizes a route definition. |
||
61 | * |
||
62 | * @param array $definition |
||
63 | */ |
||
64 | static public function normalize(array &$definition) |
||
71 | |||
72 | /** |
||
73 | * Ensures that a route definition has an identifier and generates one if required. |
||
74 | * |
||
75 | * @param array $definition |
||
76 | * |
||
77 | * @return string The route identifier. |
||
78 | */ |
||
79 | static public function ensure_has_id(array &$definition) |
||
88 | |||
89 | static private $anonymous_id_count; |
||
90 | |||
91 | /** |
||
92 | * Generates an anonymous route identifier. |
||
93 | * |
||
94 | * @return string |
||
95 | */ |
||
96 | static private function generate_anonymous_id() |
||
100 | |||
101 | /** |
||
102 | * Asserts that a route definition is valid. |
||
103 | * |
||
104 | * @param array $definition |
||
105 | * |
||
106 | * @throws PatternNotDefined when the pattern is not defined |
||
107 | * @throws ControllerNotDefined when both controller and location are not defined. |
||
108 | */ |
||
109 | static public function assert_is_valid(array $definition) |
||
129 | |||
130 | /** |
||
131 | * No instance should be created from this class. |
||
132 | * |
||
133 | * @codeCoverageIgnore |
||
134 | */ |
||
135 | private function __construct() |
||
139 | } |
||
140 |
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.