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 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.