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 ( 1366ff...fa7cf5 )
by
unknown
13s
created

Controller::setParentPages()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
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 ($file && file_exists($file)) {
171
172
173
            $this->setContext();
174
            /**
175
             * Set context for included file
176
             */
177
            $context = $this->context;
178
179
            include($file);
0 ignored issues
show
Bug introduced by
The variable $file does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
180
        }
181
182
        if (!$this->found) {
183
            $this->found = 'default';
184
            $this->returnDefault();
185
        }
186
    }
187
188
    /**
189
     * Fallback when no controller found or controller file isn't present
190
     */
191
    private function returnDefault()
192
    {
193
        $this->setContext();
194
        $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...
195
        Timber::render(['page-' . strtolower($this->context['post']->post_name) . '.twig', 'page.twig'], $this->context, Cache::getTimerTime());
196
    }
197
198
    /**
199
     * Return name of the body class
200
     *
201
     * @return bool|string
202
     */
203
    public function getClass()
204
    {
205
        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...
206
            return 'page-' . $this->found;
207
        } else {
208
            return false;
209
        }
210
    }
211
}
212