Passed
Push — main ( efe307...afac3c )
by Nelson
01:24
created

load.php (3 issues)

1
<?php
2
3
function load_file($file, $extension, $base_dir, $classification, $parameters = null)
4
{
5
	if (isset($parameters) && is_array($parameters)) {
6
		extract($parameters);
7
	}
8
	if (file_exists($base_dir . $file . $extension)) {	
9
		require $base_dir . $file . $extension;
10
	} else {
11
		throw (new Exception("$classification <b>$file</b> no existe!"));
12
		return;		
0 ignored issues
show
return is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
13
	}
14
}
15
16
function load_lib($lib)
17
{
18
	load_file($lib, ".php", PIN_PATH . 'libs' . DS, "Librería");
19
}
20
21
function load_config($config)
22
{
23
	load_file($config, ".php", PIN_PATH . 'config' . DS, "Configuración");
24
}
25
26
27
function load_helper($helper)
28
{
29
	load_file($helper, ".php", PIN_PATH . 'helpers' . DS, "Helper");
30
}
31
32
function load_view($view, array $parameters = null)
33
{
34
	load_file($view, ".phtml", PIN_PATH . 'views' . DS, "Vista", $parameters);
35
}
36
37
function load_partial($partial, array $parameters = null)
38
{
39
	load_file($partial, ".phtml", PIN_PATH . 'partials'. DS , "Parcial", $parameters);		
40
}
41
42
function redirect_to($url)
43
{
44
	$url = PUBLIC_PATH . $url;
45
	header("Location: $url", 301);
0 ignored issues
show
301 of type integer is incompatible with the type boolean expected by parameter $replace of header(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

45
	header("Location: $url", /** @scrutinizer ignore-type */ 301);
Loading history...
46
}
47
48
//implementar autoloader para clases
49
spl_autoload_register(function($className){
50
	$file = PIN_PATH . 'libs' . DS . strtolower($className) . '.php';
51
	if (file_exists($file)) {
52
		require_once $file;
53
		return;
54
	} else {
55
		throw new Exception("$className no existe en $file", 1);		
56
	}
57
});
58
59
60
set_error_handler('handle_error');
61
set_exception_handler('handle_exception');
62
63
function handle_error($level, $message, $file, $line)
64
{
65
    if (error_reporting() !== 0) {  
66
        throw new \ErrorException($message, 0, $level, $file, $line);
67
    }
68
}
69
70
function handle_exception($exception)
71
{
72
    $code = $exception->getCode();
73
    if ($code != 404) {
74
        $code = 500;
75
    }
76
    http_response_code($code);
77
78
    if (error_reporting() !== 0) {
79
        
80
		echo "<h1>Fatal error</h1>";
81
        echo "<p>Uncaught exception: '" . get_class($exception) . "'</p>";
82
        echo "<p>Message: '" . $exception->getMessage() . "'</p>";
83
        echo "<p>Stack trace:<pre>" . $exception->getTraceAsString() . "</pre></p>";
84
        echo "<p>Thrown in '" . $exception->getFile() . "' on line " . $exception->getLine() . "</p>";
85
		
86
    }
87
}
88
89
90
function load_page_from_url($url)
91
{
92
	$content = explode('/', $url);
93
    //quitar el elemento inicial vacio
94
    array_shift($content); 
95
	
96
	$page = !empty($content[0]) ? trim($content[0]) : 'default';
97
	array_shift($content); 
98
	
99
	if (file_exists(PIN_PATH . 'pages' . DS . $page . '.php')) {
100
		require PIN_PATH . 'pages' . DS . $page . '.php';
101
	} else {
102
		throw (new Exception("La página <b>$page</b> no existe!"));
103
		return;
0 ignored issues
show
return is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
104
	}
105
	
106
	$function_to_be_load = !empty($content[0]) ? trim($content[0]) : 'index';
107
	array_shift($content); 
108
109
	if (function_exists("page_initializer")) {
110
		call_user_func("page_initializer");
111
	}
112
	
113
	if (function_exists($function_to_be_load)) {
114
		call_user_func_array($function_to_be_load, $content);		
115
	} else {
116
		throw (new Exception("La función <b>$function_to_be_load</b> no existe en la página <b>$page</b>!"));
117
		return;
118
	}
119
    
120
}