ValueReplacerTrait::old()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Zoutapps\Laravel\Backpack\Branding\Traits;
4
5
use Illuminate\Support\Str;
6
7
trait ValueReplacerTrait
8
{
9
    protected $file;
10
11
    protected $appends = false;
12
    protected $defaults;
13
14
    protected function escape($value)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
15
    {
16
        return $value;
17
    }
18
19
    protected function old($key)
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
20
    {
21
        return null;
22
    }
23
24
    protected function replace($key, $value)
25
    {
26
        return $key . '=' . $value;
27
    }
28
29
    protected function search($key, $old)
30
    {
31
        return '/^' . $key . preg_quote('=' . $old, '/') . '.*$/m';
32
    }
33
34
    protected function applyValues()
35
    {
36
        $this->command->line('Enter the values you get asked for.');
0 ignored issues
show
Bug introduced by
The property command 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...
37
        $this->command->line('Leave blank if you don\'t want to change');
38
39
        collect($this->keys)->each(function ($key) {
0 ignored issues
show
Bug introduced by
The property keys 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...
40
            if ($this->defaults->has($key)) {
41
                $this->setValueForKey($this->defaults[$key], $key);
42
                $this->defaults->forget($key);
43
            } else {
44
                $value = $this->command->ask($key .' ('.$this->old($key).')');
45
                if ($value) {
46
                    $this->setValueForKey($value, $key);
47
                } else {
48
                    $this->command->line('<comment>'.$key . '</comment> not changed.');
49
                }
50
            }
51
        });
52
53
        $this->defaults->each(function ($value, $key) {
54
           $this->setValueForKey($value, $key);
55
        });
56
    }
57
58
    protected function setValueForKey($value, $key)
59
    {
60
        if (is_bool($value)) {
61
            $value = $value ? 'true': 'false';
62
        }
63
64
        $value = $this->escape($value);
65
        $replace = $this->replace($key, $value);
66
67
        if (Str::contains(file_get_contents($this->file), $key) === false) {
68
            if (!$this->appends) {
69
                return;
70
            }
71
            file_put_contents($this->file, PHP_EOL . $replace, FILE_APPEND);
72
        } else {
73
            $old = $this->old($key);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $old is correct as $this->old($key) (which targets Zoutapps\Laravel\Backpac...lueReplacerTrait::old()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
74
            $search = $this->search($key, $old);
75
76
            $currentContent = file_get_contents($this->file);
77
            $replacedContent = preg_replace($search, $replace, $currentContent);
78
            file_put_contents($this->file, $replacedContent);
79
        }
80
        if (isset($this->command)) {
81
            $this->command->line("Setting <comment>{$replace}</comment>");
82
        }
83
84
    }
85
}