Completed
Pull Request — master (#36)
by ARCANEDEV
01:02
created

HasConfig::configFilesPaths()   A

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