Completed
Push — master ( 3aab7f...270485 )
by Mahmoud
03:05
created

ConsolesLoaderTrait   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 44
Duplicated Lines 43.18 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 1
dl 19
loc 44
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A runConsolesAutoLoader() 0 5 1
B loadConsolesFromContainers() 19 19 5
A loadConsolesFromPort() 0 4 1
A loadConsoles() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace App\Port\Console\Loaders;
4
5
use App;
6
use App\Port\Butler\Portals\Facade\PortButler;
7
use DB;
8
use File;
9
10
/**
11
 * Class ConsolesLoaderTrait.
12
 *
13
 * @author  Mahmoud Zalt <[email protected]>
14
 */
15
trait ConsolesLoaderTrait
16
{
17
18
    protected $portConsolesDirectories = [
19
20
    ];
21
22
    public function runConsolesAutoLoader()
23
    {
24
        $this->loadConsolesFromPort();
0 ignored issues
show
Unused Code introduced by
The call to the method App\Port\Console\Loaders...:loadConsolesFromPort() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
25
        $this->loadConsolesFromContainers();
26
    }
27
28 View Code Duplication
    private function loadConsolesFromContainers()
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...
29
    {
30
        $consoleClasses = [];
31
        foreach (PortButler::getContainersNames() as $containerName) {
32
            $containerCommandsDirectory = base_path('app/Containers/' . $containerName . '/UI/CLI/Commands/');
33
            if (File::isDirectory($containerCommandsDirectory)) {
34
                $files = File::allFiles($containerCommandsDirectory);
35
                foreach ($files as $consoleFile) {
36
                    if (File::isFile($consoleFile)) {
37
                        $pathName = $consoleFile->getPathname();
38
                        $consoleClasses[] = PortButler::getClassFullNameFromFile($pathName);
39
                    }
40
                }
41
            }
42
        };
43
44
        $this->loadConsoles($consoleClasses);
45
46
    }
47
48
    private function loadConsolesFromPort()
49
    {
50
51
    }
52
53
    private function loadConsoles(array $consoleClasses = [])
54
    {
55
        $this->commands($consoleClasses);
0 ignored issues
show
Bug introduced by
It seems like commands() 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...
56
    }
57
58
}
59