Completed
Push — master ( 7eefd3...6db0a4 )
by Mahmoud
03:27
created

ProvidersLoaderTrait   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 64
Duplicated Lines 21.88 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 9
c 0
b 0
f 0
lcom 1
cbo 1
dl 14
loc 64
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A loadOnlyMainProvidersFromContainers() 0 6 1
B loadProviders() 14 23 5
A loadProvider() 0 4 1
A loadServiceProviders() 0 6 2

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\Ship\Engine\Loaders;
4
5
use App;
6
use App\Ship\Engine\Butlers\Facades\ShipButler;
7
use File;
8
9
/**
10
 * Class ProvidersLoaderTrait.
11
 *
12
 * @author  Mahmoud Zalt <[email protected]>
13
 */
14
trait ProvidersLoaderTrait
15
{
16
17
    /**
18
     * Loads only the Main Service Providers from the Containers.
19
     * All the Service Providers (registered inside the main), will be
20
     * loaded from the `boot()` function on the parent of the Main
21
     * Service Providers.
22
     *
23
     * @param $containerName
24
     */
25
    public function loadOnlyMainProvidersFromContainers($containerName)
26
    {
27
        $containerProvidersDirectory = base_path('app/Containers/' . $containerName . '/Providers');
28
29
        $this->loadProviders($containerProvidersDirectory);
30
    }
31
32
    /**
33
     * @param $directory
34
     */
35
    private function loadProviders($directory)
36
    {
37
        $mainServiceProviderNameStartWith = 'Main';
38
39
        if (File::isDirectory($directory)) {
40
41
            $files = File::allFiles($directory);
42
43 View Code Duplication
            foreach ($files as $file) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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::isFile($file)) {
46
47
                    // Check if this is the Main Service Provider
48
                    if (ShipButler::stringStartsWith($file->getFilename(), $mainServiceProviderNameStartWith)) {
49
50
                        $serviceProviderClass = ShipButler::getClassFullNameFromFile($file->getPathname());
51
52
                        $this->loadProvider($serviceProviderClass);
53
                    }
54
                }
55
            }
56
        }
57
    }
58
59
    /**
60
     * @param $providerFullName
61
     */
62
    private function loadProvider($providerFullName)
63
    {
64
        App::register($providerFullName);
65
    }
66
67
    /**
68
     * @void
69
     */
70
    public function loadServiceProviders()
71
    {
72
        foreach ($this->serviceProviders as $provider) {
0 ignored issues
show
Bug introduced by
The property serviceProviders 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...
73
            $this->loadProvider($provider);
74
        }
75
    }
76
77
}
78