GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 078d1e...95441f )
by
unknown
10s
created

Controller::page()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 31
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 5
eloc 12
nc 8
nop 0
dl 0
loc 31
rs 8.439
c 1
b 0
f 1
1
<?php
2
namespace Undefined\Stash;
3
4
use Timber\Timber;
5
use Timber\Post as TimberPost;
6
7
final class Controller
8
{
9
    /**
10
     * Controller constructor.
11
     *
12
     * Set context and fetch post id
13
     */
14
    function __construct()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
15
    {
16
        $this->found = false;
0 ignored issues
show
Bug introduced by
The property found 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...
17
    }
18
19
    /**
20
     * make singleton
21
     *
22
     * @return null|Controller
23
     */
24
    public static function Instance()
25
    {
26
        static $inst = null;
27
        if ($inst === null) {
28
            $inst = new Controller();
29
        }
30
31
        return $inst;
32
    }
33
34
    /**
35
     * Set all pages from within the functions.php->setControllers()
36
     *
37
     * @param $pages
38
     */
39
    public function setPages($pages)
40
    {
41
        $this->pages = $pages;
0 ignored issues
show
Bug introduced by
The property pages 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...
42
    }
43
44
    /**
45
     * Set context for the page
46
     */
47
    private function setContext()
48
    {
49
        $this->postId = get_the_ID();
0 ignored issues
show
Bug introduced by
The property postId 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...
50
        $this->context = Timber::get_context();
0 ignored issues
show
Bug introduced by
The property context 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...
51
        $this->context['post'] = new TimberPost();
52
    }
53
54
    private function returnId($key)
55
    {
56
        if (function_exists('pll_get_post')) {
57
            return pll_get_post($key);
58
        } else {
59
            return $key;
60
        }
61
    }
62
63
    public function page()
64
    {
65
        /**
66
         * Loop though set pages in functions.php->setControllers()
67
         */
68
        foreach ($this->pages as $key => $page) {
69
70
            if (get_the_ID() == $this->returnId($key)) {
71
                /**
72
                 * See if controller excists else fall back to default
73
                 */
74
                $file = get_template_directory() . '/controllers/' . $page . '.php';
75
                if (file_exists($file)) {
76
                    $this->found = $page;
77
78
                    $this->setContext();
79
                    /**
80
                     * Set context for included file
81
                     */
82
                    $context = $this->context;
83
84
                    include($file);
85
                }
86
            }
87
        }
88
89
        if (!$this->found) {
90
            $this->found = 'default';
91
            $this->returnDefault();
92
        }
93
    }
94
95
    /**
96
     * Fallback when no controller found or controller file isn't present
97
     */
98
    private function returnDefault()
99
    {
100
        $this->setContext();
101
        $context = $this->context;
0 ignored issues
show
Unused Code introduced by
$context is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
102
        Timber::render(['page-' . $this->context['post']->post_name . '.twig', 'page.twig'], $this->context, Cache::getTimerTime());
103
    }
104
105
    /**
106
     * Return name of the body class
107
     *
108
     * @return bool|string
109
     */
110
    public function getClass()
111
    {
112
        if ($this->found) {
113
            return 'page-' . $this->found;
114
        } else {
115
            return false;
116
        }
117
    }
118
}