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 |
||
36 | class UrlRewriteSubject extends AbstractProductSubject |
||
37 | { |
||
38 | |||
39 | /** |
||
40 | * The mapping for the SKU => visibility. |
||
41 | * |
||
42 | * @var array |
||
43 | */ |
||
44 | protected $entityIdVisibilityIdMapping = array(); |
||
45 | |||
46 | /** |
||
47 | * The array with the available visibility keys. |
||
48 | * |
||
49 | * @var array |
||
50 | */ |
||
51 | protected $availableVisibilities = array( |
||
52 | 'Not Visible Individually' => VisibilityKeys::VISIBILITY_NOT_VISIBLE, |
||
53 | 'Catalog' => VisibilityKeys::VISIBILITY_IN_CATALOG, |
||
54 | 'Search' => VisibilityKeys::VISIBILITY_IN_SEARCH, |
||
55 | 'Catalog, Search' => VisibilityKeys::VISIBILITY_BOTH |
||
56 | ); |
||
57 | |||
58 | /** |
||
59 | * Return's the visibility key for the passed visibility string. |
||
60 | * |
||
61 | * @param string $visibility The visibility string to return the key for |
||
62 | * |
||
63 | * @return integer The requested visibility key |
||
64 | * @throws \Exception Is thrown, if the requested visibility is not available |
||
65 | */ |
||
66 | public function getVisibilityIdByValue($visibility) |
||
82 | |||
83 | /** |
||
84 | * Return's the visibility for the passed entity ID, if it already has been mapped. The mapping will be created |
||
85 | * by calling <code>\TechDivision\Import\Product\Subjects\BunchSubject::getVisibilityIdByValue</code> which will |
||
86 | * be done by the <code>\TechDivision\Import\Product\Callbacks\VisibilityCallback</code>. |
||
87 | * |
||
88 | * @return integer The visibility ID |
||
89 | * @throws \Exception Is thrown, if the entity ID has not been mapped |
||
90 | * @see \TechDivision\Import\Product\Subjects\BunchSubject::getVisibilityIdByValue() |
||
91 | */ |
||
92 | View Code Duplication | public function getEntityIdVisibilityIdMapping() |
|
107 | |||
108 | /** |
||
109 | * Add the entity ID => visibility mapping for the actual entity ID. |
||
110 | * |
||
111 | * @param string $visibility The visibility of the actual entity to map |
||
112 | * |
||
113 | * @return void |
||
114 | */ |
||
115 | public function addEntityIdVisibilityIdMapping($visibility) |
||
119 | |||
120 | /** |
||
121 | * Return's TRUE if the store with the passed code is active, else FALSE. |
||
122 | * |
||
123 | * @param string $storeViewCode The store view code of the store to check for the active flag set to 1 |
||
124 | * |
||
125 | * @return boolean TRUE if the store is active, else FALSE |
||
126 | * @throws \Exception Is thrown, if the store with the actual code is not available |
||
127 | */ |
||
128 | View Code Duplication | public function storeIsActive($storeViewCode) |
|
143 | } |
||
144 |
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.