Completed
Push — master ( 835594...7f1bc5 )
by Abdelrahman
02:24 queued 01:21
created

ConsoleTools::autoloadMigrations()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Rinvex\Support\Traits;
6
7
trait ConsoleTools
8
{
9
    /**
10
     * Publish package migrations.
11
     *
12
     * @return void
13
     */
14
    protected function publishesMigrations(string $package, bool $isModule = false): void
15
    {
16
        if ($this->publishesResources()) {
17
            return;
18
        }
19
20
        $namespace = str_replace('laravel-', '', $package);
21
        $namespace = str_replace(['/', '\\', '.', '_'], '-', $namespace);
22
        $basePath = $isModule ? $this->app->path($package)
0 ignored issues
show
Bug introduced by
The property app 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...
23
            : $this->app->basePath('vendor/'.$package);
24
25
        if (file_exists($path = $basePath.'/database/migrations')) {
26
            $stubs = $this->app['files']->glob($path.'/*.php');
27
            $existing = $this->app['files']->glob($this->app->databasePath('migrations/'.$package.'/*.php'));
28
29
            $migrations = collect($stubs)->flatMap(function ($migration) use ($existing, $package) {
30
                $sequence = mb_substr(basename($migration), 0, 17);
31
                $match = collect($existing)->first(function ($item, $key) use ($migration, $sequence) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
32
                    return mb_strpos($item, str_replace($sequence, '', basename($migration))) !== false;
33
                });
34
35
                return [$migration => $this->app->databasePath('migrations/'.$package.'/'.($match ? basename($match) : date('Y_m_d_His', time() + mb_substr($sequence, -6)).str_replace($sequence, '', basename($migration))))];
36
            })->toArray();
37
38
            $this->publishes($migrations, $namespace.'-migrations');
0 ignored issues
show
Bug introduced by
The method publishes() does not exist on Rinvex\Support\Traits\ConsoleTools. Did you maybe mean publishesMigrations()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
39
        }
40
    }
41
42
    /**
43
     * Publish package config.
44
     *
45
     * @return void
46
     */
47
    protected function publishesConfig(string $package, bool $isModule = false): void
48
    {
49
        if ($this->publishesResources()) {
50
            return;
51
        }
52
53
        $namespace = str_replace('laravel-', '', $package);
54
        $namespace = str_replace(['/', '\\', '.', '_'], '-', $namespace);
55
        $basePath = $isModule ? $this->app->path($package)
56
            : $this->app->basePath('vendor/'.$package);
57
58
        if (file_exists($path = $basePath.'/config/config.php')) {
59
            $this->publishes([$path => $this->app->configPath(str_replace('-', '.', $namespace).'.php')], $namespace.'-config');
0 ignored issues
show
Bug introduced by
The method publishes() does not exist on Rinvex\Support\Traits\ConsoleTools. Did you maybe mean publishesMigrations()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
60
        }
61
    }
62
63
    /**
64
     * Publish package views.
65
     *
66
     * @return void
67
     */
68
    protected function publishesViews(string $package, bool $isModule = false): void
69
    {
70
        if ($this->publishesResources()) {
71
            return;
72
        }
73
74
        $namespace = str_replace('laravel-', '', $package);
75
        $namespace = str_replace(['/', '\\', '.', '_'], '-', $namespace);
76
        $basePath = $isModule ? $this->app->path($package)
77
            : $this->app->basePath('vendor/'.$package);
78
79
        if (file_exists($path = $basePath.'/resources/views')) {
80
            $this->publishes([$path => $this->app->resourcePath('views/vendor/'.$package)], $namespace.'-views');
0 ignored issues
show
Bug introduced by
The method publishes() does not exist on Rinvex\Support\Traits\ConsoleTools. Did you maybe mean publishesMigrations()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
81
        }
82
    }
83
84
    /**
85
     * Publish package lang.
86
     *
87
     * @return void
88
     */
89
    protected function publishesLang(string $package, bool $isModule = false): void
90
    {
91
        if ($this->publishesResources()) {
92
            return;
93
        }
94
95
        $namespace = str_replace('laravel-', '', $package);
96
        $namespace = str_replace(['/', '\\', '.', '_'], '-', $namespace);
97
        $basePath = $isModule ? $this->app->path($package)
98
            : $this->app->basePath('vendor/'.$package);
99
100
        if (file_exists($path = $basePath.'/resources/lang')) {
101
            $this->publishes([$path => $this->app->resourcePath('lang/vendor/'.$package)], $namespace.'-lang');
0 ignored issues
show
Bug introduced by
The method publishes() does not exist on Rinvex\Support\Traits\ConsoleTools. Did you maybe mean publishesMigrations()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
102
        }
103
    }
104
105
    /**
106
     * Register console commands.
107
     *
108
     * @return void
109
     */
110
    protected function registerCommands(): void
111
    {
112
        // Register artisan commands
113
        foreach ($this->commands as $key => $value) {
0 ignored issues
show
Bug introduced by
The property commands 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...
114
            $this->app->singleton($value, $key);
115
        }
116
117
        $this->commands(array_values($this->commands));
0 ignored issues
show
Bug introduced by
The method commands() does not exist on Rinvex\Support\Traits\ConsoleTools. Did you maybe mean registerCommands()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
118
    }
119
120
    /**
121
     * Can publish resources.
122
     *
123
     * @return bool
124
     */
125
    protected function publishesResources(): bool
126
    {
127
        return ! $this->app->environment('production');
128
    }
129
130
    /**
131
     * Can autoload migrations.
132
     *
133
     * @param string $config
134
     *
135
     * @return bool
136
     */
137
    protected function autoloadMigrations(string $config): bool
138
    {
139
        return ! $this->app->environment('production') && $this->app['config'][$config.'.autoload_migrations'];
140
    }
141
}
142