| Total Complexity | 41 |
| Total Lines | 163 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Db_Prefs often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Db_Prefs, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 2 | class Db_Prefs { |
||
| 3 | private $pdo; |
||
| 4 | private static $instance; |
||
| 5 | private $cache; |
||
| 6 | |||
| 7 | public function __construct() { |
||
| 12 | } |
||
| 13 | |||
| 14 | public static function get() { |
||
| 15 | if (self::$instance == null) |
||
| 16 | self::$instance = new self(); |
||
| 17 | |||
| 18 | return self::$instance; |
||
| 19 | } |
||
| 20 | |||
| 21 | public function cache() { |
||
| 22 | $user_id = $_SESSION["uid"]; |
||
| 23 | @$profile = $_SESSION["profile"]; |
||
| 24 | |||
| 25 | if (!is_numeric($profile) || !$profile || get_schema_version() < 63) $profile = null; |
||
| 26 | |||
| 27 | $sth = $this->pdo->prepare("SELECT |
||
| 28 | value,ttrss_prefs_types.type_name as type_name,ttrss_prefs.pref_name AS pref_name |
||
| 29 | FROM |
||
| 30 | ttrss_user_prefs,ttrss_prefs,ttrss_prefs_types |
||
| 31 | WHERE |
||
| 32 | (profile = :profile OR (:profile IS NULL AND profile IS NULL)) AND |
||
| 33 | ttrss_prefs.pref_name NOT LIKE '_MOBILE%' AND |
||
| 34 | ttrss_prefs_types.id = type_id AND |
||
| 35 | owner_uid = :uid AND |
||
| 36 | ttrss_user_prefs.pref_name = ttrss_prefs.pref_name"); |
||
| 37 | |||
| 38 | $sth->execute([":profile" => $profile, ":uid" => $user_id]); |
||
| 39 | |||
| 40 | while ($line = $sth->fetch()) { |
||
| 41 | if ($user_id == $_SESSION["uid"]) { |
||
| 42 | $pref_name = $line["pref_name"]; |
||
| 43 | |||
| 44 | $this->cache[$pref_name]["type"] = $line["type_name"]; |
||
| 45 | $this->cache[$pref_name]["value"] = $line["value"]; |
||
| 46 | } |
||
| 47 | } |
||
| 48 | } |
||
| 49 | |||
| 50 | public function read($pref_name, $user_id = false, $die_on_error = false) { |
||
| 51 | |||
| 52 | if (!$user_id) { |
||
| 53 | $user_id = $_SESSION["uid"]; |
||
| 54 | @$profile = $_SESSION["profile"]; |
||
| 55 | } else { |
||
| 56 | $profile = false; |
||
| 57 | } |
||
| 58 | |||
| 59 | if ($user_id == $_SESSION['uid'] && isset($this->cache[$pref_name])) { |
||
| 60 | $tuple = $this->cache[$pref_name]; |
||
| 61 | return $this->convert($tuple["value"], $tuple["type"]); |
||
| 62 | } |
||
| 63 | |||
| 64 | if (!is_numeric($profile) || !$profile || get_schema_version() < 63) $profile = null; |
||
| 65 | |||
| 66 | $sth = $this->pdo->prepare("SELECT |
||
| 67 | value,ttrss_prefs_types.type_name as type_name |
||
| 68 | FROM |
||
| 69 | ttrss_user_prefs,ttrss_prefs,ttrss_prefs_types |
||
| 70 | WHERE |
||
| 71 | (profile = :profile OR (:profile IS NULL AND profile IS NULL)) AND |
||
| 72 | ttrss_user_prefs.pref_name = :pref_name AND |
||
| 73 | ttrss_prefs_types.id = type_id AND |
||
| 74 | owner_uid = :uid AND |
||
| 75 | ttrss_user_prefs.pref_name = ttrss_prefs.pref_name"); |
||
| 76 | $sth->execute([":uid" => $user_id, ":profile" => $profile, ":pref_name" => $pref_name]); |
||
| 77 | |||
| 78 | if ($row = $sth->fetch()) { |
||
| 79 | $value = $row["value"]; |
||
| 80 | $type_name = $row["type_name"]; |
||
| 81 | |||
| 82 | if ($user_id == $_SESSION["uid"]) { |
||
| 83 | $this->cache[$pref_name]["type"] = $type_name; |
||
| 84 | $this->cache[$pref_name]["value"] = $value; |
||
| 85 | } |
||
| 86 | |||
| 87 | return $this->convert($value, $type_name); |
||
| 88 | |||
| 89 | } else if ($die_on_error) { |
||
| 90 | user_error("Fatal error, unknown preferences key: $pref_name (owner: $user_id)", E_USER_ERROR); |
||
| 91 | return null; |
||
| 92 | } else { |
||
| 93 | return null; |
||
| 94 | } |
||
| 95 | } |
||
| 96 | |||
| 97 | public function convert($value, $type_name) { |
||
| 98 | if ($type_name == "bool") { |
||
| 99 | return $value == "true"; |
||
| 100 | } else if ($type_name == "integer") { |
||
| 101 | return (int)$value; |
||
| 102 | } else { |
||
| 103 | return $value; |
||
| 104 | } |
||
| 105 | } |
||
| 106 | |||
| 107 | public function write($pref_name, $value, $user_id = false, $strip_tags = true) { |
||
| 165 | } |
||
| 166 | } |
||
| 167 | } |
||
| 168 | } |
||
| 169 |