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 |
||
| 14 | class midgard_object_class |
||
| 15 | { |
||
| 16 | 10 | private static function resolve_classname($guid, $include_deleted = false) |
|
| 17 | { |
||
| 18 | 10 | $qb = connection::get_em()->createQueryBuilder(); |
|
| 19 | 10 | $qb->from('midgard:midgard_repligard', 'r') |
|
| 20 | 10 | ->select('r.typename, r.object_action') |
|
| 21 | 10 | ->where('r.guid = ?1') |
|
| 22 | 10 | ->setParameter(1, $guid); |
|
| 23 | |||
| 24 | try { |
||
| 25 | 10 | $result = $qb->getQuery()->getSingleResult(); |
|
| 26 | 10 | } catch (\Doctrine\ORM\NoResultException $e) { |
|
| 27 | 3 | $result = null; |
|
| 28 | } |
||
| 29 | 10 | if ($result === null) { |
|
| 30 | 3 | throw exception::not_exists(); |
|
| 31 | } |
||
| 32 | 10 | if ($result["object_action"] == subscriber::ACTION_PURGE) { |
|
| 33 | 2 | throw exception::object_purged(); |
|
| 34 | } |
||
| 35 | 9 | if (!$include_deleted && $result["object_action"] == subscriber::ACTION_DELETE) { |
|
| 36 | 1 | throw exception::object_deleted(); |
|
| 37 | } |
||
| 38 | 8 | if ($include_deleted && !self::has_metadata($result["typename"])) { |
|
| 39 | 1 | throw exception::invalid_property_value(); |
|
| 40 | } |
||
| 41 | |||
| 42 | 7 | return $result["typename"]; |
|
| 43 | } |
||
| 44 | |||
| 45 | 8 | public static function resolve_fqn($classname) |
|
| 46 | { |
||
| 47 | 8 | $cm = connection::get_em()->getClassMetadata('midgard:' . $classname); |
|
| 48 | 8 | return $cm->name; |
|
| 49 | } |
||
| 50 | |||
| 51 | 5 | public static function factory($classname, $id = null) |
|
| 52 | { |
||
| 53 | 5 | if ($classname === null) { |
|
| 54 | return null; |
||
| 55 | } |
||
| 56 | 5 | $classname = self::resolve_fqn($classname); |
|
| 57 | 5 | return new $classname($id); |
|
| 58 | } |
||
| 59 | |||
| 60 | 4 | public static function undelete($guid) |
|
| 61 | { |
||
| 62 | try { |
||
| 63 | 4 | $classname = self::resolve_classname($guid, true); |
|
| 64 | 4 | } catch (exception $e) { |
|
| 65 | 2 | return false; |
|
| 66 | } |
||
| 67 | 3 | $classname = self::resolve_fqn($classname); |
|
| 68 | |||
| 69 | 3 | $qb = new \midgard_query_builder($classname); |
|
| 70 | 3 | $qb->include_deleted(); |
|
| 71 | 3 | $qb->add_constraint('guid', '=', $guid); |
|
| 72 | 3 | $results = $qb->execute(); |
|
| 73 | 3 | if (count($results) === 0) { |
|
| 74 | exception::not_exists(); |
||
| 75 | return false; |
||
| 76 | } |
||
| 77 | 3 | $entity = array_shift($results); |
|
| 78 | |||
| 79 | 3 | if (!$entity->metadata_deleted) { |
|
| 80 | 1 | exception::internal(new \Exception("Object is not deleted.")); |
|
| 81 | 1 | return false; |
|
| 82 | } |
||
| 83 | |||
| 84 | try { |
||
| 85 | 3 | $om = new objectmanager(connection::get_em()); |
|
| 86 | 3 | $om->undelete($entity); |
|
| 87 | 3 | } catch (\Exception $e) { |
|
| 88 | exception::internal($e); |
||
| 89 | return false; |
||
| 90 | } |
||
| 91 | |||
| 92 | 3 | midgard_connection::get_instance()->set_error(MGD_ERR_OK); |
|
| 93 | 3 | return true; |
|
| 94 | } |
||
| 95 | |||
| 96 | 7 | public static function get_object_by_guid($guid) |
|
| 97 | { |
||
| 98 | 7 | if (!mgd_is_guid($guid)) { |
|
| 99 | 1 | throw exception::not_exists(); |
|
| 100 | } |
||
| 101 | |||
| 102 | 6 | $type = self::resolve_classname($guid); |
|
| 103 | 4 | return self::factory($type, $guid); |
|
| 104 | } |
||
| 105 | |||
| 106 | 1 | View Code Duplication | public static function get_property_up($classname) |
| 107 | { |
||
| 108 | 1 | if (is_object($classname)) { |
|
| 109 | 1 | $classname = get_class($classname); |
|
| 110 | 1 | } |
|
| 111 | 1 | $cm = connection::get_em()->getClassMetadata($classname); |
|
| 112 | 1 | return $cm->midgard['upfield']; |
|
| 113 | } |
||
| 114 | |||
| 115 | 1 | View Code Duplication | public static function get_property_parent($classname) |
| 116 | { |
||
| 117 | 1 | if (is_object($classname)) { |
|
| 118 | 1 | $classname = get_class($classname); |
|
| 119 | 1 | } |
|
| 120 | 1 | $cm = connection::get_em()->getClassMetadata($classname); |
|
| 121 | 1 | return $cm->midgard['parentfield']; |
|
| 122 | } |
||
| 123 | |||
| 124 | public static function connect_default($classname, $signal, $callback, $userdata = null) // <== check! |
||
| 125 | { |
||
| 126 | } |
||
| 127 | |||
| 128 | 5 | public static function has_metadata($classname) |
|
| 129 | { |
||
| 130 | 5 | if (is_string($classname)) { |
|
| 131 | 5 | return in_array('midgard\\portable\\storage\\interfaces\\metadata', class_implements($classname)); |
|
| 132 | } |
||
| 133 | 1 | if (is_object($classname)) { |
|
| 134 | 1 | return ($classname instanceof metadata); |
|
| 135 | } |
||
| 136 | return false; |
||
| 137 | } |
||
| 138 | |||
| 139 | public static function get_schema_value($classname, $node_name) |
||
| 142 | } |
||
| 143 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: