Kodoc_Controller::__call()   C
last analyzed

Complexity

Conditions 8
Paths 21

Size

Total Lines 48
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 8
eloc 22
c 2
b 1
f 0
nc 21
nop 2
dl 0
loc 48
rs 5.9322
1
<?php defined('SYSPATH') or die('No direct access allowed.');
2
3
class Kodoc_Controller extends Template_Controller
4
{
5
6
    // Do not allow to run in production
7
    const ALLOW_PRODUCTION = false;
8
9
    public $template = 'kodoc/template';
10
11
    // Kodoc instance
12
    protected $kodoc;
13
14
    public function __construct()
15
    {
16
        parent::__construct();
17
18
        $active = $this->uri->segment(2) ? $this->uri->segment(2) : 'core';
0 ignored issues
show
Bug introduced by
The property uri does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
19
20
        // Add the menu to the template
21
        $this->template->menu = new View('kodoc/menu', array('active' => $active));
22
    }
23
24
    public function index()
25
    {
26
        $this->template->content = 'hi';
27
    }
28
29
    public function media()
30
    {
31
        if (isset($this->profiler)) {
32
            $this->profiler->disable();
0 ignored issues
show
Bug introduced by
The property profiler does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
33
        }
34
35
        // Get the filename
36
        $file = implode('/', $this->uri->segment_array(1));
37
        $ext = strrchr($file, '.');
38
39
        if ($ext !== false) {
40
            $file = substr($file, 0, -strlen($ext));
41
            $ext = substr($ext, 1);
42
        }
43
44
        // Disable auto-rendering
45
        $this->auto_render = false;
46
47
        try {
48
            // Attempt to display the output
49
            echo new View('kodoc/'.$file, null, $ext);
50
        } catch (Kohana_Exception $e) {
0 ignored issues
show
Unused Code introduced by
catch (\Kohana_Exception...t::run('system.404'); } does not seem to be 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...
51
            Event::run('system.404');
52
        }
53
    }
54
55
    public function __call($method, $args)
56
    {
57
        if (count($segments = $this->uri->segment_array(1)) > 1) {
58
            // Find directory (type) and filename
59
            $type = array_shift($segments);
60
            $file = implode('/', $segments);
61
62
            if (substr($file, -(strlen(EXT))) === EXT) {
63
                // Remove extension
64
                $file = substr($file, 0, -(strlen(EXT)));
65
            }
66
67
            if ($type === 'config') {
68
                if ($file === 'config') {
69
                    // This file can only exist in one location
70
                    $file = APPPATH.$type.'/config'.EXT;
71
                } else {
72
                    foreach (array_reverse(Kohana::include_paths()) as $path) {
73
                        if (is_file($path.$type.'/'.$file.EXT)) {
74
                            // Found the file
75
                            $file = $path.$type.'/'.$file.EXT;
76
                            break;
77
                        }
78
                    }
79
                }
80
            } else {
81
                // Get absolute path to file
82
                $file = Kohana::find_file($type, $file);
83
            }
84
85
            if (in_array($type, Kodoc::get_types())) {
86
                // Load Kodoc
87
                $this->kodoc = new Kodoc($type, $file);
88
89
                // Set the title
90
                $this->template->title = implode('/', $this->uri->segment_array(1));
91
92
                // Load documentation for this file
93
                $this->template->content = new View('kodoc/documentation');
94
95
                // Exit this method
96
                return;
97
            }
98
        }
99
100
        // Nothing to document
101
        url::redirect('kodoc');
102
    }
103
} // End Kodoc Controller
104