Completed
Push — master ( 537fe0...18e3dc )
by ARCANEDEV
01:30
created

HasConfig   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 31.71%

Importance

Changes 0
Metric Value
dl 0
loc 139
ccs 13
cts 41
cp 0.3171
rs 10
c 0
b 0
f 0
wmc 16
lcom 1
cbo 1

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getConfigFolder() 0 4 1
A getConfigKey() 0 8 2
A getConfigFile() 0 4 1
A configFilesPaths() 0 4 1
A registerConfig() 0 6 2
A registerSingleConfig() 0 4 1
A registerMultipleConfigs() 0 8 2
A publishConfig() 0 6 2
A publishSingleConfig() 0 6 2
A publishMultipleConfigs() 0 11 2
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
     * @param  bool    $withVendor
48
     * @param  string  $separator
49
     *
50
     * @return string
51
     */
52 18
    protected function getConfigKey(bool $withVendor = false, string $separator = '.'): string
53
    {
54 18
        $key = $withVendor
55
            ? $this->getVendorName().' '.$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...
56 18
            : $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...
57
58 18
        return Str::slug($key, $separator);
59
    }
60
61
    /**
62
     * Get config file path.
63
     *
64
     * @return string
65
     */
66 18
    protected function getConfigFile(): string
67
    {
68 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...
69
    }
70
71
    /**
72
     * Get the config files (paths).
73
     *
74
     * @return array|false
75
     */
76
    protected function configFilesPaths()
77
    {
78
        return glob($this->getConfigFolder().DIRECTORY_SEPARATOR.'*.php');
79
    }
80
81
    /**
82
     * Register configs.
83
     *
84
     * @param  string  $separator
85
     */
86 18
    protected function registerConfig(string $separator = '.'): void
87
    {
88 18
        $this->multiConfigs
89
            ? $this->registerMultipleConfigs($separator)
90 18
            : $this->registerSingleConfig();
91 18
    }
92
93
    /**
94
     * Register a single config file.
95
     */
96 18
    protected function registerSingleConfig(): void
97
    {
98 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...
99 18
    }
100
101
    /**
102
     * Register all package configs.
103
     *
104
     * @param  string  $separator
105
     */
106
    protected function registerMultipleConfigs(string $separator = '.'): void
107
    {
108
        foreach ($this->configFilesPaths() as $path) {
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...
109
            $key = $this->getConfigKey(true, $separator).$separator.basename($path, '.php');
110
111
            $this->mergeConfigFrom($path, $key);
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...
112
        }
113
    }
114
115
    /**
116
     * Publish the config file.
117
     *
118
     * @param  string|null  $path
119
     */
120
    protected function publishConfig(?string $path = null): void
121
    {
122
        $this->multiConfigs
123
            ? $this->publishMultipleConfigs()
124
            : $this->publishSingleConfig($path);
125
    }
126
127
    /**
128
     * Publish a single config file.
129
     *
130
     * @param  string|null  $path
131
     */
132
    protected function publishSingleConfig(?string $path = null): void
133
    {
134
        $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...
135
            $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...
136
        ], $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...
137
    }
138
139
    /**
140
     * Publish multiple config files.
141
     */
142
    protected function publishMultipleConfigs(): void
143
    {
144
        $paths   = [];
145
        $package = $this->getConfigKey(true, DIRECTORY_SEPARATOR);
146
147
        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...
148
            $paths[$file] = config_path($package.DIRECTORY_SEPARATOR.basename($file));
149
        }
150
151
        $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...
152
    }
153
}
154