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 |
||
| 19 | class URLHandler implements URLHandlerInterface |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * @var \eZ\Publish\Core\Persistence\Cache\CacheServiceDecorator |
||
| 23 | */ |
||
| 24 | protected $cache; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var \eZ\Publish\SPI\Persistence\Handler |
||
| 28 | */ |
||
| 29 | protected $persistenceHandler; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var \eZ\Publish\Core\Persistence\Cache\PersistenceLogger |
||
| 33 | */ |
||
| 34 | protected $logger; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Setups current handler with everything needed. |
||
| 38 | * |
||
| 39 | * @param \eZ\Publish\Core\Persistence\Cache\CacheServiceDecorator $cache |
||
| 40 | * @param \eZ\Publish\SPI\Persistence\Handler $persistenceHandler |
||
| 41 | * @param \eZ\Publish\Core\Persistence\Cache\PersistenceLogger $logger |
||
| 42 | */ |
||
| 43 | public function __construct( |
||
| 52 | |||
| 53 | /** |
||
| 54 | * {@inheritdoc} |
||
| 55 | */ |
||
| 56 | public function updateUrl($id, URLUpdateStruct $struct) |
||
| 57 | { |
||
| 58 | $this->logger->logCall(__METHOD__, [ |
||
| 59 | 'url' => $id, |
||
| 60 | 'struct' => $struct, |
||
| 61 | ]); |
||
| 62 | |||
| 63 | $url = $this->persistenceHandler->urlHandler()->updateUrl($id, $struct); |
||
| 64 | |||
| 65 | $this->cache->clear('url', $id); |
||
| 66 | $this->cache->clear('content'); |
||
| 67 | |||
| 68 | return $url; |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * {@inheritdoc} |
||
| 73 | */ |
||
| 74 | public function find(URLQuery $query) |
||
| 82 | |||
| 83 | /** |
||
| 84 | * {@inheritdoc} |
||
| 85 | */ |
||
| 86 | View Code Duplication | public function loadById($id) |
|
| 99 | |||
| 100 | /** |
||
| 101 | * {@inheritdoc} |
||
| 102 | */ |
||
| 103 | public function loadByUrl($url) |
||
| 107 | |||
| 108 | /** |
||
| 109 | * {@inheritdoc} |
||
| 110 | */ |
||
| 111 | View Code Duplication | public function findUsages($id) |
|
| 124 | } |
||
| 125 |