Complex classes like phpwikiPlugin 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 phpwikiPlugin, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class phpwikiPlugin extends Plugin { |
||
23 | |||
24 | const SEARCH_PAGENAME_EN = 'FullTextSearch'; |
||
25 | const SEARCH_PAGENAME_FR = 'RechercheEnTexteIntégral'; |
||
26 | |||
27 | public function __construct($id) { |
||
28 | parent::__construct($id); |
||
29 | $this->setScope(self::SCOPE_PROJECT); |
||
30 | |||
31 | $this->name = 'phpwiki'; |
||
32 | $this->text = 'PHPWiki'; |
||
33 | |||
34 | $this->addHook(Event::LAYOUT_SEARCH_ENTRY); |
||
35 | $this->addHook(Event::SEARCH_TYPE); |
||
36 | $this->addHook(Event::SEARCH_TYPES_PRESENTERS); |
||
37 | |||
38 | $this->addHook('backend_system_purge_files', 'purgeFiles'); |
||
39 | |||
40 | $this->addHook(Event::SERVICE_ICON); |
||
41 | $this->addHook(Event::SERVICES_ALLOWED_FOR_PROJECT); |
||
42 | $this->addHook(Event::SERVICE_PUBLIC_AREAS); |
||
43 | |||
44 | $this->addHook(Event::GET_SYSTEM_EVENT_CLASS); |
||
45 | $this->addHook(Event::SYSTEM_EVENT_GET_TYPES_FOR_DEFAULT_QUEUE); |
||
46 | $this->addHook('site_admin_option_hook'); |
||
47 | |||
48 | if ($this->isDocmanPluginActivated()) { |
||
49 | $this->addHook(PLUGIN_DOCMAN_EVENT_GET_PHPWIKI_PAGE, 'getWikiPage'); |
||
50 | } |
||
51 | |||
52 | $this->addHook('phpwiki_redirection'); |
||
53 | |||
54 | $this->addHook(Event::SERVICES_TRUNCATED_EMAILS); |
||
55 | |||
56 | $this->addHook(Event::REST_PROJECT_GET_PHPWIKI); |
||
57 | $this->addHook(Event::REST_PROJECT_OPTIONS_PHPWIKI); |
||
58 | $this->addHook(EVENT::REST_RESOURCES); |
||
59 | $this->addHook(EVENT::REST_PROJECT_RESOURCES); |
||
60 | |||
61 | } |
||
62 | |||
63 | private function isDocmanPluginActivated() { |
||
66 | |||
67 | public function getPluginInfo() { |
||
68 | if (!is_a($this->pluginInfo, 'PHPWikiPluginInfo')) { |
||
69 | $this->pluginInfo = new PHPWikiPluginInfo($this); |
||
70 | } |
||
71 | return $this->pluginInfo; |
||
72 | } |
||
73 | |||
74 | public function getServiceShortname() { |
||
77 | |||
78 | public function service_icon($params) { |
||
81 | |||
82 | public function service_public_areas($params) { |
||
83 | if ($params['project']->usesService($this->getServiceShortname())) { |
||
84 | $service = $params['project']->getService($this->getServiceShortname()); |
||
85 | $wiki = new PHPWiki($params['project']->getID()); |
||
86 | |||
87 | $presenter = new WidgetPublicAreaPresenter( |
||
88 | $service->getUrl(), |
||
89 | $GLOBALS['HTML']->getImagePath('ic/wiki.png'), |
||
90 | $this->text, |
||
91 | $wiki->getProjectPageCount() |
||
92 | ); |
||
93 | $renderer = TemplateRendererFactory::build()->getRenderer(PHPWIKI_TEMPLATE_DIR); |
||
94 | $params['areas'][] = $renderer->renderToString('widget_public_area', $presenter); |
||
95 | } |
||
96 | } |
||
97 | |||
98 | public function process(HTTPRequest $request) { |
||
102 | |||
103 | public function processAdmin(HTTPRequest $request) { |
||
107 | |||
108 | public function processUpload(HTTPRequest $request) { |
||
122 | |||
123 | public function layout_search_entry($params) { |
||
124 | $is_in_phpwiki = strpos($_SERVER['REQUEST_URI'], PHPWIKI_PLUGIN_BASE_URL . '/') !== false; |
||
125 | $params['search_entries'][] = array( |
||
126 | 'value' => $this->name, |
||
127 | 'label' => $this->text, |
||
128 | 'selected' => $is_in_phpwiki, |
||
129 | ); |
||
130 | } |
||
131 | |||
132 | public function search_type($params) { |
||
133 | $query = $params['query']; |
||
134 | $project = $query->getProject(); |
||
135 | if ($query->getTypeOfSearch() === $this->name) { |
||
136 | if (!$project->isError()) { |
||
137 | util_return_to($this->getPhpwikiSearchURI($project, $query->getWords())); |
||
138 | } |
||
139 | } |
||
140 | } |
||
141 | |||
142 | public function search_types_presenters($params) { |
||
143 | if ($this->isSearchEntryAvailable($params['project'])) { |
||
144 | $params['project_presenters'][] = new Search_SearchTypePresenter( |
||
145 | $this->name, |
||
146 | $this->text, |
||
147 | array(), |
||
148 | $this->getPhpwikiSearchURI($params['project'], $params['words']) |
||
149 | ); |
||
150 | } |
||
151 | } |
||
152 | |||
153 | private function isSearchEntryAvailable(Project $project = null) { |
||
154 | if ($project && !$project->isError()) { |
||
155 | return $project->usesService('plugin_phpwiki'); |
||
156 | } |
||
157 | return false; |
||
158 | } |
||
159 | |||
160 | private function getPhpwikiSearchURI(Project $project, $words) { |
||
161 | $project_id = $project->getID(); |
||
162 | $page_name = $this->getSearchPageName($project->getID()); |
||
163 | return $this->getPluginPath() . '/index.php?group_id=' . $project_id . '&pagename=' . urlencode($page_name) . '&s=' . urlencode($words); |
||
164 | } |
||
165 | |||
166 | private function getSearchPageName($project_id) { |
||
167 | $wiki_dao = new PHPWikiDao(); |
||
168 | $search_page = self::SEARCH_PAGENAME_EN; |
||
169 | if ($wiki_dao->searchLanguage($project_id) == 'fr_FR') { |
||
170 | $search_page = self::SEARCH_PAGENAME_FR; |
||
171 | } |
||
172 | |||
173 | return $search_page; |
||
174 | } |
||
175 | |||
176 | public function purgeFiles($params) { |
||
180 | |||
181 | public function getWikiPage($params) { |
||
182 | $project_manager = ProjectManager::instance(); |
||
183 | $project = $project_manager->getProject($params['project_id']); |
||
184 | if ($project->usesService($this->getServiceShortname())) { |
||
185 | $wiki_page = new PHPWikiPage($params['project_id'], $params['wiki_page_name']); |
||
186 | $params['phpwiki_page'] = $wiki_page; |
||
187 | } |
||
188 | } |
||
189 | |||
190 | public function phpwiki_redirection($params) { |
||
191 | $request = HTTPRequest::instance(); |
||
192 | $project = $request->getProject(); |
||
193 | if ($project && $project->usesService($this->getServiceShortname())) { |
||
194 | $requested_uri = $request->getFromServer('REQUEST_URI'); |
||
195 | $new_uri = preg_replace('/^\/wiki/', PHPWIKI_PLUGIN_BASE_URL, $requested_uri); |
||
196 | $GLOBALS['Response']->redirect($new_uri); |
||
197 | } |
||
198 | } |
||
199 | |||
200 | public function site_admin_option_hook() { |
||
203 | |||
204 | public function system_event_get_types_for_default_queue(array &$params) { |
||
207 | |||
208 | public function get_system_event_class($params) { |
||
209 | switch($params['type']) { |
||
210 | case SystemEvent_PHPWIKI_SWITCH_TO_PLUGIN::NAME: |
||
211 | $params['class'] = 'SystemEvent_PHPWIKI_SWITCH_TO_PLUGIN'; |
||
212 | $params['dependencies'] = array( |
||
213 | $this->getPHPWikiMigratorDao() |
||
214 | ); |
||
215 | } |
||
216 | } |
||
217 | |||
218 | private function getPHPWikiMigratorDao() { |
||
221 | |||
222 | public function services_truncated_emails($params) { |
||
223 | $project = $params['project']; |
||
224 | if ($project->usesService($this->getServiceShortname())) { |
||
225 | $params['services'][] = $GLOBALS['Language']->getText('plugin_phpwiki', 'service_lbl_key'); |
||
226 | } |
||
227 | } |
||
228 | |||
229 | public function rest_project_get_phpwiki($params) { |
||
230 | $user = $params['user']; |
||
231 | $project = $params['project']; |
||
232 | |||
233 | if (! $this->userCanAccessPhpWikiService($user, $project)) { |
||
234 | $class_exception = 'Luracast\Restler\RestException'; |
||
235 | throw new $class_exception(403, 'You are not allowed to access the PHPWiki plugin'); |
||
236 | } |
||
237 | |||
238 | if ($project->usesService($this->getServiceShortname())) { |
||
239 | $class = 'Tuleap\PhpWiki\REST\v1\ProjectResource'; |
||
240 | $project_resource = new $class($this->getPaginatedPHPWikiPagesFactory()); |
||
241 | $project = $params['project']; |
||
242 | |||
243 | $params['result'] = $project_resource->getPhpWikiPlugin( |
||
244 | $user, |
||
245 | $project->getID(), |
||
246 | $params['limit'], |
||
247 | $params['offset'], |
||
248 | $params['pagename'] |
||
249 | ); |
||
250 | } |
||
251 | } |
||
252 | |||
253 | public function rest_project_options_phpwiki($params) { |
||
256 | |||
257 | /** |
||
258 | * @return bool |
||
259 | */ |
||
260 | private function userCanAccessPhpWikiService(PFUser $user, Project $project) { |
||
264 | |||
265 | /** |
||
266 | * @return PaginatedPHPWikiPagesFactory |
||
267 | */ |
||
268 | private function getPaginatedPHPWikiPagesFactory() { |
||
271 | |||
272 | /** |
||
273 | * @see REST_RESOURCES |
||
274 | */ |
||
275 | public function rest_resources($params) { |
||
279 | |||
280 | /** |
||
281 | * @see Event::REST_PROJECT_RESOURCES |
||
282 | */ |
||
283 | public function rest_project_resources(array $params) { |
||
287 | } |
||
288 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.