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 |
||
| 13 | class InfoCommand extends AbstractMagentoCommand |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * @var array |
||
| 17 | */ |
||
| 18 | protected $infos = []; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var \Magento\Framework\App\ProductMetadataInterface |
||
| 22 | */ |
||
| 23 | protected $productMetadata; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var \Magento\Customer\Model\CustomerFactory |
||
| 27 | */ |
||
| 28 | protected $customerFactory; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var \Magento\Catalog\Model\ProductFactory |
||
| 32 | */ |
||
| 33 | protected $productFactory; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var \Magento\Catalog\Model\CategoryFactory |
||
| 37 | */ |
||
| 38 | protected $categoryFactory; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var \Magento\Eav\Model\Entity\AttributeFactory |
||
| 42 | */ |
||
| 43 | protected $attributeFactory; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var \Magento\Framework\App\Cache\Type\FrontendPool |
||
| 47 | */ |
||
| 48 | protected $frontendPool; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var \Magento\Framework\Module\ModuleListInterface |
||
| 52 | */ |
||
| 53 | protected $moduleList; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var \Magento\Framework\App\DeploymentConfig |
||
| 57 | */ |
||
| 58 | protected $deploymentConfig; |
||
| 59 | |||
| 60 | protected function configure() |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @param \Magento\Framework\App\ProductMetadataInterface $productMetadata |
||
| 81 | * @param \Magento\Customer\Model\CustomerFactory $customerFactory |
||
| 82 | * @param \Magento\Catalog\Model\ProductFactory $productFactory |
||
| 83 | * @param \Magento\Catalog\Model\CategoryFactory $categoryFactory |
||
| 84 | * @param \Magento\Eav\Model\Entity\AttributeFactory $attributeFactory |
||
| 85 | * @param \Magento\Framework\App\Cache\Type\FrontendPool $frontendPool |
||
| 86 | * @param \Magento\Framework\App\DeploymentConfig $deploymentConfig |
||
| 87 | * @param \Magento\Framework\Module\ModuleListInterface $moduleList |
||
| 88 | */ |
||
| 89 | public function inject( |
||
| 90 | \Magento\Framework\App\ProductMetadataInterface $productMetadata, |
||
| 91 | \Magento\Customer\Model\CustomerFactory $customerFactory, |
||
| 92 | \Magento\Catalog\Model\ProductFactory $productFactory, |
||
| 93 | \Magento\Catalog\Model\CategoryFactory $categoryFactory, |
||
| 94 | \Magento\Eav\Model\Entity\AttributeFactory $attributeFactory, |
||
| 95 | \Magento\Framework\App\Cache\Type\FrontendPool $frontendPool, |
||
| 96 | \Magento\Framework\App\DeploymentConfig $deploymentConfig, |
||
| 97 | \Magento\Framework\Module\ModuleListInterface $moduleList |
||
| 98 | |||
| 99 | ) { |
||
| 100 | $this->productMetadata = $productMetadata; |
||
| 101 | $this->customerFactory = $customerFactory; |
||
| 102 | $this->productFactory = $productFactory; |
||
| 103 | $this->categoryFactory = $categoryFactory; |
||
| 104 | $this->attributeFactory = $attributeFactory; |
||
| 105 | $this->frontendPool = $frontendPool; |
||
| 106 | $this->deploymentConfig = $deploymentConfig; |
||
| 107 | $this->moduleList = $moduleList; |
||
| 108 | } |
||
| 109 | |||
| 110 | public function hasInfo() |
||
| 111 | { |
||
| 112 | return !empty($this->infos); |
||
| 113 | } |
||
| 114 | |||
| 115 | public function getInfo($key = null) |
||
|
|
|||
| 116 | { |
||
| 117 | if (is_null($key)) { |
||
| 118 | return $this->infos; |
||
| 119 | } |
||
| 120 | |||
| 121 | return isset($this->infos[$key]) ? $this->infos[$key] : null; |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @param \Symfony\Component\Console\Input\InputInterface $input |
||
| 126 | * @param \Symfony\Component\Console\Output\OutputInterface $output |
||
| 127 | * @return int|void |
||
| 128 | */ |
||
| 129 | protected function execute(InputInterface $input, OutputInterface $output) |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @todo there is also the product repository API...?! |
||
| 165 | */ |
||
| 166 | protected function addProductCount() |
||
| 167 | { |
||
| 168 | $this->infos['Product Count'] = $this->productFactory |
||
| 169 | ->create() |
||
| 170 | ->getCollection() |
||
| 171 | ->getSize(); |
||
| 172 | } |
||
| 173 | |||
| 174 | protected function addCustomerCount() |
||
| 175 | { |
||
| 176 | $this->infos['Customer Count'] = $this->customerFactory->create() |
||
| 177 | ->getCollection() |
||
| 178 | ->getSize(); |
||
| 179 | } |
||
| 180 | |||
| 181 | protected function addCategoryCount() |
||
| 182 | { |
||
| 183 | $this->infos['Category Count'] = $this->categoryFactory |
||
| 184 | ->create() |
||
| 185 | ->getCollection() |
||
| 186 | ->getSize(); |
||
| 187 | } |
||
| 188 | |||
| 189 | protected function addAttributeCount() |
||
| 190 | { |
||
| 191 | $this->infos['Attribute Count'] = $this->attributeFactory |
||
| 192 | ->create() |
||
| 193 | ->getCollection() |
||
| 194 | ->getSize(); |
||
| 195 | } |
||
| 196 | |||
| 197 | protected function addCacheInfos() |
||
| 198 | { |
||
| 199 | $cachePool = $this->frontendPool; |
||
| 200 | |||
| 201 | $this->infos['Cache Backend'] = get_class($cachePool->get('config')->getBackend()); |
||
| 202 | |||
| 203 | switch (get_class($cachePool->get('config')->getBackend())) { |
||
| 204 | case 'Zend_Cache_Backend_File': |
||
| 205 | case 'Cm_Cache_Backend_File': |
||
| 206 | // @TODO Where are the cache options? |
||
| 207 | //$cacheDir = $cachePool->get('config')->getBackend()->getOptions()->getCacheDir(); |
||
| 208 | //$this->infos['Cache Directory'] = $cacheDir; |
||
| 209 | break; |
||
| 210 | |||
| 211 | default: |
||
| 212 | } |
||
| 213 | } |
||
| 214 | |||
| 215 | protected function addDeploymentInfo() |
||
| 222 | |||
| 223 | protected function addVersionInfo() |
||
| 224 | { |
||
| 225 | $this->infos['Name'] = $this->productMetadata->getName(); |
||
| 226 | $this->infos['Version'] = $this->productMetadata->getVersion(); |
||
| 227 | $this->infos['Edition'] = $this->productMetadata->getEdition(); |
||
| 228 | } |
||
| 229 | |||
| 230 | protected function addVendors() |
||
| 231 | { |
||
| 247 | } |
||
| 248 |
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.