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 = array();  | 
            ||
| 68 | |||
| 69 | /**  | 
            ||
| 70 | * Constructor  | 
            ||
| 71 | *  | 
            ||
| 72 | * @param ObjectManager $objectManager  | 
            ||
| 73 | * @param string $objectName  | 
            ||
| 74 | * @param string|array $index Field or fields to find current entities by  | 
            ||
| 75 | */  | 
            ||
| 76 | public function __construct(ObjectManager $objectManager, $objectName, $index = null)  | 
            ||
| 92 | |||
| 93 | protected function ensureSupportedObjectManager(ObjectManager $objectManager)  | 
            ||
| 101 | |||
| 102 | /**  | 
            ||
| 103 | * @return boolean  | 
            ||
| 104 | */  | 
            ||
| 105 | public function getTruncate()  | 
            ||
| 109 | |||
| 110 | /**  | 
            ||
| 111 | * Set whether to truncate the table first  | 
            ||
| 112 | *  | 
            ||
| 113 | * @param boolean $truncate  | 
            ||
| 114 | *  | 
            ||
| 115 | * @return $this  | 
            ||
| 116 | */  | 
            ||
| 117 | public function setTruncate($truncate)  | 
            ||
| 123 | |||
| 124 | /**  | 
            ||
| 125 | * Disable truncation  | 
            ||
| 126 | *  | 
            ||
| 127 | * @return $this  | 
            ||
| 128 | */  | 
            ||
| 129 | public function disableTruncate()  | 
            ||
| 135 | |||
| 136 | /**  | 
            ||
| 137 | * Disable Doctrine logging  | 
            ||
| 138 | *  | 
            ||
| 139 | * @return $this  | 
            ||
| 140 | */  | 
            ||
| 141 | public function prepare()  | 
            ||
| 149 | |||
| 150 | /**  | 
            ||
| 151 | * Return a new instance of the object  | 
            ||
| 152 | *  | 
            ||
| 153 | * @return object  | 
            ||
| 154 | */  | 
            ||
| 155 | protected function getNewInstance()  | 
            ||
| 165 | |||
| 166 | /**  | 
            ||
| 167 | * Call a setter of the object  | 
            ||
| 168 | *  | 
            ||
| 169 | * @param object $object  | 
            ||
| 170 | * @param mixed $value  | 
            ||
| 171 | * @param string $setter  | 
            ||
| 172 | */  | 
            ||
| 173 | protected function setValue($object, $value, $setter)  | 
            ||
| 179 | |||
| 180 | /**  | 
            ||
| 181 | * Re-enable Doctrine logging  | 
            ||
| 182 | *  | 
            ||
| 183 | * @return $this  | 
            ||
| 184 | */  | 
            ||
| 185 | public function finish()  | 
            ||
| 190 | |||
| 191 | /**  | 
            ||
| 192 |      * {@inheritdoc} | 
            ||
| 193 | */  | 
            ||
| 194 | public function writeItem(array $item)  | 
            ||
| 203 | |||
| 204 | /**  | 
            ||
| 205 | * @param array $item  | 
            ||
| 206 | * @param object $object  | 
            ||
| 207 | */  | 
            ||
| 208 | protected function updateObject(array $item, $object)  | 
            ||
| 232 | |||
| 233 | /**  | 
            ||
| 234 | * Add the associated objects in case the item have for persist its relation  | 
            ||
| 235 | *  | 
            ||
| 236 | * @param array $item  | 
            ||
| 237 | * @param object $object  | 
            ||
| 238 | */  | 
            ||
| 239 | protected function loadAssociationObjectsToObject(array $item, $object)  | 
            ||
| 256 | |||
| 257 | /**  | 
            ||
| 258 | * Truncate the database table for this writer  | 
            ||
| 259 | */  | 
            ||
| 260 | protected function truncateTable()  | 
            ||
| 271 | |||
| 272 | /**  | 
            ||
| 273 | * Disable Doctrine logging  | 
            ||
| 274 | */  | 
            ||
| 275 | View Code Duplication | protected function disableLogging()  | 
            |
| 284 | |||
| 285 | /**  | 
            ||
| 286 | * Re-enable Doctrine logging  | 
            ||
| 287 | */  | 
            ||
| 288 | View Code Duplication | protected function reEnableLogging()  | 
            |
| 296 | |||
| 297 | /**  | 
            ||
| 298 | * Finds existing object or create a new instance  | 
            ||
| 299 | *  | 
            ||
| 300 | * @param array $item  | 
            ||
| 301 | */  | 
            ||
| 302 | protected function findOrCreateItem(array $item)  | 
            ||
| 327 | |||
| 328 | /**  | 
            ||
| 329 | * Flush and clear the object manager  | 
            ||
| 330 | */  | 
            ||
| 331 | public function flush()  | 
            ||
| 336 | }  | 
            ||
| 337 | 
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..