Completed
Pull Request — master (#1151)
by
unknown
11:07 queued 01:06
created

LaravelEloquentRepository::getOrdered()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
rs 9.9332
1
<?php
2
3
namespace Nwidart\Modules\Laravel;
4
5
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
6
use Nwidart\Modules\Collection;
7
use Illuminate\Container\Container;
8
use Nwidart\Modules\Contracts\RepositoryInterface;
9
use Nwidart\Modules\Entities\ModuleEntity;
10
use Nwidart\Modules\Exceptions\ModuleNotFoundException;
11
use Nwidart\Modules\Module;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Nwidart\Modules\Laravel\Module.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
12
13
class LaravelEloquentRepository implements RepositoryInterface
14
{
15
    /**
16
     * @var ModuleEntity
17
     */
18
    private $moduleEntity;
19
20
    /**
21
     * @var Container
22
     */
23
    private $app;
24
25
    public function __construct(Container $app, ModuleEntity $moduleEntity)
26
    {
27
        $this->app = $app;
28
        $this->moduleEntity = $moduleEntity;
29
    }
30
31
    /**
32
     * Get all modules.
33
     * @return \Illuminate\Database\Eloquent\Collection
34
     */
35
    public function all(): array
36
    {
37
        return $this->moduleEntity->get()->toArray();
38
    }
39
40
    /**
41
     * Get cached modules.
42
     * @return array
43
     */
44
    public function getCached(): array
45
    {
46
        return $this->app['cache']->remember($this->config('cache.key'), $this->config('cache.lifetime'), function () {
47
            return $this->toCollection()->toArray();
48
        });
49
    }
50
51
    /**
52
     * Scan & get all available modules.
53
     * @return array
54
     */
55
    public function scan(): array
56
    {
57
        return $this->toCollection()->toArray();
58
    }
59
60
    /**
61
     * Get modules as modules collection instance.
62
     * @return EloquentCollection
63
     */
64
    public function toCollection(): Collection
65
    {
66
        return $this->convertToCollection($this->all());
0 ignored issues
show
Documentation introduced by
$this->all() is of type array, but the function expects a object<Illuminate\Database\Eloquent\Collection>.

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...
67
    }
68
69
    protected function createModule(...$args)
70
    {
71
        return new Module(...$args);
0 ignored issues
show
Bug introduced by
The call to Module::__construct() misses some required arguments starting with $name.
Loading history...
Documentation introduced by
$args is of type array<integer,?>, but the function expects a object<Illuminate\Container\Container>.

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...
72
    }
73
74
    /**
75
     * Get scanned paths.
76
     * @return array
77
     */
78
    public function getScanPaths(): array
79
    {
80
        return [];
81
    }
82
83
    /**
84
     * Get list of enabled modules.
85
     * @return array
86
     */
87
    public function allEnabled(): array
88
    {
89
        return $this->convertToCollection($results)->toArray();
0 ignored issues
show
Bug introduced by
The variable $results does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
90
    }
91
92
    /**
93
     * Get list of disabled modules.
94
     * @return mixed
95
     */
96
    public function allDisabled()
97
    {
98
        $results = $this->moduleEntity->newQuery()->where('is_active', 0)->get();
99
100
        return $this->convertToCollection($results)->toArray();
0 ignored issues
show
Bug introduced by
It seems like $results defined by $this->moduleEntity->new...('is_active', 0)->get() on line 98 can also be of type array<integer,object<Ill...base\Eloquent\Builder>>; however, Nwidart\Modules\Laravel\...::convertToCollection() does only seem to accept object<Illuminate\Database\Eloquent\Collection>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
101
    }
102
103
    /**
104
     * Get count from all modules.
105
     * @return int
106
     */
107
    public function count(): int
108
    {
109
        return $this->moduleEntity->count();
110
    }
111
112
    /**
113
     * Get all ordered modules.
114
     * @param string $direction
115
     * @return mixed
116
     */
117
    public function getOrdered(string $direction = 'asc'): array
118
    {
119
        $results = $this->moduleEntity
0 ignored issues
show
Bug introduced by
The method orderBy() does not exist on Illuminate\Database\Eloquent\Builder. Did you maybe mean enforceOrderBy()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
120
            ->newQuery()
121
            ->where('is_active', 1)
122
            ->orderBy('order', $direction)
123
            ->get();
124
125
        return $this->convertToCollection($results)->toArray();
126
    }
127
128
    /**
129
     * Get modules by the given status.
130
     * @param int $status
131
     * @return array
132
     */
133
    public function getByStatus($status): array
134
    {
135
        $results = $this->moduleEntity
136
            ->newQuery()
137
            ->where('is_active', $status)
138
            ->get();
139
        return $this->convertToCollection($results)->toArray();
0 ignored issues
show
Bug introduced by
It seems like $results defined by $this->moduleEntity->new...ctive', $status)->get() on line 135 can also be of type array<integer,object<Ill...base\Eloquent\Builder>>; however, Nwidart\Modules\Laravel\...::convertToCollection() does only seem to accept object<Illuminate\Database\Eloquent\Collection>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
140
    }
141
142
    /**
143
     * Find a specific module.
144
     * @param $name
145
     * @return \Nwidart\Modules\Module
146
     */
147
    public function find($name): ?\Nwidart\Modules\Module
148
    {
149
        $module = $this->moduleEntity
150
            ->newQuery()
151
            ->where('name', $name)
152
            ->first();
153
154
        if ($module === null) {
155
            return null;
156
        }
157
158
        return $this->createModule($this->app, $module->name, $module->path);
159
    }
160
161
    /**
162
     * Find a specific module. If there return that, otherwise throw exception.
163
     * @param $name
164
     * @return \Nwidart\Modules\Module
165
     * @throws ModuleNotFoundException
166
     */
167
    public function findOrFail($name): \Nwidart\Modules\Module
168
    {
169
        $module = $this->find($name);
170
171
        if ($module === null) {
172
            throw new ModuleNotFoundException();
173
        }
174
175
        return $module;
176
    }
177
178
    public function getModulePath($moduleName)
179
    {
180
        $module = $this->findOrFail($moduleName);
181
182
        return $module->getPath();
183
    }
184
185
    /**
186
     * @return \Illuminate\Filesystem\Filesystem
187
     */
188
    public function getFiles()
189
    {
190
        return $this->app['files'];
191
    }
192
193
    public function config($key, $default = null)
194
    {
195
        return $this->app['config']->get('modules.' . $key, $default);
196
    }
197
198
    private function convertToCollection(EloquentCollection $eloquentCollection): Collection
199
    {
200
        $collection = new Collection();
201
        $eloquentCollection->map(function ($module) use ($collection) {
202
            $collection->push($this->createModule($this->app, $module->name, $module->path));
203
        });
204
        return $collection;
205
    }
206
207
    public function findRequirements($name): array
208
    {
209
        // TODO: Implement findRequirements() method.
210
    }
211
212
    public function getPath(): string
213
    {
214
        // TODO: Implement getPath() method.
215
    }
216
217
    public function findByAlias(string $alias)
218
    {
219
        // TODO: Implement findByAlias() method.
220
    }
221
222
    public function boot(): void
223
    {
224
        // TODO: Implement boot() method.
225
    }
226
227
    public function register(): void
228
    {
229
        // TODO: Implement register() method.
230
    }
231
232
    public function assetPath(string $module): string
233
    {
234
        // TODO: Implement assetPath() method.
235
    }
236
237
    public function delete(string $module): bool
238
    {
239
        // TODO: Implement delete() method.
240
    }
241
242
    public function isEnabled(string $name): bool
243
    {
244
        // TODO: Implement isEnabled() method.
245
    }
246
247
    public function isDisabled(string $name): bool
248
    {
249
        // TODO: Implement isDisabled() method.
250
    }
251
}
252