Completed
Push — master ( d2ea38...de9ee1 )
by Abdelrahman
02:52 queued 01:41
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
        $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...
22
            : $this->app->basePath('vendor/'.$package);
23
24
        if (file_exists($path = $basePath.'/database/migrations')) {
25
            $stubs = $this->app['files']->glob($path.'/*.php');
26
            $existing = $this->app['files']->glob($this->app->databasePath('migrations/'.$package.'/*.php'));
27
28
            $migrations = collect($stubs)->flatMap(function ($migration) use ($existing, $package) {
29
                $sequence = mb_substr(basename($migration), 0, 17);
30
                $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...
31
                    return mb_strpos($item, str_replace($sequence, '', basename($migration))) !== false;
32
                });
33
34
                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))))];
35
            })->toArray();
36
37
            $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...
38
        }
39
    }
40
41
    /**
42
     * Publish package config.
43
     *
44
     * @return void
45
     */
46
    protected function publishesConfig(string $package, bool $isModule = false): void
47
    {
48
        if (! $this->publishesResources()) {
49
            return;
50
        }
51
52
        $namespace = str_replace('laravel-', '', $package);
53
        $basePath = $isModule ? $this->app->path($package)
54
            : $this->app->basePath('vendor/'.$package);
55
56
        if (file_exists($path = $basePath.'/config/config.php')) {
57
            $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...
58
        }
59
    }
60
61
    /**
62
     * Publish package views.
63
     *
64
     * @return void
65
     */
66
    protected function publishesViews(string $package, bool $isModule = false): void
67
    {
68
        if (! $this->publishesResources()) {
69
            return;
70
        }
71
72
        $namespace = str_replace('laravel-', '', $package);
73
        $basePath = $isModule ? $this->app->path($package)
74
            : $this->app->basePath('vendor/'.$package);
75
76
        if (file_exists($path = $basePath.'/resources/views')) {
77
            $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...
78
        }
79
    }
80
81
    /**
82
     * Publish package lang.
83
     *
84
     * @return void
85
     */
86
    protected function publishesLang(string $package, bool $isModule = false): void
87
    {
88
        if (! $this->publishesResources()) {
89
            return;
90
        }
91
92
        $namespace = str_replace('laravel-', '', $package);
93
        $basePath = $isModule ? $this->app->path($package)
94
            : $this->app->basePath('vendor/'.$package);
95
96
        if (file_exists($path = $basePath.'/resources/lang')) {
97
            $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...
98
        }
99
    }
100
101
    /**
102
     * Determine if the application is running in the console.
103
     *
104
     * @TODO: Implement this method to detect if we're in active dev zone or not!
105
     *        Ex: running inside cortex/console action
106
     *
107
     * @return bool
108
     */
109
    public function runningInDevzone()
110
    {
111
        return true;
112
    }
113
114
    /**
115
     * Register console commands.
116
     *
117
     * @return void
118
     */
119
    protected function registerCommands(): void
120
    {
121
        if (! $this->app->runningInConsole() && ! $this->runningInDevzone()) {
122
            return;
123
        }
124
125
        if (! $this->app->environment('production') && property_exists($this, 'devCommands')) {
126
            // Register artisan devCommands
127
            foreach ($this->devCommands as $key => $value) {
0 ignored issues
show
Bug introduced by
The property devCommands 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...
128
                $this->app->singleton($value, $key);
129
            }
130
131
            $this->commands(array_values($this->devCommands));
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...
132
        }
133
134
        if (property_exists($this, 'commands')) {
135
            // Register artisan commands
136
            foreach ($this->commands as $key => $value) {
0 ignored issues
show
Bug introduced by
The property commands does not seem to exist. Did you mean devCommands?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
137
                $this->app->singleton($value, $key);
138
            }
139
140
            $this->commands(array_values($this->commands));
0 ignored issues
show
Bug introduced by
The property commands does not seem to exist. Did you mean devCommands?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
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...
141
        }
142
    }
143
144
    /**
145
     * Can publish resources.
146
     *
147
     * @return bool
148
     */
149
    protected function publishesResources(): bool
150
    {
151
        return ! $this->app->environment('production') || $this->app->runningInConsole() || $this->runningInDevzone();
152
    }
153
154
    /**
155
     * Can autoload migrations.
156
     *
157
     * @param string $config
158
     *
159
     * @return bool
160
     */
161
    protected function autoloadMigrations(string $config): bool
162
    {
163
        return $this->publishesResources() && $this->app['config'][str_replace(['laravel-', '/'], ['', '.'], $config).'.autoload_migrations'];
164
    }
165
}
166