Completed
Push — master ( 6336a8...2eca18 )
by Jacob
07:36
created

Loader::getRootURL()   B

Complexity

Conditions 7
Paths 13

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 8.8333
c 0
b 0
f 0
cc 7
nc 13
nop 1
1
<?php
2
3
final class Loader
4
{
5
6
	private $root;
7
	private $is_live;
8
	private $included_files = array();
9
10
	private static $instance;
11
12
	private function __construct()
13
	{
14
		$this->is_live = (isset($_SERVER['HTTP_HOST']) && substr($_SERVER['HTTP_HOST'], 0, 4) !== 'dev.');
15
		return $this;
0 ignored issues
show
Bug introduced by
Constructors do not have meaningful return values, anything that is returned from here is discarded. Are you sure this is correct?
Loading history...
16
	}
17
18
	public static function instance()
19
	{
20
		if(!isset(self::$instance))
21
			self::$instance = new Loader();
22
		return self::$instance;
23
	}
24
25
	private function get_root()
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
26
	{
27
		if(!isset($this->root))
28
		{
29
			$current_directory = dirname(__FILE__);
30
			$current_directory = substr($current_directory, 0, -7);
31
			$this->root = $current_directory;
32
		}
33
		
34
		return $this->root;
35
	}
36
37
	private function get_delimiter()
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
38
	{
39
		return (true || $this->is_live) ? '/' : '\\';
40
	}
41
42
	private function check_delimiters($path)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
43
	{
44
		return (true || $this->is_live) ? $path : str_replace('/', '\\', $path);
45
	}
46
47
	private static function get_class_name($path)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
48
	{
49
		$path_array = explode('/', $path);
50
		return array_pop($path_array);
51
	}
52
53
	private static function get_extension($type)
54
	{
55
		switch($type)
56
		{
57
			case 'collector' :
58
			case 'controller' :
59
			case 'model' :
60
			case 'module' :
61
			case 'router' :
62
			case 'utility' :
63
				$extension = '.class.inc.php';
64
			break;
65
			case 'view' :
66
				$extension = '.tpl.php';
67
			break;
68
		}
69
		return $extension;
0 ignored issues
show
Bug introduced by
The variable $extension 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...
70
	}
71
72 View Code Duplication
	public static function getImagePath($type, $file)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
73
	{
74
		$path = self::instance()->get_root();
75
		$path .= 'public';
76
		$path .= self::instance()->get_delimiter();
77
		$path .= $type;
78
		$path .= self::instance()->get_delimiter();
79
		$path .= self::instance()->check_delimiters($file);
80
		
81
		return $path;
82
	}
83
84 View Code Duplication
	private static function get_path($type, $file)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
85
	{
86
		$path = self::instance()->get_root();
87
		$path .= $type;
88
		$path .= self::instance()->get_delimiter();
89
		$path .= self::instance()->check_delimiters($file);
90
		$path .= self::get_extension($type);
91
		
92
		return $path;
93
	}
94
95
	private function get_included_files()
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
96
	{
97
		return $this->included_files;
98
	}
99
100
	private function add_included_file($path)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
101
	{
102
		$this->included_files[] = $path;
103
	}
104
105
	public static function load($type, $files, $data = array())
106
	{
107
		foreach((array) $files as $file)
108
		{
109
			$file_path = self::instance()->get_path($type, $file);
110
			if(in_array($file_path, self::instance()->get_included_files()) && $type !== 'view')
111
				continue;
112
			
113
			self::instance()->add_included_file($file_path);
114
			
115
			switch($type)
116
			{
117
				case 'images' :
118
				case 'scripts' :
119
				case 'styles' :
120
					echo file_get_contents($file_path);
121
				break;
122
				case 'view' :
123
					extract($data);
124
					include($file_path);
125
				break;
126
				default :
127
					include_once($file_path);
128
				break;
129
			}
130
		}
131
	}
132
133
	private static function create_reflection_class($file)
134
	{
135
		$class_name = self::instance()->get_class_name($file);
136
		return new ReflectionClass($class_name);
137
	}
138
139
	public static function loadInstance($type, $file)
140
	{
141
		self::load($type, $file);
142
		
143
		$reflectionObject = self::create_reflection_class($file);
144
		
145
		if(
146
			$reflectionObject->hasMethod('instance') &&
147
			$reflectionObject->getMethod('instance')->isStatic())
148
		{
149
			return $reflectionObject->getMethod('instance')->invoke(null);
150
		}
151
		trigger_error("Requested class cannot be instance'd: {$type}, {$file}");
152
	}
153
154
	public static function loadNew($type, $file, $data = array())
155
	{
156
		self::load($type, $file);
157
		
158
		$reflectionObject = self::create_reflection_class($file);
159
		
160
		if($reflectionObject->hasMethod('__construct'))
161
			return $reflectionObject->newInstanceArgs($data);
162
		else
163
			return $reflectionObject->newInstance();
164
	}
165
166
	public static function getRoot()
167
	{
168
		return self::instance()->get_root();
169
	}
170
171
	public static function isLive()
172
	{
173
		return self::instance()->is_live;
174
	}
175
176
    public static function getRootURL($site = '')
177
    {
178
        if (strlen($site) > 0) {
179
            $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http';
180
            if ($site == 'waterfalls' && self::instance()->is_live) {
181
                return "{$protocol}://www.waterfallsofthekeweenaw.com/";
182
            } else {
183
                return $protocol . '://' . (self::instance()->is_live ? '' : 'dev.') . $site . '.jacobemerick.com/';
184
            }
185
        }
186
        return '/';
187
    }
188
189
}
190