ModuleVerification::verifyActive()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 9
Ratio 100 %

Importance

Changes 0
Metric Value
dl 9
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
namespace Mnabialek\LaravelModular\Console\Traits;
4
5
use Exception;
6
use Illuminate\Support\Collection;
7
use Mnabialek\LaravelModular\Models\Module;
8
9
trait ModuleVerification
10
{
11
    /**
12
     * Verify whether given modules exist and are active
13
     *
14
     * @param Collection $moduleNames
15
     *
16
     * @return Collection
17
     * @throws Exception
18
     */
19 View Code Duplication
    protected function verifyActive(Collection $moduleNames)
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...
20
    {
21
        $modules = $this->verifyModules($moduleNames, true);
22
        if ($modules->count() != $moduleNames->count()) {
23
            throw new Exception('There were errors. You need to pass only valid active module names');
24
        }
25
26
        return $modules;
27
    }
28
29
    /**
30
     * Verify whether given modules exist
31
     *
32
     * @param Collection $moduleNames
33
     *
34
     * @return Collection
35
     * @throws Exception
36
     */
37 View Code Duplication
    protected function verifyExisting(Collection $moduleNames)
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...
38
    {
39
        $modules = $this->verifyModules($moduleNames, false);
40
        if ($modules->count() != $moduleNames->count()) {
41
            throw new Exception('There were errors. You need to pass only valid module names');
42
        }
43
44
        return $modules;
45
    }
46
47
    /**
48
     * Verifies whether given modules exist and whether they are active
49
     *
50
     * @param Collection $moduleNames
51
     * @param bool $verifyActive
52
     *
53
     * @return Collection|bool
54
     */
55
    private function verifyModules(Collection $moduleNames, $verifyActive)
56
    {
57
        $modules = collect();
58
59
        $moduleNames->each(function ($name) use ($verifyActive, $modules) {
60
            /** @var Module $module */
61
            $module = $this->laravel['modular']->find($name);
0 ignored issues
show
Bug introduced by
The property laravel 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...
62
63
            if (!$module) {
64
                $this->error("Module {$name} does not exist");
0 ignored issues
show
Bug introduced by
It seems like error() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
65
66
                return;
67
            }
68
69
            if ($verifyActive && !$module->active()) {
70
                $this->error("Module {$name} is not active");
0 ignored issues
show
Bug introduced by
It seems like error() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
71
72
                return;
73
            }
74
75
            $modules->push($module);
76
        });
77
78
        return $modules;
79
    }
80
}
81