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 |
||
| 30 | class RouteLoader extends PaginatedLoader implements LoaderInterface |
||
| 31 | { |
||
| 32 | /** |
||
| 33 | * @var MetaFactoryInterface |
||
| 34 | */ |
||
| 35 | protected $metaFactory; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var RouteRepositoryInterface |
||
| 39 | */ |
||
| 40 | 111 | protected $routeRepository; |
|
| 41 | |||
| 42 | 111 | /** |
|
| 43 | 111 | * @var array |
|
| 44 | */ |
||
| 45 | protected $supportedParameters = ['name', 'slug', 'parent']; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * RouteLoader constructor. |
||
| 49 | * |
||
| 50 | * @param MetaFactoryInterface $metaFactory |
||
| 51 | * @param RouteRepositoryInterface $routeRepository |
||
| 52 | */ |
||
| 53 | public function __construct(MetaFactoryInterface $metaFactory, RouteRepositoryInterface $routeRepository) |
||
| 58 | |||
| 59 | /** |
||
| 60 | * {@inheritdoc} |
||
| 61 | 11 | */ |
|
| 62 | public function load($type, $parameters = [], $withoutParameters = [], $responseType = LoaderInterface::SINGLE) |
||
| 63 | 11 | { |
|
| 64 | if (LoaderInterface::SINGLE === $responseType) { |
||
| 65 | 11 | $route = $parameters['route_object'] ?? null; |
|
| 66 | 11 | if (null === $route) { |
|
| 67 | if (empty($parameters)) { |
||
| 68 | return false; |
||
| 69 | } |
||
| 70 | |||
| 71 | $criteria = new Criteria(); |
||
| 72 | foreach ($this->supportedParameters as $supportedParameter) { |
||
| 73 | if (array_key_exists($supportedParameter, $parameters)) { |
||
| 74 | $criteria->set($supportedParameter, $parameters[$supportedParameter]); |
||
| 75 | } |
||
| 76 | } |
||
| 77 | |||
| 78 | $route = $this->routeRepository->getQueryByCriteria($criteria, [], 'r') |
||
| 79 | 11 | ->setMaxResults(1) |
|
| 80 | ->getQuery() |
||
| 81 | 11 | ->getOneOrNullResult(); |
|
| 82 | } |
||
| 83 | |||
| 84 | if (null !== $route) { |
||
| 85 | return $this->metaFactory->create($route); |
||
| 86 | } |
||
| 87 | } elseif (LoaderInterface::COLLECTION === $responseType) { |
||
| 88 | $criteria = new Criteria(); |
||
| 89 | |||
| 90 | if (\array_key_exists('parent', $parameters)) { |
||
| 91 | $parent = $parameters['parent']; |
||
| 92 | if (is_numeric($parent)) { |
||
| 93 | $criteria->set('parent', $parent); |
||
| 94 | } elseif (\is_string($parent)) { |
||
| 95 | $parentObject = $this->routeRepository->getQueryByCriteria(new Criteria(['name' => $parent]), $criteria->get('order', []), 'r') |
||
| 96 | ->getQuery()->getOneOrNullResult(); |
||
| 97 | if (null !== $parentObject) { |
||
| 98 | $criteria->set('parent', $parentObject->getId()); |
||
| 99 | } |
||
| 100 | } elseif ($parent instanceof Meta && $parent->getValues() instanceof RouteInterface) { |
||
| 101 | $criteria->set('parent', $parent->getValues()->getId()); |
||
| 102 | } |
||
| 103 | } |
||
| 104 | |||
| 105 | $this->applyPaginationToCriteria($criteria, $parameters); |
||
| 106 | $countCriteria = clone $criteria; |
||
| 107 | $routesCollection = $this->routeRepository->getQueryByCriteria($criteria, $criteria->get('order', []), 'r')->getQuery()->getResult(); |
||
| 108 | |||
| 109 | View Code Duplication | if (\count($routesCollection) > 0) { |
|
|
|
|||
| 110 | $metaCollection = new MetaCollection(); |
||
| 111 | $metaCollection->setTotalItemsCount($this->routeRepository->countByCriteria($countCriteria)); |
||
| 112 | foreach ($routesCollection as $route) { |
||
| 113 | $routeMeta = $this->metaFactory->create($route); |
||
| 114 | if (null !== $routeMeta) { |
||
| 115 | $metaCollection->add($routeMeta); |
||
| 116 | } |
||
| 117 | } |
||
| 118 | unset($routesCollection, $route, $criteria); |
||
| 119 | |||
| 120 | return $metaCollection; |
||
| 121 | } |
||
| 122 | } |
||
| 123 | |||
| 124 | return false; |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Checks if Loader supports provided type. |
||
| 129 | * |
||
| 130 | * @param string $type |
||
| 131 | * |
||
| 132 | * @return bool |
||
| 133 | */ |
||
| 134 | public function isSupported(string $type): bool |
||
| 138 | } |
||
| 139 |
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.