| Total Complexity | 46 |
| Total Lines | 198 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 2 |
Complex classes like Plugin 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 Plugin, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | class Plugin { |
||
| 29 | |||
| 30 | //directory name of plugin |
||
| 31 | protected $name = ""; |
||
| 32 | |||
| 33 | //database row |
||
| 34 | protected $row = array(); |
||
| 35 | |||
| 36 | protected $json_data = null; |
||
| 37 | |||
| 38 | protected static $allowed_types = array("library", "metaplugin", "project"); |
||
| 39 | |||
| 40 | /** |
||
| 41 | * default constructor |
||
| 42 | * |
||
| 43 | * @param string $name directory name of plugin |
||
| 44 | * @param array $row optional database row from plugin |
||
| 45 | */ |
||
| 46 | public function __construct(string $name, array $row = array()) { |
||
| 47 | $this->name = $name; |
||
| 48 | $this->row = $row; |
||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * load plugin.json file |
||
| 53 | */ |
||
| 54 | public function load () { |
||
| 55 | $file_path = PLUGIN_PATH . $this->name . "/plugin.json"; |
||
| 56 | |||
| 57 | //check, if file exists |
||
| 58 | if (!file_exists($file_path)) { |
||
| 59 | throw new IllegalStateException("plugin.json for plugin '" . $this->name . "' does not exists (expected path: '" . $file_path . "')!"); |
||
| 60 | } |
||
| 61 | |||
| 62 | $this->json_data = json_decode(file_get_contents($file_path), true); |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * get directory name of plugin |
||
| 67 | * |
||
| 68 | * @return string directory name of plugin |
||
| 69 | */ |
||
| 70 | public function getName () : string { |
||
| 72 | } |
||
| 73 | |||
| 74 | public function getPath () : string { |
||
| 75 | return PLUGIN_PATH . $this->name . "/"; |
||
| 76 | } |
||
| 77 | |||
| 78 | public function getType () : string { |
||
| 79 | $type = $this->json_data['type']; |
||
| 80 | |||
| 81 | if (!in_array($type, self::$allowed_types)) { |
||
| 82 | throw new IllegalStateException("plugin type '" . $type . "' (plugin '" . $this->name . "') is not supported!"); |
||
| 83 | } |
||
| 84 | |||
| 85 | return $type; |
||
| 86 | } |
||
| 87 | |||
| 88 | public function getTitle () : string { |
||
| 89 | return htmlentities($this->json_data['title']); |
||
| 90 | } |
||
| 91 | |||
| 92 | public function getDescription (string $lang_token = "") : string { |
||
| 93 | $desc = $this->json_data['description']; |
||
| 94 | |||
| 95 | if (is_array($desc)) { |
||
| 96 | //several languages are supported |
||
| 97 | if (empty($lang_token) || !isset($desc[$lang_token])) { |
||
| 98 | //return english description |
||
| 99 | return htmlentities($desc['en']); |
||
| 100 | } else { |
||
| 101 | return htmlentities($desc[$lang_token]); |
||
| 102 | } |
||
| 103 | } else { |
||
| 104 | //use default language |
||
| 105 | return htmlentities($desc); |
||
| 106 | } |
||
| 107 | } |
||
| 108 | |||
| 109 | public function getVersion () : string { |
||
| 110 | return $this->json_data['version']; |
||
| 111 | } |
||
| 112 | |||
| 113 | public function getInstalledVersion () : string { |
||
| 114 | return (!empty($this->row) ? $this->row['version'] : "n/a"); |
||
| 115 | } |
||
| 116 | |||
| 117 | public function getHomepage () : string { |
||
| 118 | return (isset($this->json_data['homepage']) ? $this->json_data['homepage'] : ""); |
||
| 119 | } |
||
| 120 | |||
| 121 | public function getLicense () : string { |
||
| 122 | return $this->json_data['license']; |
||
| 123 | } |
||
| 124 | |||
| 125 | public function listAuthors () : array { |
||
| 126 | return $this->json_data['authors']; |
||
| 127 | } |
||
| 128 | |||
| 129 | public function listSupportArray () : array { |
||
| 130 | return $this->json_data['support']; |
||
| 131 | } |
||
| 132 | |||
| 133 | public function hasSourceLink () : bool { |
||
| 134 | return isset($this->json_data['support']) && isset($this->json_data['support']['source']); |
||
| 135 | } |
||
| 136 | |||
| 137 | public function getSourceLink () : string { |
||
| 138 | if ($this->hasSourceLink()) { |
||
| 139 | return $this->json_data['support']['source']; |
||
| 140 | } else { |
||
| 141 | return ""; |
||
| 142 | } |
||
| 143 | } |
||
| 144 | |||
| 145 | public function hasIssuesLink () : bool { |
||
| 146 | return isset($this->json_data['support']) && isset($this->json_data['support']['issues']); |
||
| 147 | } |
||
| 148 | |||
| 149 | public function getIssuesLink () : string { |
||
| 150 | if ($this->hasIssuesLink()) { |
||
| 151 | return $this->json_data['support']['issues']; |
||
| 152 | } else { |
||
| 153 | return ""; |
||
| 154 | } |
||
| 155 | } |
||
| 156 | |||
| 157 | public function hasSupportMail () : bool { |
||
| 158 | return isset($this->json_data['support']) && isset($this->json_data['support']['email']); |
||
| 159 | } |
||
| 160 | |||
| 161 | public function getSupportMail () : string { |
||
| 166 | } |
||
| 167 | } |
||
| 168 | |||
| 169 | public function listSupportLinks () : array { |
||
| 170 | $array = array(); |
||
| 171 | |||
| 172 | if ($this->hasIssuesLink()) { |
||
| 173 | $array[] = array( |
||
| 174 | 'title' => Translator::translate("Issues"), |
||
| 175 | 'href' => $this->getIssuesLink() |
||
| 176 | ); |
||
| 177 | } |
||
| 178 | |||
| 179 | if ($this->hasSourceLink()) { |
||
| 180 | $array[] = array( |
||
| 181 | 'title' => Translator::translate("Source"), |
||
| 182 | 'href' => $this->getSourceLink() |
||
| 183 | ); |
||
| 184 | } |
||
| 185 | |||
| 186 | if ($this->hasSupportMail()) { |
||
| 187 | $array[] = array( |
||
| 188 | 'title' => Translator::translate("Mail"), |
||
| 189 | 'href' => "mailto:" . $this->getSupportMail(), |
||
| 190 | ); |
||
| 191 | } |
||
| 192 | |||
| 193 | return $array; |
||
| 194 | } |
||
| 195 | |||
| 196 | public function listKeywords () : array { |
||
| 198 | } |
||
| 199 | |||
| 200 | public function listCategories () : array { |
||
| 201 | return $this->json_data['categories']; |
||
| 202 | } |
||
| 203 | |||
| 204 | public function hasInstallJson () : bool { |
||
| 206 | } |
||
| 207 | |||
| 208 | public function getInstallJsonFile () : string { |
||
| 210 | } |
||
| 211 | |||
| 212 | public function getRequiredPlugins () : array { |
||
| 213 | return $this->json_data['require']; |
||
| 214 | } |
||
| 215 | |||
| 216 | public function isInstalled () : bool { |
||
| 217 | return (!empty($this->row) ? $this->row['installed'] == 1 : false); |
||
| 218 | } |
||
| 219 | |||
| 220 | public function isActivated () : bool { |
||
| 221 | return (!empty($this->row) ? $this->row['activated'] == 1 : false); |
||
| 222 | } |
||
| 223 | |||
| 224 | public static function castPlugin (Plugin $plugin) : Plugin { |
||
| 226 | } |
||
| 227 | |||
| 228 | } |
||
| 229 | |||
| 231 |
Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.
A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.