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 |
||
15 | class ZohoDatabaseCopier |
||
16 | { |
||
17 | /** |
||
18 | * @var Connection |
||
19 | */ |
||
20 | private $connection; |
||
21 | |||
22 | private $prefix; |
||
23 | |||
24 | /** |
||
25 | * @var ZohoChangeListener[] |
||
26 | */ |
||
27 | private $listeners; |
||
28 | |||
29 | /** |
||
30 | * @var LoggerInterface |
||
31 | */ |
||
32 | private $logger; |
||
33 | |||
34 | /** |
||
35 | * @var LocalChangesTracker |
||
36 | */ |
||
37 | private $localChangesTracker; |
||
38 | |||
39 | /** |
||
40 | * ZohoDatabaseCopier constructor. |
||
41 | * |
||
42 | * @param Connection $connection |
||
43 | * @param string $prefix Prefix for the table name in DB |
||
44 | * @param ZohoChangeListener[] $listeners The list of listeners called when a record is inserted or updated. |
||
45 | */ |
||
46 | View Code Duplication | public function __construct(Connection $connection, $prefix = 'zoho_', array $listeners = [], LoggerInterface $logger = null) |
|
|
|||
47 | { |
||
48 | $this->connection = $connection; |
||
49 | $this->prefix = $prefix; |
||
50 | $this->listeners = $listeners; |
||
51 | if ($logger === null) { |
||
52 | $this->logger = new NullLogger(); |
||
53 | } else { |
||
54 | $this->logger = $logger; |
||
55 | } |
||
56 | $this->localChangesTracker = new LocalChangesTracker($connection, $this->logger); |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * @param LoggerInterface $logger |
||
61 | */ |
||
62 | public function setLogger(LoggerInterface $logger) |
||
66 | |||
67 | /** |
||
68 | * @param AbstractZohoDao $dao |
||
69 | * @param bool $incrementalSync Whether we synchronize only the modified files or everything. |
||
70 | * @param bool $twoWaysSync |
||
71 | * |
||
72 | * @throws \Doctrine\DBAL\DBALException |
||
73 | * @throws \Doctrine\DBAL\Schema\SchemaException |
||
74 | * @throws \Wabel\Zoho\CRM\Exception\ZohoCRMResponseException |
||
75 | */ |
||
76 | public function fetchFromZoho(AbstractZohoDao $dao, $incrementalSync = true, $twoWaysSync = true) |
||
168 | |||
169 | } |
||
170 |
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.