Controller::assign()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 5
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 7
ccs 0
cts 7
cp 0
crap 6
rs 9.4285
1
<?php
2
namespace puck;
3
4
class Controller
5
{
6
7
    protected $viewPath='';
8
    protected $title = '';
9
    private $twig;
10
    private $tVar=array();
11
12
    public function __construct()
13
    {
14
        $loader=new \Twig_Loader_Filesystem(BASE_PATH.$this->viewPath);
15
        $twig=new \Twig_Environment($loader, array(
16
            'debug' => DEBUG,
17
            'cache' => BASE_PATH.'/cache',
18
        ));
19
        $this->twig=$twig;
20
        $this->initTwigFilter();
21
        $this->initTwigFunction();
22
        $this->db=\MysqliDb::getInstance();
0 ignored issues
show
Bug introduced by
The property db 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...
23
    }
24
    private function initTwigFilter() {
25
        $filter=new \Twig_SimpleFilter('long2ip', 'long2ip');
26
        $this->twig->addFilter($filter);
27
    }
28
    private function initTwigFunction() {
29
        $function=new \Twig_SimpleFunction('I', 'I');
30
        $this->twig->addFunction($function);
31
    }
32
33
    protected function show($tmpPath='')
34
    {
35
        if ($tmpPath == '') {
36
            if (defined("CONTROLLER_NAME") && defined("ACTION_NAME")) {
37
                $tmpPath=parse_name(CONTROLLER_NAME).'/'.parse_name(ACTION_NAME);
38
            } else {
39
                show_json($this->tVar);
40
            }
41
        }
42
        header('Content-Type:text/html; charset=utf-8');
43
        header('Cache-control: private'); // 页面缓存控制
44
        header('X-Powered-By:ViviAnAuthSystem');
45
        $this->assign('title', $this->title);
46
        echo $this->twig->render($tmpPath.'.'.TempExt, $this->tVar);
47
        die();
0 ignored issues
show
Coding Style Compatibility introduced by
The method show() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
48
    }
49
50
    /**
51
     * @param string $name
52
     */
53
    protected function assign($name, $value = '') {
54
        if (is_array($name)) {
55
            $this->tVar = array_merge($this->tVar, $name);
56
        } else {
57
            $this->tVar[$name] = $value;
58
        }
59
    }
60
61
    protected function model($modelName, $vars = []) {
62
        return app($modelName . '_model', $vars);
0 ignored issues
show
Unused Code introduced by
The call to app() has too many arguments starting with $vars.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
63
    }
64
}