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 | throw exception::not_exists(); |
|
| 28 | } |
||
| 29 | |||
| 30 | 10 | if ($result["object_action"] == subscriber::ACTION_PURGE) { |
|
| 31 | 2 | throw exception::object_purged(); |
|
| 32 | } |
||
| 33 | 9 | if (!$include_deleted && $result["object_action"] == subscriber::ACTION_DELETE) { |
|
| 34 | 1 | throw exception::object_deleted(); |
|
| 35 | } |
||
| 36 | 8 | if ($include_deleted && !self::has_metadata($result["typename"])) { |
|
| 37 | 1 | throw exception::invalid_property_value(); |
|
| 38 | } |
||
| 39 | |||
| 40 | 7 | return $result["typename"]; |
|
| 41 | } |
||
| 42 | |||
| 43 | 8 | public static function resolve_fqn($classname) |
|
| 44 | { |
||
| 45 | 8 | $cm = connection::get_em()->getClassMetadata('midgard:' . $classname); |
|
| 46 | 8 | return $cm->name; |
|
| 47 | } |
||
| 48 | |||
| 49 | 5 | public static function factory($classname, $id = null) |
|
| 50 | { |
||
| 51 | 5 | if ($classname === null) { |
|
| 52 | return null; |
||
| 53 | } |
||
| 54 | 5 | $classname = self::resolve_fqn($classname); |
|
| 55 | 5 | return new $classname($id); |
|
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @return boolean |
||
| 60 | */ |
||
| 61 | 4 | public static function undelete($guid) |
|
| 62 | { |
||
| 63 | try { |
||
| 64 | 4 | $classname = self::resolve_classname($guid, true); |
|
| 65 | 4 | } catch (exception $e) { |
|
| 66 | 2 | return false; |
|
| 67 | } |
||
| 68 | 3 | $classname = self::resolve_fqn($classname); |
|
| 69 | |||
| 70 | 3 | $qb = new \midgard_query_builder($classname); |
|
| 71 | 3 | $qb->include_deleted(); |
|
| 72 | 3 | $qb->add_constraint('guid', '=', $guid); |
|
| 73 | 3 | $results = $qb->execute(); |
|
| 74 | 3 | if (count($results) === 0) { |
|
| 75 | exception::not_exists(); |
||
| 76 | return false; |
||
| 77 | } |
||
| 78 | 3 | $entity = array_shift($results); |
|
| 79 | |||
| 80 | 3 | if (!$entity->metadata_deleted) { |
|
| 81 | 1 | exception::internal(new \Exception("Object is not deleted.")); |
|
| 82 | 1 | return false; |
|
| 83 | } |
||
| 84 | |||
| 85 | try { |
||
| 86 | 3 | $om = new objectmanager(connection::get_em()); |
|
| 87 | 3 | $om->undelete($entity); |
|
| 88 | 3 | } catch (\Exception $e) { |
|
| 89 | exception::internal($e); |
||
| 90 | return false; |
||
| 91 | } |
||
| 92 | |||
| 93 | 3 | midgard_connection::get_instance()->set_error(MGD_ERR_OK); |
|
| 94 | 3 | return true; |
|
| 95 | } |
||
| 96 | |||
| 97 | 7 | public static function get_object_by_guid($guid) |
|
| 98 | { |
||
| 99 | 7 | if (!mgd_is_guid($guid)) { |
|
| 100 | 1 | throw exception::not_exists(); |
|
| 101 | } |
||
| 102 | |||
| 103 | 6 | $type = self::resolve_classname($guid); |
|
| 104 | 4 | return self::factory($type, $guid); |
|
| 105 | } |
||
| 106 | |||
| 107 | 2 | public static function get_property_up($classname) |
|
| 108 | { |
||
| 109 | 2 | if (is_object($classname)) { |
|
| 110 | 1 | $classname = get_class($classname); |
|
| 111 | 1 | } |
|
| 112 | 2 | $cm = connection::get_em()->getClassMetadata($classname); |
|
| 113 | 2 | return $cm->midgard['upfield']; |
|
| 114 | } |
||
| 115 | |||
| 116 | 3 | public static function get_property_parent($classname) |
|
| 117 | { |
||
| 118 | 2 | if (is_object($classname)) { |
|
| 119 | 1 | $classname = get_class($classname); |
|
| 120 | 1 | } |
|
| 121 | 2 | $cm = connection::get_em()->getClassMetadata($classname); |
|
| 122 | 2 | return $cm->midgard['parentfield']; |
|
| 123 | 3 | } |
|
| 124 | |||
| 125 | public static function connect_default($classname, $signal, $callback, $userdata = null) // <== check! |
||
| 126 | { |
||
| 127 | } |
||
| 128 | |||
| 129 | 7 | public static function has_metadata($classname) |
|
| 138 | } |
||
| 139 | |||
| 140 | public static function get_schema_value($classname, $node_name) |
||
| 142 | } |
||
| 143 | } |
||
| 144 |
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: