| Total Complexity | 41 |
| Total Lines | 175 |
| 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() { |
||
| 8 | $this->pdo = Db::pdo(); |
||
| 9 | $this->cache = array(); |
||
| 10 | |||
| 11 | if ($_SESSION["uid"]) { |
||
| 12 | $this->cache(); |
||
| 13 | } |
||
| 14 | } |
||
| 15 | |||
| 16 | public static function get() { |
||
| 17 | if (self::$instance == null) { |
||
| 18 | self::$instance = new self(); |
||
| 19 | } |
||
| 20 | |||
| 21 | return self::$instance; |
||
| 22 | } |
||
| 23 | |||
| 24 | public function cache() { |
||
| 25 | $user_id = $_SESSION["uid"]; |
||
| 26 | @$profile = $_SESSION["profile"]; |
||
| 27 | |||
| 28 | if (!is_numeric($profile) || !$profile || get_schema_version() < 63) { |
||
| 29 | $profile = null; |
||
| 30 | } |
||
| 31 | |||
| 32 | $sth = $this->pdo->prepare("SELECT |
||
| 33 | value,ttrss_prefs_types.type_name as type_name,ttrss_prefs.pref_name AS pref_name |
||
| 34 | FROM |
||
| 35 | ttrss_user_prefs,ttrss_prefs,ttrss_prefs_types |
||
| 36 | WHERE |
||
| 37 | (profile = :profile OR (:profile IS NULL AND profile IS NULL)) AND |
||
| 38 | ttrss_prefs.pref_name NOT LIKE '_MOBILE%' AND |
||
| 39 | ttrss_prefs_types.id = type_id AND |
||
| 40 | owner_uid = :uid AND |
||
| 41 | ttrss_user_prefs.pref_name = ttrss_prefs.pref_name"); |
||
| 42 | |||
| 43 | $sth->execute([":profile" => $profile, ":uid" => $user_id]); |
||
| 44 | |||
| 45 | while ($line = $sth->fetch()) { |
||
| 46 | if ($user_id == $_SESSION["uid"]) { |
||
| 47 | $pref_name = $line["pref_name"]; |
||
| 48 | |||
| 49 | $this->cache[$pref_name]["type"] = $line["type_name"]; |
||
| 50 | $this->cache[$pref_name]["value"] = $line["value"]; |
||
| 51 | } |
||
| 52 | } |
||
| 53 | } |
||
| 54 | |||
| 55 | public function read($pref_name, $user_id = false, $die_on_error = false) { |
||
| 56 | |||
| 57 | if (!$user_id) { |
||
| 58 | $user_id = $_SESSION["uid"]; |
||
| 59 | @$profile = $_SESSION["profile"]; |
||
| 60 | } else { |
||
| 61 | $profile = false; |
||
| 62 | } |
||
| 63 | |||
| 64 | if ($user_id == $_SESSION['uid'] && isset($this->cache[$pref_name])) { |
||
| 65 | $tuple = $this->cache[$pref_name]; |
||
| 66 | return $this->convert($tuple["value"], $tuple["type"]); |
||
| 67 | } |
||
| 68 | |||
| 69 | if (!is_numeric($profile) || !$profile || get_schema_version() < 63) { |
||
| 70 | $profile = null; |
||
| 71 | } |
||
| 72 | |||
| 73 | $sth = $this->pdo->prepare("SELECT |
||
| 74 | value,ttrss_prefs_types.type_name as type_name |
||
| 75 | FROM |
||
| 76 | ttrss_user_prefs,ttrss_prefs,ttrss_prefs_types |
||
| 77 | WHERE |
||
| 78 | (profile = :profile OR (:profile IS NULL AND profile IS NULL)) AND |
||
| 79 | ttrss_user_prefs.pref_name = :pref_name AND |
||
| 80 | ttrss_prefs_types.id = type_id AND |
||
| 81 | owner_uid = :uid AND |
||
| 82 | ttrss_user_prefs.pref_name = ttrss_prefs.pref_name"); |
||
| 83 | $sth->execute([":uid" => $user_id, ":profile" => $profile, ":pref_name" => $pref_name]); |
||
| 84 | |||
| 85 | if ($row = $sth->fetch()) { |
||
| 86 | $value = $row["value"]; |
||
| 87 | $type_name = $row["type_name"]; |
||
| 88 | |||
| 89 | if ($user_id == $_SESSION["uid"]) { |
||
| 90 | $this->cache[$pref_name]["type"] = $type_name; |
||
| 91 | $this->cache[$pref_name]["value"] = $value; |
||
| 92 | } |
||
| 93 | |||
| 94 | return $this->convert($value, $type_name); |
||
| 95 | |||
| 96 | } else if ($die_on_error) { |
||
| 97 | user_error("Fatal error, unknown preferences key: $pref_name (owner: $user_id)", E_USER_ERROR); |
||
| 98 | return null; |
||
| 99 | } else { |
||
| 100 | return null; |
||
| 101 | } |
||
| 102 | } |
||
| 103 | |||
| 104 | public function convert($value, $type_name) { |
||
| 105 | if ($type_name == "bool") { |
||
| 106 | return $value == "true"; |
||
| 107 | } else if ($type_name == "integer") { |
||
| 108 | return (int) $value; |
||
| 109 | } else { |
||
| 110 | return $value; |
||
| 111 | } |
||
| 112 | } |
||
| 113 | |||
| 114 | public function write($pref_name, $value, $user_id = false, $strip_tags = true) { |
||
| 177 | } |
||
| 178 | } |
||
| 179 | } |
||
| 180 | } |
||
| 181 |