Completed
Push — master ( eae31d...537fe0 )
by ARCANEDEV
13s queued 11s
created

HasConfig::publishMultipleConfigs()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 11
ccs 0
cts 7
cp 0
crap 6
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arcanedev\Support\Providers\Concerns;
6
7
use Illuminate\Support\Str;
8
9
/**
10
 * Trait     HasConfig
11
 *
12
 * @package  Arcanedev\Support\Providers\Concerns
13
 * @author   ARCANEDEV <[email protected]>
14
 */
15
trait HasConfig
16
{
17
    /* -----------------------------------------------------------------
18
     |  Properties
19
     | -----------------------------------------------------------------
20
     */
21
22
    /**
23
     * Merge multiple config files into one instance (package name as root key)
24
     *
25
     * @var bool
26
     */
27
    protected $multiConfigs = false;
28
29
    /* -----------------------------------------------------------------
30
     |  Main Methods
31
     | -----------------------------------------------------------------
32
     */
33
34
    /**
35
     * Get config folder.
36
     *
37
     * @return string
38
     */
39
    protected function getConfigFolder(): string
40
    {
41
        return realpath($this->getBasePath().DIRECTORY_SEPARATOR.'config');
0 ignored issues
show
Bug introduced by
It seems like getBasePath() 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...
42
    }
43
44
    /**
45
     * Get config key.
46
     *
47
     * @return string
48
     */
49 18
    protected function getConfigKey(): string
50
    {
51 18
        return Str::slug($this->getPackageName());
0 ignored issues
show
Bug introduced by
It seems like getPackageName() 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...
52
    }
53
54
    /**
55
     * Get config file path.
56
     *
57
     * @return string
58
     */
59 18
    protected function getConfigFile(): string
60
    {
61 18
        return $this->getConfigFolder().DIRECTORY_SEPARATOR."{$this->getPackageName()}.php";
0 ignored issues
show
Bug introduced by
It seems like getPackageName() 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...
62
    }
63
64
    /**
65
     * Get the config files (paths).
66
     *
67
     * @return array|false
68
     */
69
    protected function configFilesPaths()
70
    {
71
        return glob($this->getConfigFolder().DIRECTORY_SEPARATOR.'*.php');
72
    }
73
74
    /**
75
     * Register configs.
76
     *
77
     * @param  string  $separator
78
     */
79 18
    protected function registerConfig(string $separator = '.'): void
80
    {
81 18
        $this->multiConfigs
82
            ? $this->registerMultipleConfigs($separator)
83 18
            : $this->registerSingleConfig();
84 18
    }
85
86
    /**
87
     * Register a single config file.
88
     */
89 18
    protected function registerSingleConfig(): void
90
    {
91 18
        $this->mergeConfigFrom($this->getConfigFile(), $this->getConfigKey());
0 ignored issues
show
Bug introduced by
It seems like mergeConfigFrom() 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...
92 18
    }
93
94
    /**
95
     * Register all package configs.
96
     *
97
     * @param  string  $separator
98
     */
99
    protected function registerMultipleConfigs(string $separator = '.'): void
100
    {
101
        foreach ($this->configFilesPaths() as $configPath) {
0 ignored issues
show
Bug introduced by
The expression $this->configFilesPaths() of type array|false is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
102
            $this->mergeConfigFrom(
0 ignored issues
show
Bug introduced by
It seems like mergeConfigFrom() 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...
103
                $configPath, $this->getConfigKey().$separator.basename($configPath, '.php')
104
            );
105
        }
106
    }
107
108
    /**
109
     * Publish the config file.
110
     *
111
     * @param  string|null  $path
112
     */
113
    protected function publishConfig(?string $path = null): void
114
    {
115
        $this->multiConfigs
116
            ? $this->publishMultipleConfigs()
117
            : $this->publishSingleConfig($path);
118
    }
119
120
    /**
121
     * Publish a single config file.
122
     *
123
     * @param  string|null  $path
124
     */
125
    protected function publishSingleConfig(?string $path = null): void
126
    {
127
        $this->publishes([
0 ignored issues
show
Bug introduced by
It seems like publishes() 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...
128
            $this->getConfigFile() => $path ?: config_path("{$this->getPackageName()}.php"),
0 ignored issues
show
Bug introduced by
It seems like getPackageName() 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...
129
        ], $this->getPublishedTags('config'));
0 ignored issues
show
Bug introduced by
It seems like getPublishedTags() 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...
130
    }
131
132
    /**
133
     * Publish multiple config files.
134
     */
135
    protected function publishMultipleConfigs(): void
136
    {
137
        $paths   = [];
138
        $package = $this->getVendorName().DIRECTORY_SEPARATOR.$this->getPackageName();
0 ignored issues
show
Bug introduced by
It seems like getVendorName() 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...
Bug introduced by
It seems like getPackageName() 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...
139
140
        foreach ($this->configFilesPaths() as $file) {
0 ignored issues
show
Bug introduced by
The expression $this->configFilesPaths() of type array|false is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
141
            $paths[$file] = config_path($package.DIRECTORY_SEPARATOR.basename($file));
142
        }
143
144
        $this->publishes($paths, $this->getPublishedTags('config'));
0 ignored issues
show
Bug introduced by
It seems like getPublishedTags() 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...
Bug introduced by
It seems like publishes() 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...
145
    }
146
}
147