PublicList::index()   C
last analyzed

Complexity

Conditions 17
Paths 24

Size

Total Lines 75

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 306

Importance

Changes 0
Metric Value
cc 17
nc 24
nop 2
dl 0
loc 75
ccs 0
cts 49
cp 0
crap 306
rs 5.2166
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php defined('BASEPATH') or exit('No direct script access allowed');
2
3
class PublicList extends MY_Controller {
4
	public function __construct() {
5
		parent::__construct();
6
7
8
		$this->load->library('form_validation');
9
	}
10
11
	public function index(?string $username = NULL, string $type = 'html') : void {
12
		$show_404 = FALSE;
13
14
		$type = mb_strtolower($type);
15
		if(
16
			(!is_null($username) && $this->form_validation->valid_username($username) && ($user = $this->User->get_user_by_username($username)))
17
			&& in_array($type, ['html', 'json', 'csv'])
18
			&& (($this->User->id == $user->id) || ($this->User_Options->get('enable_public_list', $user->id) == 'enabled' ? TRUE : FALSE))//&& get option enabled
19
		) {
20
			$this->header_data['title'] = "{$username}'s list";
21
			$this->header_data['page']  = "dashboard";
22
23
			$trackerData = $this->Tracker->list->get($user->id);
24
			switch($type) {
25
				case 'csv':
26
					$csvArr = [];
27
					foreach ($trackerData['series'] as $category => $categoryData) {
28
						$categoryName = $categoryData['name'];
29
30
						foreach ($categoryData['manga'] as $manga) {
31
							$csvArr[] = [
32
								$categoryName,
33
								'=HYPERLINK("' . $manga['full_title_url'] . '","' . $manga['title_data']['title'] . '")',
34
								$manga['site_data']['site'],
35
36
								'=HYPERLINK("' . $manga['generated_current_data']['url'] . '","' . addslashes($manga['generated_current_data']['number']) . '")',
37
								'=HYPERLINK("' . $manga['generated_latest_data']['url'] . '","' . addslashes($manga['generated_latest_data']['number']) . '")',
38
39
								$manga['tag_list'],
40
								$manga['mal_id'],
41
								$manga['last_updated']
42
							];
43
						}
44
					}
45
46
					$this->output->set_content_type('text/csv', 'utf-8');
47
					$this->_render_content($this->Tracker->portation->arrayToCSVRecursive($csvArr, 'Category,Title,Site,My Status,Latest Chapter,Tag List,MAL ID, Last Updated', ',', '"', FALSE, TRUE), 'csv', TRUE, 'tracker-data');
48
					break;
49
50
				case 'html':
51
					$this->body_data['trackerData']  = $trackerData['series'];
52
53
					$this->header_data['show_header'] = FALSE;
54
					$this->footer_data['show_footer'] = FALSE;
55
56
					$this->body_data['category_custom_1']      = ($this->User_Options->get('category_custom_1', $user->id) == 'enabled' ? TRUE : FALSE);
57
					$this->body_data['category_custom_1_text'] = $this->User_Options->get('category_custom_1_text', $user->id);
58
59
					$this->body_data['category_custom_2']      = ($this->User_Options->get('category_custom_2', $user->id) == 'enabled' ? TRUE : FALSE);
60
					$this->body_data['category_custom_2_text'] = $this->User_Options->get('category_custom_2_text', $user->id);
61
62
					$this->body_data['category_custom_3']      = ($this->User_Options->get('category_custom_3', $user->id) == 'enabled' ? TRUE : FALSE);
63
					$this->body_data['category_custom_3_text'] = $this->User_Options->get('category_custom_3_text', $user->id);
64
65
					$this->_render_page('User/PublicList');
66
					break;
67
68
				case 'json':
69
					$trackerData = $this->_walk_recursive_remove($trackerData, function($v, $k) {
70
						return in_array($k, ['mal_icon']);
71
					});
72
					$this->_render_json($trackerData);
73
					break;
74
75
				default:
76
					//This will never happen.
77
					break;
78
			}
79
80
		} else {
81
			$show_404 = TRUE;
82
		}
83
84
		if($show_404) show_404();
85
	}
86
87
	private function _walk_recursive_remove (array $array, callable $callback) : array {
88
		foreach ($array as $k => $v) {
89
			if (is_array($v)) {
90
				$array[$k] = $this->_walk_recursive_remove($v, $callback);
91
			} else {
92
				if ($callback($v, $k)) {
93
					unset($array[$k]);
94
				}
95
			}
96
		}
97
98
		return $array;
99
	}
100
}
101