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
Pull Request — master (#82)
by Vincent
02:34
created

Controller::archive()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 8

Duplication

Lines 17
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 0
dl 17
loc 17
rs 9.4285
c 0
b 0
f 0
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
    /**
64
     * Find the correct controller for the single post object
65
     *
66
     * If none controller is found, fallback on teh default post
67
     */
68 View Code Duplication
    public function single()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
69
    {
70
        $this->setContext();
71
72
        $context = $this->context;
73
74
        $file = get_template_directory() . '/controllers/single/' . $this->context['post']->post_type . '.php';
75
76
        if (file_exists($file)) {
77
            /**
78
             * Set context for included file
79
             */
80
            include($file);
81
        } else {
82
            Timber::render(array('single-' . $context['post']->ID . '.twig', 'single-' . $context['post']->post_type . '.twig', 'single.twig'), $context, Cache::getTimerTime());
83
        }
84
    }
85
86
    public function fourOFour()
87
    {
88
        include(get_template_directory() . '/controllers/404.php');
89
    }
90
91 View Code Duplication
    public function archive()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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
        $this->setContext();
94
95
        $context = $this->context;
96
97
        $file = get_template_directory() . '/controllers/archive/' . $this->context['post']->post_type . '.php';
98
99
        if (file_exists($file)) {
100
            /**
101
             * Set context for included file
102
             */
103
            include($file);
104
        } else {
105
            Timber::render(array('single-' . $context['post']->ID . '.twig', 'single-' . $context['post']->post_type . '.twig', 'single.twig'), $context, Cache::getTimerTime());
106
        }
107
    }
108
109
    public function page()
110
    {
111
        /**
112
         * Loop though set pages in functions.php->setControllers()
113
         */
114
        foreach ($this->pages as $key => $page) {
115
            if (get_the_ID() == $this->returnId($key)) {
116
                /**
117
                 * See if controller excists else fall back to default
118
                 */
119
                $file = get_template_directory() . '/controllers/pages/' . $page . '.php';
120
                if (file_exists($file)) {
121
                    $this->found = $page;
122
123
                    $this->setContext();
124
                    /**
125
                     * Set context for included file
126
                     */
127
                    $context = $this->context;
128
129
                    include($file);
130
                }
131
            }
132
        }
133
134
        if (!$this->found) {
135
            $this->found = 'default';
136
            $this->returnDefault();
137
        }
138
    }
139
140
    /**
141
     * Fallback when no controller found or controller file isn't present
142
     */
143
    private function returnDefault()
144
    {
145
        $this->setContext();
146
        $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...
147
        Timber::render(['page-' . $this->context['post']->post_name . '.twig', 'page.twig'], $this->context, Cache::getTimerTime());
148
    }
149
150
    /**
151
     * Return name of the body class
152
     *
153
     * @return bool|string
154
     */
155
    public function getClass()
156
    {
157
        if ($this->found) {
158
            return 'page-' . $this->found;
159
        } else {
160
            return false;
161
        }
162
    }
163
}