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 |
||
| 11 | final class UserExtension extends Nette\DI\CompilerExtension implements |
||
| 12 | Kdyby\Doctrine\DI\IEntityProvider, |
||
| 13 | Kdyby\Doctrine\DI\ITargetEntityProvider, |
||
| 14 | Kdyby\Translation\DI\ITranslationProvider |
||
| 15 | { |
||
| 16 | /** @var array */ |
||
| 17 | private $defaults = [ |
||
| 18 | # 'common' is loaded in constructor |
||
| 19 | # 'doctrine_identity' is loaded in constructor |
||
| 20 | # 'forgot_password' is loaded in constructor |
||
| 21 | # 'authentication' is loaded in constructor |
||
| 22 | ]; |
||
| 23 | |||
| 24 | /** @var string[]|\SixtyEightPublishers\User\DI\IExtensionAdapter[] */ |
||
| 25 | private $extensionAdapters = [ |
||
| 26 | 'common' => SixtyEightPublishers\User\Common\DI\CommonExtensionAdapter::class, |
||
| 27 | 'doctrine_identity' => SixtyEightPublishers\User\DoctrineIdentity\DI\DoctrineIdentityExtensionAdapter::class, |
||
| 28 | 'forgot_password' => SixtyEightPublishers\User\ForgotPassword\DI\ForgotPasswordExtensionAdapter::class, |
||
| 29 | 'authentication' => SixtyEightPublishers\User\Authentication\DI\AuthenticationExtensionAdapter::class, |
||
| 30 | ]; |
||
| 31 | |||
| 32 | /** @var bool */ |
||
| 33 | private $extensionAdaptersBuilded = FALSE; |
||
| 34 | |||
| 35 | public function __construct() |
||
| 44 | |||
| 45 | /** |
||
| 46 | * {@inheritdoc} |
||
| 47 | */ |
||
| 48 | public function loadConfiguration(): void |
||
| 54 | |||
| 55 | /** |
||
| 56 | * {@inheritdoc} |
||
| 57 | */ |
||
| 58 | public function beforeCompile(): void |
||
| 64 | |||
| 65 | /** |
||
| 66 | * {@inheritdoc} |
||
| 67 | */ |
||
| 68 | public function afterCompile(Nette\PhpGenerator\ClassType $class): void |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @param array $config |
||
| 77 | */ |
||
| 78 | private function buildExtensionAdapters(array $config): void |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @return \SixtyEightPublishers\User\DI\IExtensionAdapter[] |
||
| 93 | */ |
||
| 94 | private function getExtensionAdapters(): array |
||
| 106 | |||
| 107 | /**************** interface \Kdyby\Doctrine\DI\IEntityProvider ****************/ |
||
| 108 | |||
| 109 | /** |
||
| 110 | * {@inheritdoc} |
||
| 111 | */ |
||
| 112 | View Code Duplication | public function getEntityMappings(): array |
|
| 123 | |||
| 124 | /**************** interface \Kdyby\Doctrine\DI\ITargetEntityProvider ****************/ |
||
| 125 | |||
| 126 | /** |
||
| 127 | * {@inheritdoc} |
||
| 128 | */ |
||
| 129 | View Code Duplication | public function getTargetEntityMappings(): array |
|
| 140 | |||
| 141 | /**************** interface \Kdyby\Translation\DI\ITranslationProvider ****************/ |
||
| 142 | |||
| 143 | /** |
||
| 144 | * {@inheritdoc} |
||
| 145 | */ |
||
| 146 | View Code Duplication | public function getTranslationResources(): array |
|
| 157 | } |
||
| 158 |
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.