Passed
Branch main (ea6e86)
by Nelson
01:12
created
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(string $lib)
17
{
18
	load_file($lib, ".php", PIN_PATH . 'libs' . DS, "Librería");
19
}
20
21
function load_config(string $config)
22
{
23
	load_file($config, ".php", PIN_PATH . 'config' . DS, "Configuración");
24
}
25
26
27
function load_helper(string $helper)
28
{
29
	load_file($helper, ".php", PIN_PATH . 'helpers' . DS, "Helper");
30
}
31
32
function load_view(string $view, array $parameters = null)
33
{
34
	load_file($view, ".phtml", PIN_PATH . 'views' . DS, "Vista", $parameters);
35
}
36
37
function load_partial(string $partial, array $parameters = null)
38
{
39
	load_file($partial, ".phtml", PIN_PATH . 'partials'. DS , "Parcial", $parameters);		
40
}
41
42
function redirect_to(string $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
49
set_error_handler('handle_error');
50
set_exception_handler('handle_exception');
51
52
function handle_error($level, $message, $file, $line)
53
{
54
    if (error_reporting() !== 0) {  
55
        throw new \ErrorException($message, 0, $level, $file, $line);
56
    }
57
}
58
59
function handle_exception($exception)
60
{
61
    $code = $exception->getCode();
62
    if ($code != 404) {
63
        $code = 500;
64
    }
65
    http_response_code($code);
66
67
    if (error_reporting() !== 0) {
68
        
69
		load_partial("templates/header");
70
        echo "<h1>Fatal error</h1>";
71
        echo "<p>Uncaught exception: '" . get_class($exception) . "'</p>";
72
        echo "<p>Message: '" . $exception->getMessage() . "'</p>";
73
        echo "<p>Stack trace:<pre>" . $exception->getTraceAsString() . "</pre></p>";
74
        echo "<p>Thrown in '" . $exception->getFile() . "' on line " . $exception->getLine() . "</p>";
75
		load_partial("templates/footer");
76
    }
77
}
78
79
80
function load_page_from_url($url)
81
{
82
	$content = explode('/', $url);
83
    //quitar el elemento inicial vacio
84
    array_shift($content); 
85
	
86
	$page = !empty($content[0]) ? trim($content[0]) : 'default';
87
	array_shift($content); 
88
	
89
	if (file_exists(PIN_PATH . 'pages' . DS . $page . '.php')) {
90
		require PIN_PATH . 'pages' . DS . $page . '.php';
91
	} else {
92
		throw (new Exception("La página <b>$page</b> no existe!"));
93
		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...
94
	}
95
	
96
	$function_to_be_load = !empty($content[0]) ? trim($content[0]) : 'index';
97
	array_shift($content); 
98
99
	if (function_exists("page_initializer")) {
100
		call_user_func("page_initializer");
101
	}
102
	
103
	if (function_exists($function_to_be_load)) {
104
		call_user_func($function_to_be_load, $content);
105
	} else {
106
		throw (new Exception("La función <b>$function_to_be_load</b> no existe en la página <b>$page</b>!"));
107
		return;
108
	}
109
    
110
}