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 |
||
| 29 | class ArticleSitemapProvider implements SitemapProviderInterface |
||
| 30 | { |
||
| 31 | /** |
||
| 32 | * @var Manager |
||
| 33 | */ |
||
| 34 | private $manager; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var DocumentFactoryInterface |
||
| 38 | */ |
||
| 39 | private $documentFactory; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var WebspaceManagerInterface |
||
| 43 | */ |
||
| 44 | private $webspaceManager; |
||
| 45 | |||
| 46 | public function __construct( |
||
| 55 | |||
| 56 | /** |
||
| 57 | * {@inheritdoc} |
||
| 58 | */ |
||
| 59 | public function build($page, $scheme, $host) |
||
| 60 | { |
||
| 61 | $repository = $this->manager->getRepository($this->documentFactory->getClass('article')); |
||
| 62 | |||
| 63 | $webspaceKeys = $this->getWebspaceKeysByHost($host); |
||
| 64 | |||
| 65 | $result = []; |
||
| 66 | $from = 0; |
||
| 67 | $size = 1000; |
||
| 68 | |||
| 69 | do { |
||
| 70 | $bulk = $this->getBulk($repository, $webspaceKeys, $from, $size); |
||
| 71 | /** @var SitemapUrl[] $alternatives */ |
||
| 72 | $sitemapUrlListByUuid = []; |
||
| 73 | |||
| 74 | /** @var ArticleViewDocumentInterface $item */ |
||
| 75 | foreach ($bulk as $item) { |
||
| 76 | // Get all webspace keys which are for the current document and current selected webspaces |
||
| 77 | $itemWebspaceKeys = array_intersect( |
||
| 78 | array_merge([$item->getMainWebspace()], $item->getAdditionalWebspaces()), |
||
| 79 | $webspaceKeys |
||
| 80 | ); |
||
| 81 | |||
| 82 | foreach ($itemWebspaceKeys as $itemWebspaceKey) { |
||
| 83 | $url = $this->buildUrl($item, $scheme, $host, $itemWebspaceKey); |
||
| 84 | |||
| 85 | $result[] = $url; |
||
| 86 | |||
| 87 | $alternativeUrlsKey = $itemWebspaceKey . '__' . $item->getUuid(); |
||
| 88 | if (!isset($sitemapUrlListByUuid[$alternativeUrlsKey])) { |
||
| 89 | $sitemapUrlListByUuid[$alternativeUrlsKey] = []; |
||
| 90 | } |
||
| 91 | |||
| 92 | $sitemapUrlListByUuid[$alternativeUrlsKey] = $this->setAlternatives( |
||
| 93 | $sitemapUrlListByUuid[$alternativeUrlsKey], |
||
| 94 | $url |
||
| 95 | ); |
||
| 96 | } |
||
| 97 | } |
||
| 98 | |||
| 99 | $from += $size; |
||
| 100 | } while ($bulk->count() > $from && $from < static::PAGE_SIZE); |
||
| 101 | |||
| 102 | return $result; |
||
| 103 | } |
||
| 104 | |||
| 105 | protected function buildUrl(ArticleViewDocumentInterface $articleView, $scheme, $host, $webspaceKey) |
||
| 106 | { |
||
| 107 | return new SitemapUrl( |
||
| 108 | $this->findUrl($articleView, $scheme, $host, $webspaceKey), |
||
| 109 | $articleView->getLocale(), |
||
| 110 | $articleView->getChanged() |
||
| 111 | ); |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @return string |
||
| 116 | */ |
||
| 117 | private function findUrl(ArticleViewDocumentInterface $articleView, $scheme, $host, $webspaceKey) |
||
| 118 | { |
||
| 119 | return $this->webspaceManager->findUrlByResourceLocator( |
||
| 120 | $articleView->getRoutePath(), |
||
| 121 | null, |
||
| 122 | $articleView->getLocale(), |
||
| 123 | $webspaceKey, |
||
| 124 | $host, |
||
| 125 | $scheme |
||
| 126 | ); |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Set alternatives to sitemap url. |
||
| 131 | * |
||
| 132 | * @param SitemapUrl[] $sitemapUrlList |
||
| 133 | * @param SitemapUrl $sitemapUrl |
||
| 134 | * |
||
| 135 | * @return SitemapUrl[] |
||
| 136 | */ |
||
| 137 | private function setAlternatives(array $sitemapUrlList, SitemapUrl $sitemapUrl) |
||
| 155 | |||
| 156 | private function getBulk(Repository $repository, $webspaceKeys, $from, $size) |
||
| 173 | |||
| 174 | /** |
||
| 175 | * {@inheritdoc} |
||
| 176 | */ |
||
| 177 | public function createSitemap($scheme, $host) |
||
| 181 | |||
| 182 | /** |
||
| 183 | * {@inheritdoc} |
||
| 184 | */ |
||
| 185 | public function getMaxPage($schema, $host) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @return string[] |
||
| 204 | */ |
||
| 205 | private function getWebspaceKeysByHost(string $host): array |
||
| 216 | |||
| 217 | public function getAlias() |
||
| 221 | } |
||
| 222 |
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.