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 |
||
18 | View Code Duplication | class ModifiedItemFilter implements FilterInterface |
|
|
|||
19 | { |
||
20 | /** |
||
21 | * @var CachedSourceManager |
||
22 | */ |
||
23 | protected $sourceManager; |
||
24 | |||
25 | /** |
||
26 | * @param CachedSourceManager $sourceManager |
||
27 | */ |
||
28 | 10 | public function __construct(CachedSourceManager $sourceManager) |
|
32 | |||
33 | /** |
||
34 | * @inheritdoc |
||
35 | * |
||
36 | * @param ItemBag $item |
||
37 | */ |
||
38 | 10 | public function filter(ParameterBag $item) |
|
39 | { |
||
40 | /** @var FeedItemBag $item */ |
||
41 | // if source does not exist yet, by all means process it |
||
42 | 10 | if (null === $source = $this->findSource($item)) { |
|
43 | 4 | return; |
|
44 | } |
||
45 | |||
46 | // first try modification date |
||
47 | 8 | if (null !== $mutationDate = $item->getDatetimeModified()) { |
|
48 | 6 | if ($source->getDatetimeImported() > $mutationDate) { |
|
49 | 4 | throw new FilterException('Item is not modified'); |
|
50 | } |
||
51 | } |
||
52 | |||
53 | // item is modified or we don't have enough information to determine that, either way continue. |
||
54 | 4 | } |
|
55 | |||
56 | /** |
||
57 | * @param ItemBag $item |
||
58 | * |
||
59 | * @return null|SourceInterface |
||
60 | */ |
||
61 | 10 | protected function findSource(ItemBag $item) |
|
73 | } |
||
74 |
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.