Replacer::configClass()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Mnabialek\LaravelModular\Traits;
4
5
use Illuminate\Support\Collection;
6
use Mnabialek\LaravelModular\Models\Module;
7
use Mnabialek\LaravelModular\Services\Config;
8
9
trait Replacer
10
{
11
    /**
12
     * Replace given string with default replacements and optionally user given
13
     *
14
     * @param string $string
15
     * @param Module $module
16
     * @param array $replacements
17
     *
18
     * @return string
19
     */
20
    protected function replace($string, Module $module, array $replacements = [])
21
    {
22
        $replacements = $this->getReplacements($module, $replacements);
23
24
        return str_replace($replacements->keys()->all(),
25
            $replacements->values()->all(), $string);
26
    }
27
28
    /**
29
     * Get replacement array that will be used for replace in string
30
     *
31
     * @param Module $module
32
     * @param array $definedReplacements
33
     *
34
     * @return Collection
35
     */
36
    private function getReplacements(Module $module, array $definedReplacements)
37
    {
38
        $replacements = collect();
39
40
        collect([
41
            'module' => $module->name(),
42
            'class' => $module->name(),
43
            'moduleNamespace' => $module->name(),
44
            'namespace' => rtrim($this->configClass()->modulesNamespace(), '\\'),
45
            'plural|lower' => mb_strtolower(str_plural($module->name())),
46
        ])->merge($definedReplacements)
47
            ->each(function ($value, $key) use ($replacements) {
48
                $replacements->put($this->configClass()->startSeparator() .
49
                    $key . $this->configClass()->endSeparator(), $value);
50
            });
51
52
        return $replacements;
53
    }
54
55
    /**
56
     * Get config class instance
57
     *
58
     * @return Config
59
     */
60
    private function configClass()
61
    {
62
        return $this->laravel['modular.config'];
0 ignored issues
show
Bug introduced by
The property laravel 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...
63
    }
64
}
65