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 |
||
5 | class ResourceRegistrar extends \Illuminate\Routing\ResourceRegistrar |
||
6 | { |
||
7 | /** |
||
8 | * The default actions for a resourceful controller. |
||
9 | * |
||
10 | * @var array |
||
11 | */ |
||
12 | protected $resourceDefaults = ['index', 'store', 'show', 'update', 'destroy']; |
||
13 | |||
14 | /** |
||
15 | * The default actions for each named resource relationship. |
||
16 | * |
||
17 | * @var array |
||
18 | */ |
||
19 | protected $relationshipDefaults = ['show']; |
||
20 | |||
21 | /** |
||
22 | * Create a new resource registrar instance. |
||
23 | * |
||
24 | * @param \Huntie\JsonApi\Routing\Router $router |
||
25 | */ |
||
26 | public function __construct(Router $router) |
||
30 | |||
31 | /** |
||
32 | * Route a resource to a controller. |
||
33 | * |
||
34 | * @param string $name |
||
35 | * @param string $controller |
||
36 | * @param array $options |
||
37 | */ |
||
38 | public function register($name, $controller, array $options = []) |
||
44 | |||
45 | /** |
||
46 | * Route any resource relationships to a controller. |
||
47 | * |
||
48 | * @param string $name |
||
49 | * @param string $controller |
||
50 | * @param array $options |
||
51 | */ |
||
52 | protected function registerRelationships($name, $controller, array $options) |
||
74 | |||
75 | /** |
||
76 | * Add a relationship show method to match named relationships on the resource. |
||
77 | * |
||
78 | * @param string $name |
||
79 | * @param string $base |
||
80 | * @param array $relationships |
||
81 | * @param string $controller |
||
82 | * @param array $options |
||
83 | * |
||
84 | * @return \Illuminate\Routing\Route |
||
85 | */ |
||
86 | View Code Duplication | protected function addRelationshipShow($name, $base, array $relationships, $controller, array $options) |
|
94 | |||
95 | /** |
||
96 | * Add a relationship update method to match named relationships on the resource. |
||
97 | * |
||
98 | * @param string $name |
||
99 | * @param string $base |
||
100 | * @param array $relationships |
||
101 | * @param string $controller |
||
102 | * @param array $options |
||
103 | * |
||
104 | * @return \Illuminate\Routing\Route |
||
105 | */ |
||
106 | View Code Duplication | protected function addRelationshipUpdate($name, $base, array $relationships, $controller, array $options) |
|
114 | |||
115 | /** |
||
116 | * Get the URI for resource relationships. |
||
117 | * |
||
118 | * @param string $name |
||
119 | * @param string $base |
||
120 | * |
||
121 | * @return string |
||
122 | */ |
||
123 | public function getRelationshipUri($name, $base) |
||
127 | |||
128 | /** |
||
129 | * Format a resource parameter for usage. |
||
130 | * |
||
131 | * @param string $value |
||
132 | * |
||
133 | * @return string |
||
134 | */ |
||
135 | public function getResourceWildcard($value) |
||
149 | } |
||
150 |
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.