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.

Controller::fourOFour()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Undefined\Stash;
4
5
use Timber\Timber;
6
use Timber\Post as TimberPost;
7
8
final class Controller
9
{
10
    private $parentPages;
11
    private $found;
12
    private $pages;
13
14
    /**
15
     * Controller constructor.
16
     *
17
     * Set context and fetch post id
18
     */
19
    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...
20
    {
21
        $this->found = false;
22
    }
23
24
    /**
25
     * make singleton
26
     *
27
     * @return null|Controller
28
     */
29
    public static function Instance()
30
    {
31
        static $inst = null;
32
        if ($inst === null) {
33
            $inst = new Controller();
34
        }
35
36
        return $inst;
37
    }
38
39
    /**
40
     * Set all pages from within the functions.php->setControllers()
41
     *
42
     * @param $pages
43
     */
44
    public function setPages($pages)
45
    {
46
        $this->pages = $pages;
47
    }
48
49
    /**
50
     * Set all pages from within the functions.php->setControllers()
51
     *
52
     * @param $pages
53
     */
54
    public function setParentPages($pages)
55
    {
56
        $this->parentPages = $pages;
57
    }
58
59
    /**
60
     * Set context for the page
61
     */
62
    private function setContext()
63
    {
64
        $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...
65
        $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...
66
        $this->context['post'] = new TimberPost();
67
    }
68
69
    private function returnId($key)
70
    {
71
        if (function_exists('pll_get_post')) {
72
            return pll_get_post($key);
73
        } else {
74
            return $key;
75
        }
76
    }
77
78
    /**
79
     * Find the correct controller for the single post object
80
     *
81
     * If none controller is found, fallback on teh default post
82
     */
83
    public function single()
84
    {
85
        $this->setContext();
86
87
        $context = $this->context;
88
89
        $file = get_template_directory() . '/controllers/single/' . strtolower($this->context['post']->post_type) . '.php';
90
91 View Code Duplication
        if (file_exists($file)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
92
            /**
93
             * Set context for included file
94
             */
95
            include($file);
96
        } else {
97
            Timber::render(array('single-' . $context['post']->ID . '.twig', 'single-' . $context['post']->post_type . '.twig', 'single.twig'), $context, Cache::getTimerTime());
98
        }
99
    }
100
101
    public function search()
102
    {
103
        $this->setContext();
104
105
        $context = $this->context;
106
107
        Timber::render(array('search.twig', 'archive.twig'), $context, Cache::getTimerTime());
108
    }
109
110
    public function fourOFour()
111
    {
112
        include(get_template_directory() . '/controllers/404.php');
113
    }
114
115
    public function archive()
116
    {
117
        $this->setContext();
118
119
        $context = $this->context;
120
121
        if (is_category()) {
122
            $file = get_template_directory() . '/controllers/archive/category.php';
123
        } else if (is_tax()) {
124
            $file = get_template_directory() . '/controllers/archive/tax.php';
125
        } else {
126
            $file = get_template_directory() . '/controllers/archive/' . strtolower($this->context['post']->title) . '.php';
127
        }
128
129 View Code Duplication
        if (file_exists($file)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
130
            /**
131
             * Set context for included file
132
             */
133
            include($file);
134
        } else {
135
            Timber::render(array('single-' . $context['post']->ID . '.twig', 'single-' . $context['post']->post_type . '.twig', 'single.twig'), $context, Cache::getTimerTime());
136
        }
137
    }
138
139
    public function page()
140
    {
141
        $pageId = get_the_ID();
142
        $parentId = wp_get_post_parent_id($pageId);
143
144
        /**
145
         * Loop though set pages in functions.php->setControllers()
146
         */
147 View Code Duplication
        foreach ($this->parentPages as $key => $page) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
148
            if ($parentId == $this->returnId($key)) {
149
                /**
150
                 * See if controller excists else fall back to default
151
                 */
152
                $file = get_template_directory() . '/controllers/pages/' . $page . '.php';
153
                $this->found = $page;
154
            }
155
        }
156
157
        /**
158
         * Loop though set pages in functions.php->setControllers()
159
         */
160 View Code Duplication
        foreach ($this->pages as $key => $page) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
161
            if ($pageId == $this->returnId($key)) {
162
                /**
163
                 * See if controller excists else fall back to default
164
                 */
165
                $file = get_template_directory() . '/controllers/pages/' . $page . '.php';
166
                $this->found = $page;
167
            }
168
        }
169
170
        if (isset($file) && file_exists($file)) {
171
            $this->setContext();
172
            /**
173
             * Set context for included file
174
             */
175
            $context = $this->context;
176
177
            include($file);
178
        }
179
180
        if (!$this->found) {
181
            $this->found = 'default';
182
            $this->returnDefault();
183
        }
184
    }
185
186
    /**
187
     * Fallback when no controller found or controller file isn't present
188
     */
189
    private function returnDefault()
190
    {
191
        $this->setContext();
192
        $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...
193
        Timber::render(['page-' . strtolower($this->context['post']->post_name) . '.twig', 'page.twig'], $this->context, Cache::getTimerTime());
194
    }
195
196
    /**
197
     * Return name of the body class
198
     *
199
     * @return bool|string
200
     */
201
    public function getClass()
202
    {
203
        if ($this->found) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->found of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
204
            return 'page-' . $this->found;
205
        } else {
206
            return false;
207
        }
208
    }
209
}
210