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

MigrationsLoaderTrait   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 53
Duplicated Lines 24.53 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A runMigrationsAutoLoader() 0 5 1
A loadMigrationsFromContainers() 13 13 3
A loadMigrationsFromPort() 0 7 2
A loadMigrations() 0 8 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\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 MigrationsLoaderTrait.
12
 *
13
 * @author  Mahmoud Zalt <[email protected]>
14
 */
15
trait MigrationsLoaderTrait
16
{
17
18
    /**
19
     * runMigrationsAutoLoader
20
     */
21
    public function runMigrationsAutoLoader()
22
    {
23
        $this->loadMigrationsFromContainers();
24
        $this->loadMigrationsFromPort();
25
    }
26
27
    /**
28
     * loadMigrationsFromContainers
29
     */
30 View Code Duplication
    private function loadMigrationsFromContainers()
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
        foreach (PortButler::getContainersNames() as $containerName) {
33
34
            $containerMigrationDirectory = base_path('app/Containers/' . $containerName . '/Data/Migrations');
35
36
            if (File::isDirectory($containerMigrationDirectory)) {
37
38
                $this->loadMigrations($containerMigrationDirectory);
39
40
            }
41
        }
42
    }
43
44
    /**
45
     * loadMigrationsFromPort
46
     */
47
    private function loadMigrationsFromPort()
48
    {
49
        // `$this->portMigrationsDirectories` is defined in the Main Service Provider
50
        foreach ($this->portMigrationsDirectories as $portMigrationsDirectory) {
0 ignored issues
show
Bug introduced by
The property portMigrationsDirectories 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...
51
            $this->loadMigrations($portMigrationsDirectory);
52
        }
53
    }
54
55
    /**
56
     * @param $directory
57
     */
58
    private function loadMigrations($directory)
59
    {
60
        App::afterResolving('migrator', function ($migrator) use ($directory) {
61
            foreach ((array)$directory as $path) {
62
                $migrator->path($path);
63
            }
64
        });
65
    }
66
67
}
68