Completed
Push — master ( a1095d...66879a )
by Angus
03:23
created

MY_Controller   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 6

Test Coverage

Coverage 64.71%

Importance

Changes 0
Metric Value
dl 0
loc 58
ccs 22
cts 34
cp 0.6471
rs 10
c 0
b 0
f 0
wmc 12
lcom 2
cbo 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A _render_json() 0 6 2
A _render_content() 0 8 2
A __construct() 0 19 3
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 50
	public function __construct(){
10 50
		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, DashboardBeta, Favourites, Forgot_Password, GetKey, GetList, Help, History, Import_AMR, IndexC, 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 50
		$this->global_data['user'] = ($this->ion_auth->user() ? $this->ion_auth->user()->row() : ['username' => '']);
14 50
		$this->global_data['username'] = $this->User->username;
15
16
		//TODO: Move this to a lib or something.
17 50
		$this->global_data['analytics_tracking_id'] = $this->config->item('tracking_id');
18
19 50
		$this->global_data['theme'] = $this->User_Options->get('theme');
20 50
		if(ENVIRONMENT !== 'development') {
21 50
			$css_path = "css/main.{$this->User_Options->get('theme')}";
22 50
			$this->global_data['complied_css_path'] = asset_url()."{$css_path}.".filemtime(APPPATH . "../public/assets/{$css_path}.css").".css";
23
24 50
			$js_path = 'js/compiled.min';
25 50
			$this->global_data['complied_js_path']  = asset_url()."{$js_path}.".filemtime(APPPATH . "../public/assets/{$js_path}.js").".js";
26
		}
27 50
	}
28
29 21
	public function _render_page(/*(array) $paths*/) : void {
30
		//We could just use global, but this is the only var we need in both header+footer
31 21
		$this->footer_data['page'] = $this->header_data['page'];
32
33 21
		$this->header_data['show_header'] = (array_key_exists('show_header', $this->header_data) ? $this->header_data['show_header'] : TRUE);
34 21
		$this->footer_data['show_footer'] = (array_key_exists('show_footer', $this->footer_data) ? $this->footer_data['show_footer'] : TRUE);
35
36 21
		$this->load->view('common/header', ($this->global_data + $this->header_data));
37 21
		foreach(func_get_args() as $path) {
38 21
			view_exists($path) or show_404(); //TODO (FIXME): This seems bad performance wise in the long run. Is there any reason to have it in production?
39
40 21
			$this->load->view($path, ($this->global_data + $this->body_data));
41
		}
42
		//using the union operator + makes sure global_data always takes priority
43
		//SEE: http://stackoverflow.com/a/2140094/1168377
44 21
		$this->load->view('common/footer', ($this->global_data + $this->footer_data));
45 21
	}
46
	public function _render_json($json_input, bool $download = FALSE, string $filenamePrefix = 'tracker') : void {
47
		$json = is_array($json_input) ? json_encode($json_input) : $json_input;
48
49
		$this->output->set_content_type('application/json', 'utf-8');
50
		$this->_render_content($json ?? '{}','json', $download, $filenamePrefix);
51
	}
52
	public function _render_content(string $content, string $filenameExt, bool $download = FALSE, string $filenamePrefix = 'tracker') : void {
53
		if($download) {
54
			$date = date('Ymd_Hi', time());
55
			$this->output->set_header('Content-Disposition: attachment; filename="'.$filenamePrefix.'-'.$date.'.'.$filenameExt.'"');
56
			$this->output->set_header('Content-Length: '.strlen($content));
57
		}
58
		$this->output->set_output($content);
59
	}
60
}
61
62
class CLI_Controller extends CI_Controller {
63 1
	public function __construct() {
64 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, DashboardBeta, Favourites, Forgot_Password, GetKey, GetList, Help, History, Import_AMR, IndexC, 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...
65
66
		//NOTE: This should fail, assuming routes.php does handles things properly.
67
		//      It's good to have "just in case" fallbacks though.
68 1
		is_cli() or exit("ERROR: This controller can only be called via command line: php index.php ...");
69 1
	}
70
}
71
72
/**** AUTH CONTROLLERS ****/
73
class User_Controller extends MY_Controller {
74 46
	public function __construct() {
75 46
		parent::__construct();
76
77 46
		$this->load->database();
78 46
	}
79
}
80
81
class Auth_Controller extends User_Controller {
82 13
	public function __construct(bool $redirect = TRUE) {
83 13
		parent::__construct();
84
85 13
		if(!$this->ion_auth->logged_in()) {
86 13
			if($redirect || $_SERVER['REQUEST_METHOD'] === 'GET') {
87 7
				$this->User->login_redirect();
88 7
				redirect('user/login');
89
			} else {
90 6
				$this->output->set_status_header(401, 'Session has expired, please re-log to continue.');
91 6
				exit_ci();
92
			}
93
		}
94
	}
95
}
96
97
class No_Auth_Controller extends User_Controller {
98 30
	public function __construct() {
99 30
		parent::__construct();
100
101 30
		if($this->ion_auth->logged_in()) redirect('/');
102 27
	}
103
}
104
105
class Admin_Controller extends Auth_Controller {
106
	public function __construct() {
107
		parent::__construct();
108
109
		if(!$this->ion_auth->is_admin()) {
110
			//user is not an admin, redirect them to front page
111
			//TODO (CHECK): Should we note that "you must be an admin to view this page"?
112
113
			redirect('/');
114
		}
115
	}
116
}
117
118
/**** AJAX CONTROLLERS ****/
119
class AJAX_Controller extends CI_Controller {
120 4
	public function __construct() {
121 4
		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, DashboardBeta, Favourites, Forgot_Password, GetKey, GetList, Help, History, Import_AMR, IndexC, 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...
122
123
		//TODO: general security stuff
124 4
	}
125
}
126