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 |
||
| 16 | abstract class AbstractAdapterFactory implements FactoryInterface |
||
|
|
|||
| 17 | { |
||
| 18 | /** |
||
| 19 | * @var array |
||
| 20 | */ |
||
| 21 | protected $options; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * AbstractAdapterFactory constructor. |
||
| 25 | * |
||
| 26 | * @param array $options |
||
| 27 | */ |
||
| 28 | 62 | public function __construct(array $options = []) |
|
| 32 | |||
| 33 | /** |
||
| 34 | * Set creation options |
||
| 35 | * |
||
| 36 | * @param array $options |
||
| 37 | * @return void |
||
| 38 | */ |
||
| 39 | 62 | public function setCreationOptions(array $options) |
|
| 43 | |||
| 44 | /** |
||
| 45 | * @param ContainerInterface $container |
||
| 46 | * @param $requestedName |
||
| 47 | * @param array|null $options |
||
| 48 | * @return AdapterInterface|VirtualProxyInterface |
||
| 49 | */ |
||
| 50 | 9 | public function __invoke(ContainerInterface $container, $requestedName, array $options = null) |
|
| 51 | { |
||
| 52 | 9 | if (null !== $options) { |
|
| 53 | 1 | $this->setCreationOptions($options); |
|
| 54 | } |
||
| 55 | |||
| 56 | 9 | $this->mergeMvcConfig($container, $requestedName); |
|
| 57 | |||
| 58 | 9 | $this->validateConfig(); |
|
| 59 | |||
| 60 | 9 | $service = $this->doCreateService($container); |
|
| 61 | |||
| 62 | 9 | return $service; |
|
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Create service |
||
| 67 | * |
||
| 68 | * @param ServiceLocatorInterface $serviceLocator |
||
| 69 | * @return AdapterInterface|VirtualProxyInterface |
||
| 70 | */ |
||
| 71 | View Code Duplication | public function createService(ServiceLocatorInterface $serviceLocator) |
|
| 72 | { |
||
| 73 | if (method_exists($serviceLocator, 'getServiceLocator')) { |
||
| 74 | $serviceLocator = $serviceLocator->getServiceLocator(); |
||
| 75 | } |
||
| 76 | |||
| 77 | return $this($serviceLocator, func_get_arg(2)); |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Merges the options given from the ServiceLocator Config object with the create options of the class. |
||
| 82 | * |
||
| 83 | * @param ServiceLocatorInterface $serviceLocator |
||
| 84 | * @param string $requestedName |
||
| 85 | */ |
||
| 86 | 12 | protected function mergeMvcConfig(ServiceLocatorInterface $serviceLocator, $requestedName) |
|
| 101 | |||
| 102 | /** |
||
| 103 | * @param ServiceLocatorInterface $serviceLocator |
||
| 104 | * @return LazyLoadingValueHolderFactory |
||
| 105 | * @throws InvalidArgumentException |
||
| 106 | */ |
||
| 107 | 1 | public function getLazyFactory(ServiceLocatorInterface $serviceLocator) |
|
| 140 | |||
| 141 | /** |
||
| 142 | * Create service |
||
| 143 | * |
||
| 144 | * @param ServiceLocatorInterface $serviceLocator |
||
| 145 | * @return AdapterInterface|VirtualProxyInterface |
||
| 146 | */ |
||
| 147 | abstract protected function doCreateService(ServiceLocatorInterface $serviceLocator); |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Implement in adapter |
||
| 151 | * |
||
| 152 | * @throw UnexpectedValueException |
||
| 153 | * @return null |
||
| 154 | */ |
||
| 155 | abstract protected function validateConfig(); |
||
| 156 | } |
||
| 157 |
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.