Completed
Push — master ( 60840d...857394 )
by Mahmoud
03:05
created

ConsolesLoaderTrait   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 53
Duplicated Lines 35.85 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A runConsolesAutoLoader() 0 5 1
B loadConsolesFromContainers() 18 18 5
A loadConsoles() 0 4 1
A loadConsolesFromPort() 0 7 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\Loader\Loaders;
4
5
use App;
6
use App\Port\Foundation\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
    /**
19
     * runConsolesAutoLoader
20
     */
21
    public function runConsolesAutoLoader()
22
    {
23
        $this->loadConsolesFromPort();
0 ignored issues
show
Unused Code introduced by
The call to the method App\Port\Loader\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...
24
        $this->loadConsolesFromContainers();
25
    }
26
27
    /**
28
     * loadConsolesFromContainers
29
     */
30 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...
31
    {
32
        $consoleClasses = [];
33
        foreach (PortButler::getContainersNames() as $containerName) {
34
            $containerCommandsDirectory = base_path('app/Containers/' . $containerName . '/UI/CLI/Commands/');
35
            if (File::isDirectory($containerCommandsDirectory)) {
36
                $files = File::allFiles($containerCommandsDirectory);
37
                foreach ($files as $consoleFile) {
38
                    if (File::isFile($consoleFile)) {
39
                        $pathName = $consoleFile->getPathname();
40
                        $consoleClasses[] = PortButler::getClassFullNameFromFile($pathName);
41
                    }
42
                }
43
            }
44
        };
45
46
        $this->loadConsoles($consoleClasses);
47
    }
48
49
    /**
50
     * @param array $consoleClasses
51
     */
52
    private function loadConsoles(array $consoleClasses = [])
53
    {
54
        $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...
55
    }
56
57
    /**
58
     * Incomplete
59
     */
60
    private function loadConsolesFromPort()
61
    {
62
        // TODO: implement this function when needed
63
64
        // defined on the MainServiceProvider
65
        $this->portConsolesDirectories;
0 ignored issues
show
Bug introduced by
The property portConsolesDirectories 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...
66
    }
67
}
68