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

Loader   B

Complexity

Total Complexity 48

Size/Duplication

Total Lines 187
Duplicated Lines 11.23 %

Coupling/Cohesion

Components 4
Dependencies 0

Importance

Changes 0
Metric Value
wmc 48
lcom 4
cbo 0
dl 21
loc 187
rs 8.5599
c 0
b 0
f 0

18 Methods

Rating   Name   Duplication   Size   Complexity  
A instance() 0 6 2
A get_root() 0 11 2
A get_delimiter() 0 4 3
A check_delimiters() 0 4 3
A get_class_name() 0 5 1
B get_extension() 0 18 8
A getImagePath() 11 11 1
A get_path() 10 10 1
A get_included_files() 0 4 1
A add_included_file() 0 4 1
B load() 0 27 8
A create_reflection_class() 0 5 1
A loadInstance() 0 14 3
A loadNew() 0 11 2
A getRoot() 0 4 1
A isLive() 0 4 1
B getRootURL() 0 12 7
A __construct() 0 5 2

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like Loader often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Loader, and based on these observations, apply Extract Interface, too.

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