Completed
Push — master ( bec27e...637f9e )
by Mahmoud
03:05
created

ConsolesLoaderTrait   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 48
Duplicated Lines 35.42 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
c 0
b 0
f 0
lcom 0
cbo 1
dl 17
loc 48
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A loadConsolesFromContainers() 0 6 1
A loadConsolesFromPort() 0 8 1
A loadTheConsoles() 17 17 3

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\Loader\Helpers\Facade\LoaderHelper;
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
     * @param $containerName
20
     */
21
    public function loadConsolesFromContainers($containerName)
22
    {
23
        $containerCommandsDirectory = base_path('app/Containers/' . $containerName . '/UI/CLI/Commands/');
24
25
        $this->loadTheConsoles($containerCommandsDirectory);
26
    }
27
28
    /**
29
     * @param $portFolderName
30
     */
31
    public function loadConsolesFromPort($portFolderName)
32
    {
33
        // TODO: Never Tested
34
35
        $portFolderName = base_path('app/Port/') . $portFolderName . '/Commands/';
36
37
        $this->loadTheConsoles($portFolderName);
38
    }
39
40
    /**
41
     * @param $consoleClass
42
     */
43 View Code Duplication
    private function loadTheConsoles($directory)
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...
44
    {
45
        if (File::isDirectory($directory)) {
46
47
            $files = File::allFiles($directory);
48
49
            foreach ($files as $consoleFile) {
50
51
                $consoleClass = LoaderHelper::getClassFullNameFromFile($consoleFile->getPathname());
52
53
                // when user from the Main Service Provider, which extends Laravel
54
                // service provider you get access to `$this->commands`
55
                $this->commands([$consoleClass]);
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
    }
60
61
62
}
63