Completed
Push — master ( 936da0...f7c12a )
by Pascal
14:46 queued 13:50
created

HandlesDefaultAndOldValue::setValue()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.9137
c 0
b 0
f 0
cc 6
nc 6
nop 4
1
<?php
2
3
namespace ProtoneMedia\LaravelFormComponents\Components;
4
5
trait HandlesDefaultAndOldValue
6
{
7
    use HandlesBoundValues;
8
9
    private function setValue(
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
10
        string $name,
11
        $bind = null,
12
        $default = null,
13
        $language = null
14
    ) {
15
        if ($this->isWired()) {
0 ignored issues
show
Bug introduced by
It seems like isWired() 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...
16
            return;
17
        }
18
19
        if (!$language) {
20
            $default = $this->getBoundValue($bind, $name) ?: $default;
21
22
            return $this->value = old($name, $default);
0 ignored issues
show
Bug introduced by
The property value 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...
23
        }
24
25
        $bind = $this->getBoundTarget($bind, $name);
0 ignored issues
show
Unused Code introduced by
The call to HandlesDefaultAndOldValue::getBoundTarget() has too many arguments starting with $bind.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
26
27
        if ($bind) {
28
            $default = $bind->getTranslation($name, $language, false) ?: $default;
29
        }
30
31
        $this->value = old("{$name}.{$language}", $default);
32
    }
33
}
34