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 |
||
21 | class KunstmaanAdminExtension extends Extension implements PrependExtensionInterface |
||
22 | { |
||
23 | /** |
||
24 | * Loads a specific configuration. |
||
25 | * |
||
26 | * @param array $configs An array of configuration values |
||
27 | * @param ContainerBuilder $container A ContainerBuilder instance |
||
28 | * |
||
29 | * @throws InvalidArgumentException When provided tag is not defined in this extension |
||
30 | */ |
||
31 | 14 | public function load(array $configs, ContainerBuilder $container) |
|
90 | |||
91 | 14 | public function prepend(ContainerBuilder $container) |
|
92 | { |
||
93 | 14 | $knpMenuConfig['twig'] = true; // set to false to disable the Twig extension and the TwigRenderer |
|
|
|||
94 | 14 | $knpMenuConfig['templating'] = false; // if true, enables the helper for PHP templates |
|
95 | 14 | $knpMenuConfig['default_renderer'] = 'twig'; // The renderer to use, list is also available by default |
|
96 | 14 | $container->prependExtensionConfig('knp_menu', $knpMenuConfig); |
|
97 | |||
98 | 14 | $fosUserOriginalConfig = $container->getExtensionConfig('fos_user'); |
|
99 | 14 | if (!isset($fosUserOriginalConfig[0]['db_driver'])) { |
|
100 | 14 | $fosUserConfig['db_driver'] = 'orm'; // other valid values are 'mongodb', 'couchdb' |
|
101 | } |
||
102 | 14 | $fosUserConfig['from_email']['address'] = '[email protected]'; |
|
103 | 14 | $fosUserConfig['from_email']['sender_name'] = 'KunstmaanCMS'; |
|
104 | 14 | $fosUserConfig['firewall_name'] = 'main'; |
|
105 | 14 | $fosUserConfig['user_class'] = 'Kunstmaan\AdminBundle\Entity\User'; |
|
106 | 14 | $fosUserConfig['group']['group_class'] = 'Kunstmaan\AdminBundle\Entity\Group'; |
|
107 | 14 | $fosUserConfig['resetting']['token_ttl'] = 86400; |
|
108 | // Use this node only if you don't want the global email address for the resetting email |
||
109 | 14 | $fosUserConfig['resetting']['email']['from_email']['address'] = '[email protected]'; |
|
110 | 14 | $fosUserConfig['resetting']['email']['from_email']['sender_name'] = 'KunstmaanCMS'; |
|
111 | 14 | $fosUserConfig['resetting']['email']['template'] = '@FOSUser/Resetting/email.txt.twig'; |
|
112 | 14 | $fosUserConfig['resetting']['form']['type'] = ResettingFormType::class; |
|
113 | 14 | $fosUserConfig['resetting']['form']['name'] = 'fos_user_resetting_form'; |
|
114 | 14 | $fosUserConfig['resetting']['form']['validation_groups'] = ['ResetPassword']; |
|
115 | |||
116 | 14 | $fosUserConfig['service']['mailer'] = 'fos_user.mailer.twig_swift'; |
|
117 | 14 | $container->prependExtensionConfig('fos_user', $fosUserConfig); |
|
118 | |||
119 | 14 | $monologConfig['handlers']['main']['type'] = 'rotating_file'; |
|
120 | 14 | $monologConfig['handlers']['main']['path'] = sprintf('%s/%s', $container->getParameter('kernel.logs_dir'), $container->getParameter('kernel.environment')); |
|
121 | 14 | $monologConfig['handlers']['main']['level'] = 'debug'; |
|
122 | 14 | $container->prependExtensionConfig('monolog', $monologConfig); |
|
123 | |||
124 | 14 | $twigConfig['paths'][] = ['value' => \dirname(__DIR__).'/Resources/views', 'namespace' => 'FOSUser']; |
|
125 | 14 | $container->prependExtensionConfig('twig', $twigConfig); |
|
126 | |||
127 | // NEXT_MAJOR: Remove templating dependency |
||
128 | 14 | $frameworkConfig['templating']['engines'] = ['twig']; |
|
129 | 14 | $container->prependExtensionConfig('framework', $frameworkConfig); |
|
130 | |||
131 | 14 | $configs = $container->getExtensionConfig($this->getAlias()); |
|
132 | 14 | $this->processConfiguration(new Configuration(), $configs); |
|
133 | 14 | } |
|
134 | |||
135 | /** |
||
136 | * {@inheritdoc} |
||
137 | */ |
||
138 | 15 | public function getNamespace() |
|
139 | { |
||
140 | 15 | return 'http://bundles.kunstmaan.be/schema/dic/admin'; |
|
141 | } |
||
142 | |||
143 | /** |
||
144 | * {@inheritdoc} |
||
145 | */ |
||
146 | public function getXsdValidationBasePath() |
||
147 | { |
||
148 | return __DIR__.'/../Resources/config/schema'; |
||
149 | } |
||
150 | |||
151 | 1 | private function addSimpleMenuAdaptor(ContainerBuilder $container, array $menuItems) |
|
152 | { |
||
153 | 1 | $definition = new Definition('Kunstmaan\AdminBundle\Helper\Menu\SimpleMenuAdaptor', [ |
|
154 | 1 | new Reference('security.authorization_checker'), |
|
155 | 1 | $menuItems, |
|
156 | ]); |
||
157 | 1 | $definition->addTag('kunstmaan_admin.menu.adaptor'); |
|
158 | |||
159 | 1 | $container->setDefinition('kunstmaan_admin.menu.adaptor.simple', $definition); |
|
160 | 1 | } |
|
161 | |||
162 | /** |
||
163 | * @param string $urlSlice |
||
164 | * |
||
165 | * @return string |
||
166 | */ |
||
167 | 14 | protected function normalizeUrlSlice($urlSlice) |
|
180 | |||
181 | 14 | View Code Duplication | private function addWebsiteTitleParameter(ContainerBuilder $container, array $config) |
182 | { |
||
183 | 14 | $websiteTitle = $config['website_title']; |
|
184 | 14 | if (null === $config['website_title']) { |
|
192 | |||
193 | 14 | View Code Duplication | private function addMultiLanguageParameter(ContainerBuilder $container, array $config) |
204 | |||
205 | 14 | View Code Duplication | private function addRequiredLocalesParameter(ContainerBuilder $container, array $config) |
217 | |||
218 | 14 | View Code Duplication | private function addDefaultLocaleParameter(ContainerBuilder $container, array $config) |
229 | } |
||
230 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.