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 |
||
| 24 | abstract class AbstractArticleFactory implements ArticleFactoryInterface |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * @var array |
||
| 28 | */ |
||
| 29 | private $allowedTypes = [ |
||
| 30 | ItemInterface::TYPE_PICTURE, |
||
| 31 | ItemInterface::TYPE_FILE, |
||
| 32 | ItemInterface::TYPE_TEXT, |
||
| 33 | ItemInterface::TYPE_COMPOSITE, |
||
| 34 | ]; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var RouteProviderInterface |
||
| 38 | */ |
||
| 39 | protected $routeProvider; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * AbstractArticleFactory constructor. |
||
| 43 | * |
||
| 44 | * @param RouteProviderInterface $routeProvider |
||
| 45 | */ |
||
| 46 | 16 | public function __construct(RouteProviderInterface $routeProvider) |
|
| 50 | |||
| 51 | /** |
||
| 52 | * @param PackageInterface $package |
||
| 53 | * |
||
| 54 | * @return ArticleInterface |
||
| 55 | */ |
||
| 56 | 16 | protected function hydrateArticle(PackageInterface $package) |
|
| 75 | |||
| 76 | /** |
||
| 77 | * @param PackageInterface $package |
||
| 78 | * |
||
| 79 | * @return string |
||
| 80 | */ |
||
| 81 | 16 | View Code Duplication | protected function populateLead(PackageInterface $package) |
| 89 | |||
| 90 | /** |
||
| 91 | * @param PackageInterface $package |
||
| 92 | * |
||
| 93 | * @return string |
||
| 94 | */ |
||
| 95 | View Code Duplication | protected function populateBody(PackageInterface $package) |
|
| 103 | |||
| 104 | /** |
||
| 105 | * @param string $type |
||
| 106 | * |
||
| 107 | * @throws \InvalidArgumentException |
||
| 108 | */ |
||
| 109 | 11 | protected function ensureTypeIsAllowed(string $type) |
|
| 119 | } |
||
| 120 |
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.