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 | ||
| 28 | class KunstmaanAdminExtension extends Extension implements PrependExtensionInterface | ||
| 29 | { | ||
| 30 | /** | ||
| 31 | 14 | * Loads a specific configuration. | |
| 32 | * | ||
| 33 | 14 | * @param array $configs An array of configuration values | |
| 34 | 14 | * @param ContainerBuilder $container A ContainerBuilder instance | |
| 35 | 14 | * | |
| 36 | * @throws InvalidArgumentException When provided tag is not defined in this extension | ||
| 37 | 14 | */ | |
| 38 | 14 | public function load(array $configs, ContainerBuilder $container) | |
| 39 |     { | ||
| 40 | 14 | $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); | |
| 41 | 1 |         $loader->load('services.yml'); | |
| 42 |         $loader->load('commands.yml'); | ||
| 43 | 14 | ||
| 44 | 1 |         $container->setParameter('version_checker.url', 'https://cms.kunstmaan.be/version-check'); | |
| 45 |         $container->setParameter('version_checker.timeframe', 60 * 60 * 24); | ||
| 46 | 14 |         $container->setParameter('version_checker.enabled', true); | |
| 47 | 14 | ||
| 48 | $configuration = new Configuration(); | ||
| 49 | 14 | $config = $this->processConfiguration($configuration, $configs); | |
| 50 | 14 | ||
| 51 |         if (\array_key_exists('dashboard_route', $config)) { | ||
| 52 | 14 |             $container->setParameter('kunstmaan_admin.dashboard_route', $config['dashboard_route']); | |
| 53 | } | ||
| 54 | 14 |         if (\array_key_exists('admin_password', $config)) { | |
| 55 |             $container->setParameter('kunstmaan_admin.admin_password', $config['admin_password']); | ||
| 56 | 14 | } | |
| 57 | 14 | ||
| 58 | 14 | $this->configureAuthentication($config, $container, $loader); | |
| 59 | 14 | ||
| 60 |         $container->setParameter('kunstmaan_admin.admin_locales', $config['admin_locales']); | ||
| 61 | 14 |         $container->setParameter('kunstmaan_admin.default_admin_locale', $config['default_admin_locale']); | |
| 62 | 14 | ||
| 63 | 14 |         $container->setParameter('kunstmaan_admin.session_security.ip_check', $config['session_security']['ip_check']); | |
| 64 | 14 |         $container->setParameter('kunstmaan_admin.session_security.user_agent_check', $config['session_security']['user_agent_check']); | |
| 65 | 14 | ||
| 66 | 14 |         $container->setParameter('kunstmaan_admin.admin_prefix', $this->normalizeUrlSlice($config['admin_prefix'])); | |
| 67 | 14 | ||
| 68 | 14 |         $container->setParameter('kunstmaan_admin.admin_exception_excludes', $config['admin_exception_excludes']); | |
| 69 | |||
| 70 | 14 |         $container->setParameter('kunstmaan_admin.google_signin.enabled', $config['google_signin']['enabled']); | |
| 71 | 14 |         $container->setParameter('kunstmaan_admin.google_signin.client_id', $config['google_signin']['client_id']); | |
| 72 |         $container->setParameter('kunstmaan_admin.google_signin.client_secret', $config['google_signin']['client_secret']); | ||
| 73 | 14 |         $container->setParameter('kunstmaan_admin.google_signin.hosted_domains', $config['google_signin']['hosted_domains']); | |
| 74 | 14 | ||
| 75 | 14 |         $container->setParameter('kunstmaan_admin.password_restrictions.min_digits', $config['password_restrictions']['min_digits']); | |
| 76 |         $container->setParameter('kunstmaan_admin.password_restrictions.min_uppercase', $config['password_restrictions']['min_uppercase']); | ||
| 77 | 14 |         $container->setParameter('kunstmaan_admin.password_restrictions.min_special_characters', $config['password_restrictions']['min_special_characters']); | |
| 78 | 14 |         $container->setParameter('kunstmaan_admin.password_restrictions.min_length', $config['password_restrictions']['min_length']); | |
| 79 |         $container->setParameter('kunstmaan_admin.password_restrictions.max_length', $config['password_restrictions']['max_length']); | ||
| 80 |         $container->setParameter('kunstmaan_admin.enable_toolbar_helper', $config['enable_toolbar_helper']); | ||
| 81 | 14 |         $container->setParameter('kunstmaan_admin.toolbar_firewall_names', !empty($config['provider_keys']) ? $config['provider_keys'] : $config['toolbar_firewall_names']); | |
| 82 | 1 |         $container->setParameter('kunstmaan_admin.admin_firewall_name', $config['admin_firewall_name']); | |
| 83 |         $container->setParameter('kunstmaan_admin.user_class', $config['admin_user_class']); | ||
| 84 |         $container->setParameter('kunstmaan_admin.group_class', $config['admin_group_class']); | ||
| 85 | 14 | ||
| 86 | 14 | $container->registerForAutoconfiguration(MenuAdaptorInterface::class) | |
| 87 | 14 |             ->addTag('kunstmaan_admin.menu.adaptor'); | |
| 88 | 14 | ||
| 89 | 14 |         if (!empty($config['enable_console_exception_listener']) && $config['enable_console_exception_listener']) { | |
| 90 |             $loader->load('console_listener.yml'); | ||
| 91 | 14 | } | |
| 92 | |||
| 93 | 14 |         if (0 !== \count($config['menu_items'])) { | |
| 94 | 14 | $this->addSimpleMenuAdaptor($container, $config['menu_items']); | |
| 95 | 14 | } | |
| 96 | |||
| 97 | 14 | $this->addWebsiteTitleParameter($container, $config); | |
| 98 | 14 | $this->addMultiLanguageParameter($container, $config); | |
| 99 | 14 | $this->addRequiredLocalesParameter($container, $config); | |
| 100 | 14 | $this->addDefaultLocaleParameter($container, $config); | |
| 101 | 14 | } | |
| 102 | 14 | ||
| 103 | public function prepend(ContainerBuilder $container) | ||
| 104 | 14 |     { | |
| 105 | 14 |         $fosUserOriginalConfig = $container->getExtensionConfig('fos_user'); | |
| 106 | 14 |         if (!isset($fosUserOriginalConfig[0]['db_driver'])) { | |
| 107 | 14 | $fosUserConfig['db_driver'] = 'orm'; // other valid values are 'mongodb', 'couchdb' | |
|  | |||
| 108 | 14 | } | |
| 109 | 14 | $fosUserConfig['from_email']['address'] = '[email protected]'; | |
| 110 | $fosUserConfig['from_email']['sender_name'] = 'KunstmaanCMS'; | ||
| 111 | 14 | $fosUserConfig['firewall_name'] = 'main'; | |
| 112 | 14 | $fosUserConfig['user_class'] = 'Kunstmaan\AdminBundle\Entity\User'; | |
| 113 | $fosUserConfig['group']['group_class'] = 'Kunstmaan\AdminBundle\Entity\Group'; | ||
| 114 | $fosUserConfig['resetting']['token_ttl'] = 86400; | ||
| 115 | 14 | // Use this node only if you don't want the global email address for the resetting email | |
| 116 | $fosUserConfig['resetting']['email']['from_email']['address'] = '[email protected]'; | ||
| 117 | $fosUserConfig['resetting']['email']['from_email']['sender_name'] = 'KunstmaanCMS'; | ||
| 118 | 14 | $fosUserConfig['resetting']['email']['template'] = '@FOSUser/Resetting/email.txt.twig'; | |
| 119 | 14 | $fosUserConfig['resetting']['form']['type'] = ResettingFormType::class; | |
| 120 | $fosUserConfig['resetting']['form']['name'] = 'fos_user_resetting_form'; | ||
| 121 | $fosUserConfig['resetting']['form']['validation_groups'] = ['ResetPassword']; | ||
| 122 | |||
| 123 | 14 | $fosUserConfig['service']['mailer'] = 'fos_user.mailer.twig_swift'; | |
| 124 | 14 |         $container->prependExtensionConfig('fos_user', $fosUserConfig); | |
| 125 | 14 | ||
| 126 | // Manually register the KunstmaanAdminBundle folder as a FosUser override for symfony 4. | ||
| 127 |         if ($container->hasParameter('kernel.project_dir') && file_exists($container->getParameter('kernel.project_dir').'/templates/bundles/KunstmaanAdminBundle')) { | ||
| 128 | $twigConfig['paths'][] = ['value' => '%kernel.project_dir%/templates/bundles/KunstmaanAdminBundle', 'namespace' => 'FOSUser']; | ||
| 129 | } | ||
| 130 | 15 | $twigConfig['paths'][] = ['value' => \dirname(__DIR__).'/Resources/views', 'namespace' => 'FOSUser']; | |
| 131 |         $container->prependExtensionConfig('twig', $twigConfig); | ||
| 132 | 15 | ||
| 133 | // NEXT_MAJOR: Remove templating dependency | ||
| 134 | |||
| 135 | $configs = $container->getExtensionConfig($this->getAlias()); | ||
| 136 | $this->processConfiguration(new Configuration(), $configs); | ||
| 137 | } | ||
| 138 | |||
| 139 | /** | ||
| 140 |      * {@inheritdoc} | ||
| 141 | */ | ||
| 142 | public function getNamespace() | ||
| 143 | 1 |     { | |
| 144 | return 'http://bundles.kunstmaan.be/schema/dic/admin'; | ||
| 145 | 1 | } | |
| 146 | 1 | ||
| 147 | 1 | /** | |
| 148 |      * {@inheritdoc} | ||
| 149 | 1 | */ | |
| 150 | public function getXsdValidationBasePath() | ||
| 154 | |||
| 155 | private function addSimpleMenuAdaptor(ContainerBuilder $container, array $menuItems) | ||
| 156 |     { | ||
| 157 |         $definition = new Definition('Kunstmaan\AdminBundle\Helper\Menu\SimpleMenuAdaptor', [ | ||
| 158 |             new Reference('security.authorization_checker'), | ||
| 159 | 14 | $menuItems, | |
| 160 | ]); | ||
| 161 |         $definition->addTag('kunstmaan_admin.menu.adaptor'); | ||
| 162 | 14 | ||
| 163 |         $container->setDefinition('kunstmaan_admin.menu.adaptor.simple', $definition); | ||
| 164 | } | ||
| 165 | 14 | ||
| 166 | /** | ||
| 167 | * @param string $urlSlice | ||
| 168 | 14 | * | |
| 169 | * @return string | ||
| 170 | 14 | */ | |
| 171 | protected function normalizeUrlSlice($urlSlice) | ||
| 172 |     { | ||
| 173 | 14 | /* Get rid of exotic characters that would break the url */ | |
| 174 | $urlSlice = filter_var($urlSlice, FILTER_SANITIZE_URL); | ||
| 175 | 14 | ||
| 176 | 14 | /* Remove leading and trailing slashes */ | |
| 177 | 2 | $urlSlice = trim($urlSlice, '/'); | |
| 178 | |||
| 179 | 2 | /* Make sure our $urlSlice is literally used in our regex */ | |
| 180 | $urlSlice = preg_quote($urlSlice); | ||
| 181 | |||
| 182 | 14 | return $urlSlice; | |
| 183 | 14 | } | |
| 184 | |||
| 185 | 14 | View Code Duplication | private function addWebsiteTitleParameter(ContainerBuilder $container, array $config) | 
| 186 |     { | ||
| 187 | 14 | $websiteTitle = $config['website_title']; | |
| 188 | 14 |         if (null === $config['website_title']) { | |
| 189 | 2 |             @trigger_error('Not providing a value for the "kunstmaan_admin.website_title" config is deprecated since KunstmaanAdminBundle 5.2, this config value will be required in KunstmaanAdminBundle 6.0.', E_USER_DEPRECATED); | |
| 190 | |||
| 191 | 2 |             $websiteTitle = $container->hasParameter('websitetitle') ? $container->getParameter('websitetitle') : ''; | |
| 192 | } | ||
| 193 | |||
| 194 | 14 |         $container->setParameter('kunstmaan_admin.website_title', $websiteTitle); | |
| 195 | 14 | } | |
| 196 | |||
| 197 | 14 | View Code Duplication | private function addMultiLanguageParameter(ContainerBuilder $container, array $config) | 
| 198 |     { | ||
| 199 | 14 | $multilanguage = $config['multi_language']; | |
| 200 | 14 |         if (null === $multilanguage) { | |
| 201 | 2 |             @trigger_error('Not providing a value for the "kunstmaan_admin.multi_language" config is deprecated since KunstmaanAdminBundle 5.2, this config value will be required in KunstmaanAdminBundle 6.0.', E_USER_DEPRECATED); | |
| 202 | |||
| 203 | 2 |             $multilanguage = $container->hasParameter('multilanguage') ? $container->getParameter('multilanguage') : ''; | |
| 204 | } | ||
| 205 | |||
| 206 | 14 |         $container->setParameter('kunstmaan_admin.multi_language', $multilanguage); | |
| 207 | 14 | } | |
| 208 | 14 | ||
| 209 | View Code Duplication | private function addRequiredLocalesParameter(ContainerBuilder $container, array $config) | |
| 221 | |||
| 222 | View Code Duplication | private function addDefaultLocaleParameter(ContainerBuilder $container, array $config) | |
| 223 |     { | ||
| 224 | $defaultLocale = $config['default_locale']; | ||
| 225 |         if (null === $config['default_locale']) { | ||
| 226 |             @trigger_error('Not providing a value for the "kunstmaan_admin.default_locale" config is deprecated since KunstmaanAdminBundle 5.2, this config value will be required in KunstmaanAdminBundle 6.0.', E_USER_DEPRECATED); | ||
| 227 | |||
| 233 | |||
| 234 | private function configureAuthentication(array $config, ContainerBuilder $container, LoaderInterface $loader) | ||
| 286 | } | ||
| 287 | 
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
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey 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.