MY_Config::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 3
cts 3
cp 1
crap 1
1
<?php defined('BASEPATH') OR exit('No direct script access allowed');
2
3
class MY_Config extends CI_Config {
4 96
	public function __construct() {
5 96
		parent::__construct();
6 96
	}
7
	/**
8
	 * Load Config File
9
	 * Tweaked by @DakuTree to allow loading from config/_secure. This is intended to stop duplication of API keys (or other settings) that shouldn't be committed.
10
	 *
11
	 * @param	string	$file			Configuration file name
12
	 * @param	bool	$use_sections		Whether configuration values should be loaded into their own section
13
	 * @param	bool	$fail_gracefully	Whether to just return FALSE or display an error message
14
	 * @return	bool	TRUE if the file was loaded correctly or FALSE on failure
15
	 */
16 96
	public function load($file = '', $use_sections = FALSE, $fail_gracefully = FALSE)
17
	{
18 96
		$file = ($file === '') ? 'config' : str_replace('.php', '', $file);
19 96
		$loaded = FALSE;
20
21 96
		foreach ($this->_config_paths as $path)
22
		{
23 96
			foreach (array($file, '_secure'.DIRECTORY_SEPARATOR.$file, ENVIRONMENT.DIRECTORY_SEPARATOR.$file) as $location)
24
			{
25 96
				$file_path = $path.'config/'.$location.'.php';
26 96
				if (in_array($file_path, $this->is_loaded, TRUE))
27
				{
28 93
					return TRUE;
29
				}
30
31 96
				if ( ! file_exists($file_path))
32
				{
33 96
					continue;
34
				}
35
36 96
				include($file_path);
37
38 96
				if ( ! isset($config) OR ! is_array($config))
39
				{
40
					if ($fail_gracefully === TRUE)
41
					{
42
						return FALSE;
43
					}
44
45
					show_error('Your '.$file_path.' file does not appear to contain a valid configuration array.');
46
				}
47
48 96
				if ($use_sections === TRUE)
49
				{
50 93
					$this->config[$file] = isset($this->config[$file])
51
						? array_merge($this->config[$file], $config)
0 ignored issues
show
Bug introduced by
The variable $config does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
52 93
						: $config;
53
				}
54
				else
55
				{
56 96
					$this->config = array_merge($this->config, $config);
57
				}
58
59 96
				$this->is_loaded[] = $file_path;
60 96
				$config = NULL;
61 96
				$loaded = TRUE;
62 96
				log_message('debug', 'Config file loaded: '.$file_path);
0 ignored issues
show
Unused Code introduced by
The call to the function log_message() seems unnecessary as the function has no side-effects.
Loading history...
63
			}
64
		}
65
66 96
		if ($loaded === TRUE)
67
		{
68 96
			return TRUE;
69
		}
70
		elseif ($fail_gracefully === TRUE)
71
		{
72
			return FALSE;
73
		}
74
75
		show_error('The configuration file '.$file.'.php does not exist.');
76
	}
77
78
	/**
79
	 * Set a config file item by index
80
	 *
81
	 * @param	string	$item	Config item key
82
	 * @param	string	$value	Config item value
83
	 * @param	string	$index	Config item index
84
	 * @return	void
85
	 */
86
	public function set_item_by_index($item, $value, $index) : void {
87
		$this->config[$index][$item] = $value;
88
	}
89
}
90