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 ( 560752...2363e1 )
by Shea
02:46
created

Modules::sortByDesc()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 4
rs 10
ccs 0
cts 2
cp 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
namespace Caffeinated\Modules;
3
4
use Caffeinated\Modules\Contracts\RepositoryInterface;
5
use Illuminate\Foundation\Application;
6
7
class Modules implements RepositoryInterface
8
{
9
	/**
10
	 * @var Application
11
	 */
12
	protected $app;
13
14
	/**
15
	 * @var RepositoryInterface
16
	 */
17
	protected $repository;
18
19
	/**
20
	 * Create a new Modules instance.
21
	 *
22
	 * @param Application  $app
23
	 * @param RepositoryInterface  $repository
24 1
	 */
25
	public function __construct(Application $app, RepositoryInterface $repository)
26 1
	{
27 1
		$this->app        = $app;
28 1
		$this->repository = $repository;
29
	}
30
31
	/**
32
	 * Register the module service provider file from all modules.
33
	 *
34
	 * @return mixed
35
	 */
36
	public function register()
37
	{
38
		$modules = $this->repository->enabled();
39
40
		$modules->each(function($properties, $slug) {
0 ignored issues
show
Unused Code introduced by
The parameter $slug 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...
41
			$this->registerServiceProvider($properties);
42
43
			$this->autoloadFiles($properties);
44
		});
45
	}
46
47
	/**
48
	 * Register the module service provider.
49
	 *
50
	 * @param  string $properties
51
	 * @return string
52
	 * @throws \Caffeinated\Modules\Exception\FileMissingException
53
	 */
54
	protected function registerServiceProvider($properties)
55
	{
56
		$namespace       = $this->resolveNamespace($properties);
0 ignored issues
show
Documentation introduced by
$properties is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
57
		$file            = $this->repository->getPath()."/{$namespace}/Providers/{$namespace}ServiceProvider.php";
0 ignored issues
show
Unused Code introduced by
$file 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...
58
		$serviceProvider = $this->repository->getNamespace()."\\".$namespace."\\Providers\\{$namespace}ServiceProvider";
59
60
		$this->app->register($serviceProvider);
61
	}
62
63
	/**
64
	 * Autoload custom module files.
65
	 *
66
	 * @param array  $properties
67
	 * @return void
68
	 */
69
	protected function autoloadFiles($properties)
70
	{
71
		if (isset($properties['autoload'])) {
72
			$namespace = $this->resolveNamespace($properties);
73
			$path      = $this->repository->getPath()."/{$namespace}/";
74
75
			foreach ($properties['autoload'] as $file) {
76
				include($path.$file);
77
			}
78
		}
79
	}
80
81
	/**
82
	 * Get all modules.
83
	 *
84
	 * @return Collection
85
	 */
86
	public function all()
87
	{
88
		return $this->repository->all();
89
	}
90
91
	/**
92
	 * Get all module slugs.
93
	 *
94
	 * @return array
95
	 */
96
	public function slugs()
97
	{
98
		return $this->repository->slugs();
99
	}
100
101
	/**
102
	 * Get modules based on where clause.
103
	 *
104
	 * @param  string  $key
105
	 * @param  mixed   $value
106
	 * @return Collection
107
	 */
108
	public function where($key, $value)
109
	{
110
		return $this->repository->where($key, $value);
111
	}
112
113
	/**
114
	 * Sort modules by given key in ascending order.
115
	 *
116
	 * @param  string  $key
117
	 * @return Collection
118
	 */
119
	public function sortBy($key)
120
	{
121
		return $this->repository->sortBy($key);
122
	}
123
124
	/**
125
	 * Sort modules by given key in ascending order.
126
	 *
127
	 * @param  string  $key
128
	 * @return Collection
129
	 */
130
	public function sortByDesc($key)
131
	{
132
		return $this->repository->sortByDesc($key);
133
	}
134
135
	/**
136
	 * Check if the given module exists.
137
	 *
138
	 * @param  string  $slug
139
	 * @return bool
140
	 */
141
	public function exists($slug)
142
	{
143
		return $this->repository->exists($slug);
144
	}
145
146
	/**
147
	 * Returns count of all modules.
148
	 *
149
	 * @return int
150
	 */
151
	public function count()
152
	{
153
		return $this->repository->count();
154
	}
155
156
	/**
157
	 * Get modules path.
158
	 *
159
	 * @return string
160
	 */
161
	public function getPath()
162
	{
163
		return $this->repository->getPath();
164
	}
165
166
	/**
167
	 * Set modules path in "RunTime" mode.
168
	 *
169
	 * @param  string $path
170
	 * @return object $this
171
	 */
172
	public function setPath($path)
173
	{
174
		return $this->repository->setPath($path);
175
	}
176
177
	/**
178
	* Get path for the specified module.
179
	*
180
	* @param  string $slug
181
	* @return string
182
	*/
183
	public function getModulePath($slug)
184
	{
185
		return $this->repository->getModulePath($slug);
186
	}
187
188
	/**
189
	* Get modules namespace.
190
	*
191
	* @return string
192
	*/
193
	public function getNamespace()
194
	{
195
		return $this->repository->getNamespace();
196
	}
197
198
	/**
199
	 * Get a module's properties.
200
	 *
201
	 * @param  string $slug
202
	 * @return mixed
203
	 */
204
	public function getProperties($slug)
205
	{
206
		return $this->repository->getProperties($slug);
207
	}
208
209
	/**
210
	 * Get a module property value.
211
	 *
212
	 * @param  string $property
213
	 * @param  mixed  $default
214
	 * @return mixed
215
	 */
216
	public function getProperty($property, $default = null)
217
	{
218
		return $this->repository->getProperty($property, $default);
219
	}
220
221
	/**
222
	 * Set a module property value.
223
	 *
224
	 * @param  string $property
225
	 * @param  mixed  $value
226
	 * @return bool
227
	 */
228
	public function setProperty($property, $value)
229
	{
230
		return $this->repository->setProperty($property, $value);
231
	}
232
233
	/**
234
	 * Gets all enabled modules.
235
	 *
236
	 * @return array
237
	 */
238
	public function enabled()
239
	{
240
		return $this->repository->enabled();
241
	}
242
243
	/**
244
	 * Gets all disabled modules.
245
	 *
246
	 * @return array
247
	 */
248
	public function disabled()
249
	{
250
		return $this->repository->disabled();
251
	}
252
253
	/**
254
	 * Check if specified module is enabled.
255
	 *
256
	 * @param  string $slug
257
	 * @return bool
258
	 */
259
	public function isEnabled($slug)
260
	{
261
		return $this->repository->isEnabled($slug);
262
	}
263
264
	/**
265
	 * Check if specified module is disabled.
266
	 *
267
	 * @param  string $slug
268
	 * @return bool
269
	 */
270
	public function isDisabled($slug)
271
	{
272
		return $this->repository->isDisabled($slug);
273
	}
274
275
	/**
276
	 * Enables the specified module.
277
	 *
278
	 * @param  string $slug
279
	 * @return bool
280
	 */
281
	public function enable($slug)
282
	{
283
		return $this->repository->enable($slug);
284
	}
285
286
	/**
287
	 * Disables the specified module.
288
	 *
289
	 * @param  string $slug
290
	 * @return bool
291
	 */
292
	public function disable($slug)
293
	{
294
		return $this->repository->disable($slug);
295
	}
296
297
    public function cache()
298
    {
299
        return $this->repository->cache();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Caffeinated\Modules\Contracts\RepositoryInterface as the method cache() does only exist in the following implementations of said interface: Caffeinated\Modules\Modules, Caffeinated\Modules\Repositories\LocalRepository.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
300
    }
301
302
	/**
303
	 * Resolve the correct module namespace.
304
	 *
305
	 * @param array  $properties
306
	 */
307
	private function resolveNamespace($properties)
308
	{
309
		return (isset($properties['namespace'])
310
			? $properties['namespace']
311
			: studly_case($properties['slug'])
312
		);
313
	}
314
}
315