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 |
||
33 | class HasManyRelationship extends Relationship |
||
34 | { |
||
35 | protected $type = self::HAS_MANY; |
||
36 | private $tempData; |
||
37 | |||
38 | 2 | public function doprepareQuery($data) |
|
|
|||
39 | { |
||
40 | // @todo throw an exception when the data doesn't have the local key |
||
41 | 2 | $query = $this->createQuery(); |
|
42 | 2 | View Code Duplication | if(!$this->queryPrepared) { |
43 | 2 | $query->setTable($this->getModelInstance()->getDBStoreInformation()['quoted_table']) |
|
44 | 2 | ->addFilter($this->options['foreign_key'], $data[$this->options['local_key']] ?? null); |
|
45 | 2 | $this->queryPrepared = true; |
|
46 | 2 | return $query; |
|
47 | } |
||
48 | if(isset($data[$this->options['local_key']])) { |
||
49 | $query->setBoundData($this->options['foreign_key'], $data[$this->options['local_key']]); |
||
50 | } |
||
51 | return $query; |
||
52 | } |
||
53 | |||
54 | 2 | public function runSetup() |
|
63 | |||
64 | public function preSave(&$wrapper, $value) |
||
69 | |||
70 | public function postSave(&$wrapper) |
||
82 | } |
||
83 |
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@return
annotation as described here.