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.

LocalRepository::where()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
namespace Caffeinated\Modules\Repositories;
4
5
class LocalRepository extends Repository
6
{
7
    /**
8
     * Update cached repository of module information.
9
     *
10
     * @return bool
11
     */
12
    public function optimize()
13
    {
14
        $cachePath = $this->getCachePath();
15
        $cache     = $this->getCache();
16
        $basenames = $this->getAllBasenames();
17
        $modules   = collect();
18
19
        $basenames->each(function ($module, $key) use ($modules, $cache) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
20
            $temp = collect($cache->get($module));
21
            $manifest = collect($this->getManifest($module));
22
23
            $modules->put($module, $temp->merge($manifest));
24
        });
25
26
        $modules->each(function ($module) {
27
            if (!$module->has('enabled')) {
28
                $module->put('enabled', config('modules.enabled', true));
29
            }
30
31
            if (!$module->has('order')) {
32
                $module->put('order', 9001);
33
            }
34
35
            return $module;
36
        });
37
38
        $content = json_encode($modules->all(), JSON_PRETTY_PRINT);
39
40
        return $this->files->put($cachePath, $content);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->files->put($cachePath, $content); (integer) is incompatible with the return type declared by the interface Caffeinated\Modules\Cont...toryInterface::optimize of type boolean.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
41
    }
42
43
    /**
44
     * Get all modules.
45
     *
46
     * @return Collection
47
     */
48
    public function all()
49
    {
50
        return $this->getCache()->sortBy('order');
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->getCache()->sortBy('order'); (Illuminate\Support\Collection) is incompatible with the return type declared by the interface Caffeinated\Modules\Cont...epositoryInterface::all of type Caffeinated\Modules\Contracts\Collection.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
51
    }
52
53
    /**
54
     * Get all module slugs.
55
     *
56
     * @return Collection
57
     */
58
    public function slugs()
59
    {
60
        $slugs = collect();
61
62
        $this->all()->each(function ($item, $key) use ($slugs) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
63
            $slugs->push($item['slug']);
64
        });
65
66
        return $slugs;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $slugs; (Illuminate\Support\Collection) is incompatible with the return type declared by the interface Caffeinated\Modules\Cont...ositoryInterface::slugs of type Caffeinated\Modules\Contracts\Collection.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
67
    }
68
69
    /**
70
     * Get modules based on where clause.
71
     *
72
     * @param string $key
73
     * @param mixed  $value
74
     *
75
     * @return Collection
76
     */
77
    public function where($key, $value)
78
    {
79
        return collect($this->all()->where($key, $value)->first());
0 ignored issues
show
Bug Best Practice introduced by
The return type of return collect($this->al...key, $value)->first()); (Illuminate\Support\Collection) is incompatible with the return type declared by the interface Caffeinated\Modules\Cont...ositoryInterface::where of type Caffeinated\Modules\Contracts\Collection.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
80
    }
81
82
    /**
83
     * Sort modules by given key in ascending order.
84
     *
85
     * @param string $key
86
     *
87
     * @return Collection
88
     */
89
    public function sortBy($key)
90
    {
91
        $collection = $this->all();
92
93
        return $collection->sortBy($key);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $collection->sortBy($key); (Illuminate\Support\Collection) is incompatible with the return type declared by the interface Caffeinated\Modules\Cont...sitoryInterface::sortBy of type Caffeinated\Modules\Contracts\Collection.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
94
    }
95
96
    /**
97
     * Sort modules by given key in ascending order.
98
     *
99
     * @param string $key
100
     *
101
     * @return Collection
102
     */
103
    public function sortByDesc($key)
104
    {
105
        $collection = $this->all();
106
107
        return $collection->sortByDesc($key);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $collection->sortByDesc($key); (Illuminate\Support\Collection) is incompatible with the return type declared by the interface Caffeinated\Modules\Cont...ryInterface::sortByDesc of type Caffeinated\Modules\Contracts\Collection.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
108
    }
109
110
    /**
111
     * Determines if the given module exists.
112
     *
113
     * @param string $slug
114
     *
115
     * @return bool
116
     */
117
    public function exists($slug)
118
    {
119
        return $this->slugs()->contains(str_slug($slug));
120
    }
121
122
    /**
123
     * Returns count of all modules.
124
     *
125
     * @return int
126
     */
127
    public function count()
128
    {
129
        return $this->all()->count();
130
    }
131
132
    /**
133
     * Get a module property value.
134
     *
135
     * @param string $property
136
     * @param mixed  $default
137
     *
138
     * @return mixed
139
     */
140
    public function get($property, $default = null)
141
    {
142
        list($slug, $key) = explode('::', $property);
143
144
        $module = $this->where('slug', $slug);
145
146
        return $module->get($key, $default);
147
    }
148
149
    /**
150
     * Set the given module property value.
151
     *
152
     * @param string $property
153
     * @param mixed  $value
154
     *
155
     * @return bool
156
     */
157
    public function set($property, $value)
158
    {
159
        list($slug, $key) = explode('::', $property);
160
161
        $cachePath = $this->getCachePath();
162
        $cache     = $this->getCache();
163
        $module    = $this->where('slug', $slug);
164
        $moduleKey = $module->keys()->first();
165
        $values    = $module->first();
166
167
        if (isset($values[$key])) {
168
            unset($values[$key]);
169
        }
170
171
        $values[$key] = $value;
172
173
        $module = collect([$moduleKey => $values]);
174
175
        $merged  = $cache->merge($module);
176
        $content = json_encode($merged->all(), JSON_PRETTY_PRINT);
177
178
        return $this->files->put($cachePath, $content);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->files->put($cachePath, $content); (integer) is incompatible with the return type declared by the interface Caffeinated\Modules\Cont...epositoryInterface::set of type boolean.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
179
    }
180
181
    /**
182
     * Get all enabled modules.
183
     *
184
     * @return Collection
185
     */
186
    public function enabled()
187
    {
188
        return $this->all()->where('enabled', true);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->all()->where('enabled', true); (Illuminate\Support\Collection) is incompatible with the return type declared by the interface Caffeinated\Modules\Cont...itoryInterface::enabled of type Caffeinated\Modules\Contracts\Collection.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
189
    }
190
191
    /**
192
     * Get all disabled modules.
193
     *
194
     * @return Collection
195
     */
196
    public function disabled()
197
    {
198
        return $this->all()->where('enabled', false);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->all()->where('enabled', false); (Illuminate\Support\Collection) is incompatible with the return type declared by the interface Caffeinated\Modules\Cont...toryInterface::disabled of type Caffeinated\Modules\Contracts\Collection.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
199
    }
200
201
    /**
202
     * Check if specified module is enabled.
203
     *
204
     * @param string $slug
205
     *
206
     * @return bool
207
     */
208
    public function isEnabled($slug)
209
    {
210
        $module = $this->where('slug', $slug)
211
            ->first();
212
213
        return $module['enabled'] === true;
214
    }
215
216
    /**
217
     * Check if specified module is disabled.
218
     *
219
     * @param string $slug
220
     *
221
     * @return bool
222
     */
223
    public function isDisabled($slug)
224
    {
225
        $module = $this->where('slug', $slug)
226
            ->first();
227
228
        return $module['enabled'] === false;
229
    }
230
231
    /**
232
     * Enables the specified module.
233
     *
234
     * @param string $slug
235
     *
236
     * @return bool
237
     */
238
    public function enable($slug)
239
    {
240
        return $this->set($slug.'::enabled', true);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->set($slug . '::enabled', true); (integer) is incompatible with the return type declared by the interface Caffeinated\Modules\Cont...sitoryInterface::enable of type boolean.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
241
    }
242
243
    /**
244
     * Disables the specified module.
245
     *
246
     * @param string $slug
247
     *
248
     * @return bool
249
     */
250
    public function disable($slug)
251
    {
252
        return $this->set($slug.'::enabled', false);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->set($slug . '::enabled', false); (integer) is incompatible with the return type declared by the interface Caffeinated\Modules\Cont...itoryInterface::disable of type boolean.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
253
    }
254
255
    /**
256
     * Get the contents of the cache file.
257
     *
258
     * The cache file lists all module slugs and their
259
     * enabled or disabled status. This can be used to
260
     * filter out modules depending on their status.
261
     *
262
     * @return Collection
263
     */
264
    public function getCache()
265
    {
266
        $cachePath = $this->getCachePath();
267
268
        if (!$this->files->exists($cachePath)) {
269
            $content = json_encode(array(), JSON_PRETTY_PRINT);
270
271
            $this->files->put($cachePath, $content);
272
273
            $this->optimize();
274
275
            return collect(json_decode($content, true));
276
        }
277
278
        return collect(json_decode($this->files->get($cachePath), true));
279
    }
280
281
    /**
282
     * Get the path to the cache file.
283
     *
284
     * @return string
285
     */
286
    protected function getCachePath()
287
    {
288
        return storage_path('app/modules.json');
289
    }
290
}
291