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 |
||
9 | class EntityContainer extends V2PropertyContainer implements IEntityContainer { |
||
10 | const ENTITY_DB_ID_INCLUDE = true; |
||
11 | const ENTITY_DB_ID_EXCLUDE = false; |
||
12 | |||
13 | /** |
||
14 | * @var EntityModel $model |
||
15 | */ |
||
16 | protected $model; |
||
17 | protected static $exceptionClass = 'EntityException'; |
||
18 | protected static $modelClass = 'EntityModel'; |
||
19 | |||
20 | /** |
||
21 | * @var \Common\GlobalContainer $gc |
||
22 | */ |
||
23 | protected $gc; |
||
24 | /** |
||
25 | * Link to DB which used by this EntityModel |
||
26 | * |
||
27 | * @var \db_mysql $dbStatic |
||
28 | * deprecated - replace with container ID like 'db' or 'dbAuth' |
||
29 | */ |
||
30 | protected static $dbStatic; |
||
31 | /** |
||
32 | * Service to work with rows |
||
33 | * |
||
34 | * @var \DbRowDirectOperator $rowOperator |
||
35 | */ |
||
36 | protected static $rowOperator; |
||
37 | /** |
||
38 | * Name of table for this entity |
||
39 | * |
||
40 | * @var string $tableName |
||
41 | */ |
||
42 | protected $tableName = '_table'; |
||
43 | /** |
||
44 | * Name of key field field in this table |
||
45 | * |
||
46 | * @var string $idField |
||
47 | */ |
||
48 | protected $idField = 'id'; |
||
49 | /** |
||
50 | * Property list and description |
||
51 | * |
||
52 | * propertyName => array( |
||
53 | * P_DB_FIELD => 'dbFieldName', - directly converts property to field and vice versa |
||
54 | * ) |
||
55 | * |
||
56 | * @var array[] $properties |
||
57 | */ |
||
58 | protected $properties = array(); |
||
59 | |||
60 | |||
61 | /** |
||
62 | * BuddyContainer constructor. |
||
63 | * |
||
64 | * @param GlobalContainer $gc |
||
65 | */ |
||
66 | public function __construct($gc) { |
||
73 | |||
74 | /** |
||
75 | * @return EntityModel |
||
76 | */ |
||
77 | public function getModel() { |
||
80 | |||
81 | /** |
||
82 | * @return \db_mysql |
||
83 | */ |
||
84 | public function getDbStatic() { |
||
87 | |||
88 | public function setTableName($value) { |
||
91 | |||
92 | public function getTableName() { |
||
95 | |||
96 | public function setIdField($value) { |
||
99 | |||
100 | public function getIdFieldName() { |
||
103 | |||
104 | public function importRow($row) { |
||
120 | |||
121 | protected function exportRow($withDbId = self::ENTITY_DB_ID_INCLUDE) { |
||
138 | |||
139 | /** |
||
140 | * @return array |
||
141 | */ |
||
142 | public function exportRowWithoutId() { |
||
145 | |||
146 | /** |
||
147 | * @return array |
||
148 | */ |
||
149 | public function exportRowWithId() { |
||
152 | |||
153 | // TODO - load from self DB |
||
154 | public function loadTry() { |
||
167 | |||
168 | public function isEmpty() { |
||
172 | |||
173 | public function isNew() { |
||
176 | |||
177 | public function insert(){ |
||
180 | |||
181 | public function delete(){ |
||
184 | |||
185 | } |
||
186 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.