Completed
Pull Request — master (#2)
by Jacob
07:54
created

Loader::load()   C

Complexity

Conditions 8
Paths 7

Size

Total Lines 30
Code Lines 19

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 30
rs 5.3846
cc 8
eloc 19
nc 7
nop 3
1
<?php
2
3
final class Loader
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
4
{
5
6
	private $root;
7
	private $is_live;
8
	private $included_files = array();
9
10
	private static $instance;
11
12
	private function __construct()
0 ignored issues
show
Coding Style introduced by
__construct uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
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' :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
58
			case 'controller' :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
59
			case 'model' :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
60
			case 'module' :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
61
			case 'router' :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
62
			case 'utility' :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
63
				$extension = '.class.inc.php';
64
			break;
65
			case 'view' :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
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
			// if(!file_exists($file_path))
0 ignored issues
show
Unused Code Comprehensibility introduced by
78% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
114
			// 	Debugger::logMessage("Requested file does not exist: {$type}, {$file}");
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
115
			
116
			self::instance()->add_included_file($file_path);
117
			
118
			switch($type)
119
			{
120
				case 'images' :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
121
				case 'scripts' :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
122
				case 'styles' :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
123
					echo file_get_contents($file_path);
124
				break;
125
				case 'view' :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
126
					extract($data);
127
					include($file_path);
128
				break;
129
				default :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a DEFAULT statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in the default statement.

switch ($expr) {
    default : //wrong
        doSomething();
        break;
}

switch ($expr) {
    default: //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
130
					include_once($file_path);
131
				break;
132
			}
133
		}
134
	}
135
136
	private static function create_reflection_class($file)
137
	{
138
		$class_name = self::instance()->get_class_name($file);
139
		return new ReflectionClass($class_name);
140
	}
141
142
	public static function loadInstance($type, $file)
143
	{
144
		self::load($type, $file);
145
		
146
		$reflectionObject = self::create_reflection_class($file);
147
		
148
		if(
149
			$reflectionObject->hasMethod('instance') &&
150
			$reflectionObject->getMethod('instance')->isStatic())
151
		{
152
			return $reflectionObject->getMethod('instance')->invoke(null);
153
		}
154
		trigger_error("Requested class cannot be instance'd: {$type}, {$file}");
155
	}
156
157
	public static function loadNew($type, $file, $data = array())
158
	{
159
		self::load($type, $file);
160
		
161
		$reflectionObject = self::create_reflection_class($file);
162
		
163
		if($reflectionObject->hasMethod('__construct'))
164
			return $reflectionObject->newInstanceArgs($data);
165
		else
166
			return $reflectionObject->newInstance();
167
	}
168
169
	public static function getRoot()
170
	{
171
		return self::instance()->get_root();
172
	}
173
174
	public static function isLive()
175
	{
176
		return self::instance()->is_live;
177
	}
178
179
    public static function getRootURL($site = '')
180
    {
181
        if (strlen($site) > 0) {
182
            if ($site == 'waterfalls' && self::instance()->is_live) {
183
                return 'http://www.waterfallsofthekeweenaw.com/';
184
            } else {
185
                return 'http://' . (self::instance()->is_live ? '' : 'dev.') . $site . '.jacobemerick.com/';
186
            }
187
        }
188
        return '/';
189
    }
190
191
}
192