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 | abstract class dbobject implements ObjectManagerAware |
||
| 16 | { |
||
| 17 | protected $guid = ''; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * |
||
| 21 | * @var ClassMetadata |
||
| 22 | */ |
||
| 23 | protected $cm; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Simple map of association fields changed during the object's lifetime |
||
| 27 | * |
||
| 28 | * We need this for some workarounds for proxy-related problems during changeset calculation |
||
| 29 | * |
||
| 30 | * @var array |
||
| 31 | */ |
||
| 32 | protected $changed_associations = array(); |
||
| 33 | |||
| 34 | /** |
||
| 35 | * {@inheritDoc} |
||
| 36 | */ |
||
| 37 | 77 | public function injectObjectManager(ObjectManager $objectmanager, ClassMetadata $classmetadata) |
|
| 45 | |||
| 46 | /** |
||
| 47 | * @return array |
||
| 48 | */ |
||
| 49 | 26 | public function __get_changed_associations() |
|
| 53 | |||
| 54 | /** |
||
| 55 | * Filter out internal stuff for var_dump |
||
| 56 | * |
||
| 57 | * This is not 100% accurate right now (e.g. metadata is not handled totally correctly), but at least it |
||
| 58 | * prevents killing the server by dumping recursively linked EntityManagers and the like |
||
| 59 | * |
||
| 60 | * @return array |
||
| 61 | */ |
||
| 62 | 1 | public function __debugInfo() |
|
| 63 | { |
||
| 64 | 1 | $this->initialize(); |
|
| 65 | 1 | $properties = array_merge($this->cm->getFieldNames(), $this->cm->getAssociationNames(), array_keys($this->cm->midgard['field_aliases'])); |
|
|
|
|||
| 66 | 1 | $properties = array_filter($properties, function($input) |
|
| 67 | { |
||
| 68 | 1 | return (strpos($input, 'metadata_') === false); |
|
| 69 | 1 | }); |
|
| 70 | 1 | $ret = array(); |
|
| 71 | 1 | foreach ($properties as $property) |
|
| 72 | { |
||
| 73 | 1 | $ret[$property] = $this->__get($property); |
|
| 74 | 1 | } |
|
| 75 | |||
| 76 | 1 | return $ret; |
|
| 77 | } |
||
| 78 | |||
| 79 | 111 | public function __set($field, $value) |
|
| 144 | |||
| 145 | 120 | public function __get($field) |
|
| 180 | |||
| 181 | 107 | public function __isset($field) |
|
| 185 | |||
| 186 | 50 | protected function populate_from_entity(dbobject $entity) |
|
| 194 | |||
| 195 | 124 | protected function initialize() |
|
| 202 | } |
||
| 203 |
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: