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 |
||
40 | 1 | final class PermissionsExtension extends DI\CompilerExtension |
|
41 | { |
||
42 | /** |
||
43 | * @var array |
||
44 | */ |
||
45 | private $defaults = [ |
||
46 | 'annotation' => TRUE, |
||
47 | 'redirectUrl' => NULL, |
||
48 | 'providers' => [ |
||
49 | 'roles' => TRUE, |
||
50 | 'resources' => TRUE, |
||
51 | 'permissions' => TRUE, |
||
52 | ], |
||
53 | ]; |
||
54 | |||
55 | public function loadConfiguration() : void |
||
56 | { |
||
57 | // Get container builder |
||
58 | 1 | $builder = $this->getContainerBuilder(); |
|
59 | // Get extension configuration |
||
60 | 1 | $configuration = $this->getConfig($this->defaults); |
|
61 | |||
62 | // Application permissions |
||
63 | 1 | $builder->addDefinition($this->prefix('permissions')) |
|
64 | 1 | ->setType(Security\Permission::class); |
|
65 | |||
66 | 1 | $builder->addDefinition($this->prefix('config')) |
|
67 | 1 | ->setType(Permissions\Configuration::class) |
|
68 | 1 | ->setArguments([ |
|
69 | 1 | $configuration['redirectUrl'], |
|
70 | ]); |
||
71 | |||
72 | /** |
||
73 | * Data providers |
||
74 | */ |
||
75 | |||
76 | 1 | View Code Duplication | if ($configuration['providers']['roles'] === TRUE) { |
|
|||
77 | $builder->addDefinition($this->prefix('providers.roles')) |
||
78 | ->setType(Providers\RolesProvider::class); |
||
79 | |||
80 | 1 | } elseif (is_string($configuration['providers']['roles']) && class_exists($configuration['providers']['roles'])) { |
|
81 | $builder->addDefinition($this->prefix('providers.roles')) |
||
82 | ->setType($configuration['providers']['roles']); |
||
83 | } |
||
84 | |||
85 | 1 | View Code Duplication | if ($configuration['providers']['resources'] === TRUE) { |
86 | $builder->addDefinition($this->prefix('providers.resources')) |
||
87 | ->setType(Providers\ResourcesProvider::class); |
||
88 | |||
89 | 1 | } elseif (is_string($configuration['providers']['resources']) && class_exists($configuration['providers']['resources'])) { |
|
90 | 1 | $builder->addDefinition($this->prefix('providers.resources')) |
|
91 | 1 | ->setType($configuration['providers']['resources']); |
|
92 | } |
||
93 | |||
94 | 1 | View Code Duplication | if ($configuration['providers']['permissions'] === TRUE) { |
95 | $builder->addDefinition($this->prefix('providers.permissions')) |
||
96 | ->setType(Providers\PermissionsProvider::class); |
||
97 | |||
98 | 1 | } elseif (is_string($configuration['providers']['permissions']) && class_exists($configuration['providers']['permissions'])) { |
|
99 | 1 | $builder->addDefinition($this->prefix('providers.permissions')) |
|
100 | 1 | ->setType($configuration['providers']['permissions']); |
|
101 | } |
||
102 | |||
103 | /** |
||
104 | * Access checkers |
||
105 | */ |
||
106 | |||
107 | // Check if annotation checker is enabled |
||
108 | 1 | if ($configuration['annotation'] === TRUE) { |
|
109 | // Annotation access checkers |
||
110 | 1 | $builder->addDefinition($this->prefix('checkers.annotation')) |
|
111 | 1 | ->setType(Access\AnnotationChecker::class); |
|
112 | } |
||
113 | |||
114 | // Latte access checker |
||
115 | 1 | $builder->addDefinition($this->prefix('checkers.latte')) |
|
116 | 1 | ->setType(Access\LatteChecker::class); |
|
117 | |||
118 | // Link access checker |
||
119 | 1 | $builder->addDefinition($this->prefix('checkers.link')) |
|
120 | 1 | ->setType(Access\LinkChecker::class); |
|
121 | 1 | } |
|
122 | |||
123 | /** |
||
124 | * {@inheritdoc} |
||
125 | */ |
||
126 | public function beforeCompile() : void |
||
157 | |||
158 | /** |
||
159 | * @param Nette\Configurator $config |
||
160 | * @param string $extensionName |
||
161 | * |
||
162 | * @return void |
||
163 | */ |
||
164 | public static function register(Nette\Configurator $config, $extensionName = 'permissions') : void |
||
170 | |||
171 | /** |
||
172 | * @param array $permissions |
||
173 | * @param DI\ServiceDefinition|NULL $resourcesProvider |
||
174 | * @param DI\ServiceDefinition|NULL $permissionsProvider |
||
175 | * |
||
176 | * @return void |
||
177 | * |
||
178 | * @throws Exceptions\InvalidArgumentException |
||
179 | */ |
||
180 | private function registerPermissionsResources( |
||
227 | } |
||
228 |
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.