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 | // Manually register the KunstmaanAdminBundle folder as a FosUser override for symfony 4. |
||
| 125 | 14 | if ($container->hasParameter('kernel.project_dir') && file_exists($container->getParameter('kernel.project_dir').'/templates/bundles/KunstmaanAdminBundle')) { |
|
| 126 | $twigConfig['paths'][] = ['value' => '%kernel.project_dir%/templates/bundles/KunstmaanAdminBundle', 'namespace' => 'FOSUser']; |
||
| 127 | } |
||
| 128 | 14 | $twigConfig['paths'][] = ['value' => \dirname(__DIR__).'/Resources/views', 'namespace' => 'FOSUser']; |
|
| 129 | 14 | $container->prependExtensionConfig('twig', $twigConfig); |
|
| 130 | |||
| 131 | // NEXT_MAJOR: Remove templating dependency |
||
| 132 | 14 | $frameworkConfig['templating']['engines'] = ['twig']; |
|
| 133 | 14 | $container->prependExtensionConfig('framework', $frameworkConfig); |
|
| 134 | |||
| 135 | 14 | $configs = $container->getExtensionConfig($this->getAlias()); |
|
| 136 | 14 | $this->processConfiguration(new Configuration(), $configs); |
|
| 137 | 14 | } |
|
| 138 | |||
| 139 | /** |
||
| 140 | * {@inheritdoc} |
||
| 141 | */ |
||
| 142 | 15 | public function getNamespace() |
|
| 143 | { |
||
| 144 | 15 | return 'http://bundles.kunstmaan.be/schema/dic/admin'; |
|
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * {@inheritdoc} |
||
| 149 | */ |
||
| 150 | public function getXsdValidationBasePath() |
||
| 151 | { |
||
| 152 | return __DIR__.'/../Resources/config/schema'; |
||
| 153 | } |
||
| 154 | |||
| 155 | 1 | private function addSimpleMenuAdaptor(ContainerBuilder $container, array $menuItems) |
|
| 156 | { |
||
| 157 | 1 | $definition = new Definition('Kunstmaan\AdminBundle\Helper\Menu\SimpleMenuAdaptor', [ |
|
| 158 | 1 | new Reference('security.authorization_checker'), |
|
| 159 | 1 | $menuItems, |
|
| 160 | ]); |
||
| 161 | 1 | $definition->addTag('kunstmaan_admin.menu.adaptor'); |
|
| 162 | |||
| 163 | 1 | $container->setDefinition('kunstmaan_admin.menu.adaptor.simple', $definition); |
|
| 164 | 1 | } |
|
| 165 | |||
| 166 | /** |
||
| 167 | * @param string $urlSlice |
||
| 168 | * |
||
| 169 | * @return string |
||
| 170 | */ |
||
| 171 | 14 | protected function normalizeUrlSlice($urlSlice) |
|
| 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']) { |
|
| 196 | |||
| 197 | 14 | View Code Duplication | private function addMultiLanguageParameter(ContainerBuilder $container, array $config) |
| 208 | |||
| 209 | 14 | View Code Duplication | private function addRequiredLocalesParameter(ContainerBuilder $container, array $config) |
| 221 | |||
| 222 | 14 | View Code Duplication | private function addDefaultLocaleParameter(ContainerBuilder $container, array $config) |
| 233 | } |
||
| 234 |
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.