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:
Complex classes like DoctrineWriter often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use DoctrineWriter, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | class DoctrineWriter implements Writer, Writer\FlushableWriter |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * Doctrine object manager |
||
| 24 | * |
||
| 25 | * @var ObjectManager |
||
| 26 | */ |
||
| 27 | protected $objectManager; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Fully qualified model name |
||
| 31 | * |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | protected $objectName; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Doctrine object repository |
||
| 38 | * |
||
| 39 | * @var ObjectRepository |
||
| 40 | */ |
||
| 41 | protected $objectRepository; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var ClassMetadata |
||
| 45 | */ |
||
| 46 | protected $objectMetadata; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Original Doctrine logger |
||
| 50 | * |
||
| 51 | * @var SQLLogger |
||
| 52 | */ |
||
| 53 | protected $originalLogger; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Whether to truncate the table first |
||
| 57 | * |
||
| 58 | * @var boolean |
||
| 59 | */ |
||
| 60 | protected $truncate = true; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * List of fields used to lookup an object |
||
| 64 | * |
||
| 65 | * @var array |
||
| 66 | */ |
||
| 67 | protected $lookupFields = []; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Method used for looking up the item |
||
| 71 | * |
||
| 72 | * @var array |
||
| 73 | */ |
||
| 74 | protected $lookupMethod; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Constructor |
||
| 78 | * |
||
| 79 | * @param ObjectManager $objectManager |
||
| 80 | * @param string $objectName |
||
| 81 | * @param string|array $index Field or fields to find current entities by |
||
| 82 | * @param string $lookupMethod Method used for looking up the item |
||
| 83 | */ |
||
| 84 | 6 | public function __construct( |
|
| 115 | |||
| 116 | /** |
||
| 117 | * @return boolean |
||
| 118 | */ |
||
| 119 | public function getTruncate() |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Set whether to truncate the table first |
||
| 126 | * |
||
| 127 | * @param boolean $truncate |
||
| 128 | * |
||
| 129 | * @return $this |
||
| 130 | */ |
||
| 131 | public function setTruncate($truncate) |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Disable truncation |
||
| 140 | * |
||
| 141 | * @return $this |
||
| 142 | */ |
||
| 143 | public function disableTruncate() |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Disable Doctrine logging |
||
| 152 | * |
||
| 153 | * @return $this |
||
| 154 | */ |
||
| 155 | 1 | public function prepare() |
|
| 163 | |||
| 164 | /** |
||
| 165 | * Re-enable Doctrine logging |
||
| 166 | */ |
||
| 167 | 1 | public function finish() |
|
| 172 | |||
| 173 | /** |
||
| 174 | * {@inheritdoc} |
||
| 175 | */ |
||
| 176 | 4 | public function writeItem(array $item) |
|
| 185 | |||
| 186 | /** |
||
| 187 | * Flush and clear the object manager |
||
| 188 | */ |
||
| 189 | 1 | public function flush() |
|
| 194 | |||
| 195 | /** |
||
| 196 | * Return a new instance of the object |
||
| 197 | * |
||
| 198 | * @return object |
||
| 199 | */ |
||
| 200 | 4 | protected function getNewInstance() |
|
| 210 | |||
| 211 | /** |
||
| 212 | * Call a setter of the object |
||
| 213 | * |
||
| 214 | * @param object $object |
||
| 215 | * @param mixed $value |
||
| 216 | * @param string $setter |
||
| 217 | */ |
||
| 218 | 4 | protected function setValue($object, $value, $setter) |
|
| 224 | |||
| 225 | /** |
||
| 226 | * @param array $item |
||
| 227 | * @param object $object |
||
| 228 | */ |
||
| 229 | 4 | protected function updateObject(array $item, $object) |
|
| 251 | |||
| 252 | /** |
||
| 253 | * Add the associated objects in case the item have for persist its relation |
||
| 254 | * |
||
| 255 | * @param array $item |
||
| 256 | * @param object $object |
||
| 257 | */ |
||
| 258 | 4 | protected function loadAssociationObjectsToObject(array $item, $object) |
|
| 275 | |||
| 276 | /** |
||
| 277 | * Truncate the database table for this writer |
||
| 278 | */ |
||
| 279 | 1 | protected function truncateTable() |
|
| 290 | |||
| 291 | /** |
||
| 292 | * Disable Doctrine logging |
||
| 293 | */ |
||
| 294 | 1 | View Code Duplication | protected function disableLogging() |
| 303 | |||
| 304 | /** |
||
| 305 | * Re-enable Doctrine logging |
||
| 306 | */ |
||
| 307 | 1 | View Code Duplication | protected function reEnableLogging() |
| 315 | |||
| 316 | /** |
||
| 317 | * @param array $item |
||
| 318 | * |
||
| 319 | * @return object |
||
| 320 | */ |
||
| 321 | 4 | protected function findOrCreateItem(array $item) |
|
| 345 | |||
| 346 | 6 | protected function ensureSupportedObjectManager(ObjectManager $objectManager) |
|
| 354 | } |
||
| 355 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..