Completed
Push — master ( 0f9479...65c76e )
by Angus
02:49
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 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 4
dl 0
loc 8
ccs 0
cts 7
cp 0
crap 6
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 42
	public function __construct(){
10 42
		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, ReportBug, 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 42
		$this->global_data['user'] = ($this->ion_auth->user() ? $this->ion_auth->user()->row() : ['username' => '']);
14 42
		$this->global_data['username'] = $this->User->username;
15
16
		//TODO: Move this to a lib or something.
17 42
		$this->global_data['analytics_tracking_id'] = $this->config->item('tracking_id');
18
19 42
		$this->global_data['theme'] = $this->User_Options->get('theme');
20 42
		$css_path = "css/main.{$this->User_Options->get('theme')}";
21 42
		$this->global_data['complied_css_path'] = asset_url()."{$css_path}.".filemtime(APPPATH . "../public/assets/{$css_path}.css").".css";
22
23 42
		$js_path = 'js/compiled.min';
24 42
		$this->global_data['complied_js_path']  = asset_url()."{$js_path}.".filemtime(APPPATH . "../public/assets/{$js_path}.js").".js";
25 42
	}
26
27 21
	public function _render_page(/*(array) $paths*/) : void {
28
		//We could just use global, but this is the only var we need in both header+footer
29 21
		$this->footer_data['page'] = $this->header_data['page'];
30
31 21
		$this->header_data['show_header'] = (array_key_exists('show_header', $this->header_data) ? $this->header_data['show_header'] : TRUE);
32 21
		$this->footer_data['show_footer'] = (array_key_exists('show_footer', $this->footer_data) ? $this->footer_data['show_footer'] : TRUE);
33
34 21
		$this->load->view('common/header', ($this->global_data + $this->header_data));
35 21
		foreach(func_get_args() as $path) {
36 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?
37
38 21
			$this->load->view($path, ($this->global_data + $this->body_data));
39
		}
40
		//using the union operator + makes sure global_data always takes priority
41
		//SEE: http://stackoverflow.com/a/2140094/1168377
42 21
		$this->load->view('common/footer', ($this->global_data + $this->footer_data));
43 21
	}
44
	public function _render_json($json_input, bool $download = FALSE, string $filenamePrefix = 'tracker') : void {
45
		$json = is_array($json_input) ? json_encode($json_input) : $json_input;
46
47
		$this->output->set_content_type('application/json', 'utf-8');
48
		$this->_render_content($json,'json', $download, $filenamePrefix);
49
	}
50
	public function _render_content(string $content, string $filenameExt, bool $download = FALSE, string $filenamePrefix = 'tracker') : void {
51
		if($download) {
52
			$date = date('Ymd_Hi', time());
53
			$this->output->set_header('Content-Disposition: attachment; filename="'.$filenamePrefix.'-'.$date.'.'.$filenameExt.'"');
54
			$this->output->set_header('Content-Length: '.strlen($content));
55
		}
56
		$this->output->set_output($content);
57
	}
58
}
59
60
class CLI_Controller extends CI_Controller {
61 1
	public function __construct() {
62 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, ReportBug, 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...
63
64
		//NOTE: This should fail, assuming routes.php does handles things properly.
65
		//      It's good to have "just in case" fallbacks though.
66 1
		is_cli() or exit("ERROR: This controller can only be called via command line: php index.php ...");
67 1
	}
68
}
69
70
/**** AUTH CONTROLLERS ****/
71
class User_Controller extends MY_Controller {
72 41
	public function __construct() {
73 41
		parent::__construct();
74
75 41
		$this->load->database();
76 41
	}
77
}
78
79
class Auth_Controller extends User_Controller {
80 8
	public function __construct(bool $redirect = TRUE) {
81 8
		parent::__construct();
82
83 8
		if(!$this->ion_auth->logged_in()) {
0 ignored issues
show
Bug introduced by
The method logged_in() does not seem to exist on object<Ion_auth_model>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
84 8
			if($redirect || $_SERVER['REQUEST_METHOD'] === 'GET') {
85 2
				$this->User->login_redirect();
86 2
				redirect('user/login');
87
			} else {
88 6
				$this->output->set_status_header(401, 'Session has expired, please re-log to continue.');
89 6
				exit_ci();
90
			}
91
		}
92
	}
93
}
94
95
class No_Auth_Controller extends User_Controller {
96 30
	public function __construct() {
97 30
		parent::__construct();
98
99 30
		if($this->ion_auth->logged_in()) redirect('/');
0 ignored issues
show
Bug introduced by
The method logged_in() does not seem to exist on object<Ion_auth_model>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
100 27
	}
101
}
102
103
class Admin_Controller extends Auth_Controller {
104
	public function __construct() {
105
		parent::__construct();
106
107
		if(!$this->ion_auth->is_admin()) {
0 ignored issues
show
Bug introduced by
The method is_admin() does not seem to exist on object<Ion_auth_model>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
108
			//user is not an admin, redirect them to front page
109
			//TODO (CHECK): Should we note that "you must be an admin to view this page"?
110
111
			redirect('/');
112
		}
113
	}
114
}
115
116
/**** AJAX CONTROLLERS ****/
117
class AJAX_Controller extends CI_Controller {
118 4
	public function __construct() {
119 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, ReportBug, 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...
120
121
		//TODO: general security stuff
122 4
	}
123
}
124