Component   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 17
c 1
b 0
f 0
dl 0
loc 92
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A isNotWired() 0 3 1
A render() 0 9 1
A isWired() 0 3 1
A id() 0 11 3
A convertBracketsToDots() 0 3 1
A wireModifier() 0 5 2
A generateIdByName() 0 3 1
1
<?php
2
3
namespace ProtoneMedia\LaravelFormComponents\Components;
4
5
use Illuminate\Support\Str;
6
use Illuminate\View\Component as BaseComponent;
7
use ProtoneMedia\LaravelFormComponents\FormDataBinder;
8
9
abstract class Component extends BaseComponent
10
{
11
    /**
12
     * ID for this component.
13
     *
14
     * @var string
15
     */
16
    private $id;
17
18
    /**
19
     * {@inheritDoc}
20
     */
21
    public function render()
22
    {
23
        $alias = Str::kebab(class_basename($this));
24
25
        $config = config("form-components.components.{$alias}");
26
27
        $framework = config("form-components.framework");
28
29
        return str_replace('{framework}', $framework, $config['view']);
30
    }
31
32
    /**
33
     * Returns a boolean wether the form is wired to a Livewire component.
34
     *
35
     * @return boolean
36
     */
37
    public function isWired(): bool
38
    {
39
        return app(FormDataBinder::class)->isWired();
40
    }
41
42
    /**
43
     * The inversion of 'isWired()'.
44
     *
45
     * @return boolean
46
     */
47
    public function isNotWired(): bool
48
    {
49
        return !$this->isWired();
50
    }
51
52
    /**
53
     * Returns the optional wire modifier.
54
     *
55
     * @return string
56
     */
57
    public function wireModifier(): ?string
58
    {
59
        $modifier = app(FormDataBinder::class)->getWireModifier();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $modifier is correct as app(ProtoneMedia\Laravel...ass)->getWireModifier() targeting ProtoneMedia\LaravelForm...nder::getWireModifier() 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...
60
61
        return $modifier ? ".{$modifier}" : null;
0 ignored issues
show
introduced by
$modifier is of type null, thus it always evaluated to false.
Loading history...
62
    }
63
64
    /**
65
     * Generates an ID, once, for this component.
66
     *
67
     * @return string
68
     */
69
    public function id(): string
70
    {
71
        if ($this->id) {
72
            return $this->id;
73
        }
74
75
        if ($this->name) {
76
            return $this->id = $this->generateIdByName();
77
        }
78
79
        return $this->id = Str::random(4);
80
    }
81
82
    /**
83
     * Generates an ID by the name attribute.
84
     *
85
     * @return string
86
     */
87
    protected function generateIdByName(): string
88
    {
89
        return "auto_id_" . $this->name;
90
    }
91
92
    /**
93
     * Converts a bracket-notation to a dotted-notation
94
     *
95
     * @param string $name
96
     * @return string
97
     */
98
    protected static function convertBracketsToDots($name): string
99
    {
100
        return str_replace(['[', ']'], ['.', ''], $name);
101
    }
102
}
103