HasConfig::getConfigFolder()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 2
cp 0
crap 2
rs 10
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
 * @author   ARCANEDEV <[email protected]>
13
 */
14
trait HasConfig
15
{
16
    /* -----------------------------------------------------------------
17
     |  Properties
18
     | -----------------------------------------------------------------
19
     */
20
21
    /**
22
     * Merge multiple config files into one instance (package name as root key)
23
     *
24
     * @var bool
25
     */
26
    protected $multiConfigs = false;
27
28
    /* -----------------------------------------------------------------
29
     |  Main Methods
30
     | -----------------------------------------------------------------
31
     */
32
33
    /**
34
     * Get config folder.
35
     *
36
     * @return string
37
     */
38
    protected function getConfigFolder(): string
39
    {
40
        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...
41
    }
42
43
    /**
44
     * Get config key.
45
     *
46
     * @param  bool    $withVendor
47
     * @param  string  $separator
48
     *
49
     * @return string
50
     */
51 18
    protected function getConfigKey(bool $withVendor = false, string $separator = '.'): string
52
    {
53 18
        $package = 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...
54
55 18
        return $withVendor
56
            ? Str::slug($this->getVendorName()).$separator.$package
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...
57 18
            : $package;
58
    }
59
60
    /**
61
     * Get config file path.
62
     *
63
     * @return string
64
     */
65 18
    protected function getConfigFile(): string
66
    {
67 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...
68
    }
69
70
    /**
71
     * Get the config files (paths).
72
     *
73
     * @return array|false
74
     */
75
    protected function configFilesPaths()
76
    {
77
        return glob($this->getConfigFolder().DIRECTORY_SEPARATOR.'*.php');
78
    }
79
80
    /**
81
     * Register configs.
82
     *
83
     * @param  string  $separator
84
     */
85 18
    protected function registerConfig(string $separator = '.'): void
86
    {
87 18
        $this->multiConfigs
88
            ? $this->registerMultipleConfigs($separator)
89 18
            : $this->registerSingleConfig();
90 18
    }
91
92
    /**
93
     * Register a single config file.
94
     */
95 18
    protected function registerSingleConfig(): void
96
    {
97 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...
98 18
    }
99
100
    /**
101
     * Register all package configs.
102
     *
103
     * @param  string  $separator
104
     */
105
    protected function registerMultipleConfigs(string $separator = '.'): void
106
    {
107
        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...
108
            $key = $this->getConfigKey(true, $separator).$separator.basename($path, '.php');
109
110
            $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...
111
        }
112
    }
113
114
    /**
115
     * Publish the config file.
116
     *
117
     * @param  string|null  $path
118
     */
119
    protected function publishConfig(?string $path = null): void
120
    {
121
        $this->multiConfigs
122
            ? $this->publishMultipleConfigs()
123
            : $this->publishSingleConfig($path);
124
    }
125
126
    /**
127
     * Publish a single config file.
128
     *
129
     * @param  string|null  $path
130
     */
131
    protected function publishSingleConfig(?string $path = null): void
132
    {
133
        $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...
134
            $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...
135
        ], $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...
136
    }
137
138
    /**
139
     * Publish multiple config files.
140
     */
141
    protected function publishMultipleConfigs(): void
142
    {
143
        $paths   = [];
144
        $package = $this->getConfigKey(true, DIRECTORY_SEPARATOR);
145
146
        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...
147
            $paths[$file] = config_path($package.DIRECTORY_SEPARATOR.basename($file));
148
        }
149
150
        $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...
151
    }
152
}
153