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 | ||
| 37 | class BelongsToRelationship extends Relationship | ||
| 38 | { | ||
| 39 | protected $type = self::BELONGS_TO; | ||
| 40 | private $relatedData; | ||
| 41 | |||
| 42 | 2 | public function doprepareQuery($data) | |
|  | |||
| 43 |     { | ||
| 44 | 2 |         if (!array_key_exists($this->options['local_key'], $data)) { | |
| 45 |             throw new FieldNotFoundException("Field {$this->options['local_key']} not found for belongs to relationship query."); | ||
| 46 | } | ||
| 47 | 2 |         if (!isset($data[$this->options['local_key']])) { | |
| 48 | return; | ||
| 49 | } | ||
| 50 | 2 | $query = $this->createQuery(); | |
| 51 | 2 |         if ($this->queryPrepared) { | |
| 52 | 2 | $query->setBoundData($this->options['foreign_key'], $data[$this->options['local_key']]); | |
| 53 | View Code Duplication |         } else { | |
| 54 | 2 | $query->setTable($this->getModelInstance()->getDBStoreInformation()['quoted_table']) | |
| 55 | 2 | ->addFilter($this->options['foreign_key'], $data[$this->options['local_key']]) | |
| 56 | 2 | ->setFirstOnly(true); | |
| 57 | 2 | $this->queryPrepared = true; | |
| 58 | } | ||
| 59 | |||
| 60 | 2 | return $query; | |
| 61 | } | ||
| 62 | |||
| 63 | 2 | public function runSetup() | |
| 74 | |||
| 75 | public function preSave(&$wrapper, $value) | ||
| 83 | |||
| 84 | public function postSave(&$wrapper) | ||
| 88 | } | ||
| 89 | 
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
@returnannotation as described here.