MY_Controller   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 48.78%

Importance

Changes 0
Metric Value
dl 0
loc 72
ccs 20
cts 41
cp 0.4878
rs 10
c 0
b 0
f 0
wmc 14
lcom 1
cbo 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A _render_json() 0 6 2
A __construct() 0 20 3
A _render_content() 0 8 2
A _render_403() 0 7 1
1
<?php defined('BASEPATH') OR exit('No direct script access allowed');
2
3
class MY_Controller extends CI_Controller {
4
	protected $header_data = array();
5
	protected $body_data   = array();
6
	protected $footer_data = array();
7
	public    $global_data = array();
8
9 28
	public function __construct(){
10 28
		parent::__construct();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class CI_Controller as the method __construct() does only exist in the following sub-classes of CI_Controller: AJAX_Controller, About, AdminCLI, AdminPanel, Admin_Controller, Auth_Controller, CLI_Controller, Dashboard, DashboardBeta, Favourites, Forgot_Password, FrontPage, GetKey, GetList, Help, History, Import_AMR, Login, Logout, MY_Controller, No_Auth_Controller, Options, PublicList, ReportIssue, Signup, Stats, TitleHistory, TrackerInline, UpdateStatus, User_Controller, UsernameCheck, Userscript, Welcome. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
11
12
		//FIXME: This is pretty much a phpUnit hack. Without it phpUnit fails here. We need a proper way to fake user/admin testing.
13 28
		$this->global_data['user'] = ($this->ion_auth->user() ? $this->ion_auth->user()->row() : ['username' => '']);
14 28
		$this->global_data['username'] = $this->User->username;
15
16 28
		$this->global_data['analytics_tracking_id'] = $this->config->item('tracking_id');
17
18 28
		$this->global_data['theme'] = $this->User_Options->get('theme');
19 28
		if(ENVIRONMENT === 'production') {
20
			$this->global_data['compiled_css_path'] = function () {
21
				$css_path = "css/main.{$this->global_data['theme']}";
22
				return asset_url() . $css_path . '.' . filemtime(APPPATH . "../public/assets/{$css_path}.css") . '.css';
23
			};
24
25
			$js_path = 'js/compiled.min';
26
			$this->global_data['compiled_js_path'] = asset_url() . $js_path . '.' . filemtime(APPPATH . "../public/assets/{$js_path}.js") . '.js';
27
		}
28 28
	}
29
30 17
	public function _render_page(/*(array) $paths*/) : void {
31 17
		$this->header_data['title'] = ($this->header_data['title'] !== 'Index' ? $this->header_data['title'].' - Manga Tracker' : 'Manga Tracker');
32
33
		//We could just use global, but this is the only var we need in both header+footer
34 17
		$this->footer_data['page'] = $this->header_data['page'];
35
36 17
		$this->header_data['show_header'] = (array_key_exists('show_header', $this->header_data) ? $this->header_data['show_header'] : TRUE);
37 17
		$this->footer_data['show_footer'] = (array_key_exists('show_footer', $this->footer_data) ? $this->footer_data['show_footer'] : TRUE);
38
39 17
		$this->global_data['notices'] = get_notices();
40
41
		//$this->output->enable_profiler(TRUE);
42
43 17
		$this->load->view('common/header', ($this->global_data + $this->header_data));
44 17
		foreach(func_get_args() as $path) {
45 17
			view_exists($path) or show_404(); //CHECK: Does this have a performance impact?
46
47 17
			$this->load->view($path, ($this->global_data + $this->body_data));
48
		}
49
		//using the union operator + makes sure global_data always takes priority
50
		//SEE: http://stackoverflow.com/a/2140094/1168377
51 17
		$this->load->view('common/footer', ($this->global_data + $this->footer_data));
52 17
	}
53
	public function _render_json($json_input, bool $download = FALSE, string $filenamePrefix = 'tracker') : void {
54
		$json = is_array($json_input) ? json_encode($json_input) : $json_input;
55
56
		$this->output->set_content_type('application/json', 'utf-8');
57
		$this->_render_content($json ?? '{}','json', $download, $filenamePrefix);
58
	}
59
	public function _render_content(string $content, string $filenameExt, bool $download = FALSE, string $filenamePrefix = 'tracker') : void {
60
		if($download) {
61
			$date = date('Ymd_Hi');
62
			$this->output->set_header('Content-Disposition: attachment; filename="'.$filenamePrefix.'-'.$date.'.'.$filenameExt.'"');
63
			$this->output->set_header('Content-Length: '.strlen($content));
64
		}
65
		$this->output->set_output($content);
66
	}
67
	public function _render_403(string $errorMessage = 'The user has insufficient permissions to perform that action.') : void {
68
		$this->output->set_status_header(403);
69
70
		$this->body_data['error_message'] = htmlspecialchars($errorMessage, ENT_QUOTES, 'UTF-8');
71
72
		$this->_render_page('common/403');
73
	}
74
}
75
76
class CLI_Controller extends CI_Controller {
77 1
	public function __construct() {
78 1
		parent::__construct();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class CI_Controller as the method __construct() does only exist in the following sub-classes of CI_Controller: AJAX_Controller, About, AdminCLI, AdminPanel, Admin_Controller, Auth_Controller, CLI_Controller, Dashboard, DashboardBeta, Favourites, Forgot_Password, FrontPage, GetKey, GetList, Help, History, Import_AMR, Login, Logout, MY_Controller, No_Auth_Controller, Options, PublicList, ReportIssue, Signup, Stats, TitleHistory, TrackerInline, UpdateStatus, User_Controller, UsernameCheck, Userscript, Welcome. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
79
80
		//NOTE: This should fail, assuming routes.php does handles things properly.
81
		//      It's good to have "just in case" fallbacks though.
82 1
		is_cli() or exit('ERROR: This controller can only be called via command line: php index.php ...');
83 1
	}
84
}
85
86
/**** AUTH CONTROLLERS ****/
87
class User_Controller extends MY_Controller {
88 26
	public function __construct() {
89 26
		parent::__construct();
90
91 26
		$this->load->database();
92 26
	}
93
}
94
95
class Auth_Controller extends User_Controller {
96 1
	public function __construct(bool $redirect = TRUE) {
97 1
		parent::__construct();
98
99 1
		if(!$this->ion_auth->logged_in()) {
100 1
			if($redirect || $_SERVER['REQUEST_METHOD'] === 'GET') {
101 1
				$this->User->login_redirect();
102 1
				redirect('user/login');
103
			} else {
104
				$this->output->set_status_header(401, 'Session has expired, please re-log to continue.');
105
				exit_ci();
106
			}
107
		}
108
	}
109
}
110
111
class No_Auth_Controller extends User_Controller {
112 23
	public function __construct() {
113 23
		parent::__construct();
114
115 23
		if($this->ion_auth->logged_in()) redirect('/');
116 20
	}
117
}
118
119
class Admin_Controller extends Auth_Controller {
120
	public function __construct() {
121
		parent::__construct();
122
123
		if(!$this->ion_auth->is_admin()) {
124
			//user is not an admin, redirect them to front page
125
			//TODO (CHECK): Should we note that "you must be an admin to view this page"?
126
127
			redirect('/');
128
		}
129
	}
130
}
131
132
/**** AJAX CONTROLLERS ****/
133
class AJAX_Controller extends CI_Controller {
134 1
	public function __construct() {
135 1
		parent::__construct();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class CI_Controller as the method __construct() does only exist in the following sub-classes of CI_Controller: AJAX_Controller, About, AdminCLI, AdminPanel, Admin_Controller, Auth_Controller, CLI_Controller, Dashboard, DashboardBeta, Favourites, Forgot_Password, FrontPage, GetKey, GetList, Help, History, Import_AMR, Login, Logout, MY_Controller, No_Auth_Controller, Options, PublicList, ReportIssue, Signup, Stats, TitleHistory, TrackerInline, UpdateStatus, User_Controller, UsernameCheck, Userscript, Welcome. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
136
137
		//TODO: general security stuff
138 1
	}
139
140
	public function _render_json($json_input) : void {
141
		$json = is_array($json_input) ? json_encode($json_input) : $json_input;
142
143
		$this->output->set_content_type('application/json', 'utf-8');
144
		$this->output->set_output($json);
145
	}
146
}
147