Completed
Push — master ( 2bd0a4...b5b0a5 )
by Angus
07:46
created

MY_Controller::_render_content()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2.1481

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 4
dl 0
loc 8
ccs 2
cts 3
cp 0.6667
crap 2.1481
rs 9.4285
c 0
b 0
f 0
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
		$this->global_data['analytics_tracking_id'] = $this->config->item('tracking_id');
17 50
18
		$this->global_data['theme'] = $this->User_Options->get('theme');
19 50
		if(ENVIRONMENT !== 'development') {
20 50
			$this->global_data['compiled_css_path'] = function () {
21 50
				$css_path = "css/main.{$this->global_data['theme']}";
22 50
				return asset_url() . $css_path . filemtime(APPPATH . "../public/assets/{$css_path}.css") . '.css';
23
			};
24 50
25 50
			$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 50
		}
28
	}
29 21
30
	public function _render_page(/*(array) $paths*/) : void {
31 21
		$this->header_data['title'] = ($this->header_data['title'] !== 'Index' ? $this->header_data['title'].' - Manga Tracker' : 'Manga Tracker');
32
33 21
		//We could just use global, but this is the only var we need in both header+footer
34 21
		$this->footer_data['page'] = $this->header_data['page'];
35
36 21
		$this->header_data['show_header'] = (array_key_exists('show_header', $this->header_data) ? $this->header_data['show_header'] : TRUE);
37 21
		$this->footer_data['show_footer'] = (array_key_exists('show_footer', $this->footer_data) ? $this->footer_data['show_footer'] : TRUE);
38 21
39
		$this->global_data['notices'] = get_notices();
40 21
41
		//$this->output->enable_profiler(TRUE);
42
43
		$this->load->view('common/header', ($this->global_data + $this->header_data));
44 21
		foreach(func_get_args() as $path) {
45 21
			view_exists($path) or show_404(); //CHECK: Does this have a performance impact?
46
47
			$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
		$this->load->view('common/footer', ($this->global_data + $this->footer_data));
52
	}
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 1
			$this->output->set_header('Content-Length: '.strlen($content));
64 1
		}
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 1
		$this->output->set_status_header(403);
69 1
70
		$this->body_data['error_message'] = htmlspecialchars($errorMessage, ENT_QUOTES, 'UTF-8');
71
72
		$this->_render_page('common/403');
73
	}
74 46
}
75 46
76
class CLI_Controller extends CI_Controller {
77 46
	public function __construct() {
78 46
		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...
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 13
		is_cli() or exit('ERROR: This controller can only be called via command line: php index.php ...');
83 13
	}
84
}
85 13
86 13
/**** AUTH CONTROLLERS ****/
87 7
class User_Controller extends MY_Controller {
88 7
	public function __construct() {
89
		parent::__construct();
90 6
91 6
		$this->load->database();
92
	}
93
}
94
95
class Auth_Controller extends User_Controller {
96
	public function __construct(bool $redirect = TRUE) {
97
		parent::__construct();
98 30
99 30
		if(!$this->ion_auth->logged_in()) {
100
			if($redirect || $_SERVER['REQUEST_METHOD'] === 'GET') {
101 30
				$this->User->login_redirect();
102 27
				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
	public function __construct() {
113
		parent::__construct();
114
115
		if($this->ion_auth->logged_in()) redirect('/');
116
	}
117
}
118
119
class Admin_Controller extends Auth_Controller {
120 4
	public function __construct() {
121 4
		parent::__construct();
122
123
		if(!$this->ion_auth->is_admin()) {
124 4
			//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
	public function __construct() {
135
		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...
136
137
		//TODO: general security stuff
138
	}
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