Test Failed
Pull Request — main (#21)
by
unknown
07:08 queued 03:25
created

FormAccessibleTrait   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 14
eloc 27
c 2
b 0
f 0
dl 0
loc 101
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A hasFormMutator() 0 10 1
A getReflection() 0 7 2
B getFormValue() 0 37 9
A mutateFormAttribute() 0 3 1
A isNestedModel() 0 3 1
1
<?php
2
3
namespace Hafijul233\Form\Traits;
4
5
use Illuminate\Support\Str;
6
use ReflectionClass;
7
use ReflectionMethod;
8
9
/**
10
 * Trait FormAccessibleTrait
11
 */
12
trait FormAccessibleTrait
13
{
14
    /**
15
     * A cached ReflectionClass instance for $this
16
     *
17
     * @var ReflectionClass
18
     */
19
    protected $reflection;
20
21
    /**
22
     * @param  string  $key
23
     * @return mixed
24
     */
25
    public function getFormValue(string $key)
26
    {
27
        $value = $this->getAttributeFromArray($key);
0 ignored issues
show
Bug introduced by
It seems like getAttributeFromArray() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

27
        /** @scrutinizer ignore-call */ 
28
        $value = $this->getAttributeFromArray($key);
Loading history...
28
29
        // If the attribute is listed as a date, we will convert it to a DateTime
30
        // instance on retrieval, which makes it quite convenient to work with
31
        // date fields without having to create a mutator for each property.
32
        if (in_array($key, $this->getDates())) {
0 ignored issues
show
Bug introduced by
It seems like getDates() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

32
        if (in_array($key, $this->/** @scrutinizer ignore-call */ getDates())) {
Loading history...
33
            if (! is_null($value)) {
34
                $value = $this->asDateTime($value);
0 ignored issues
show
Bug introduced by
It seems like asDateTime() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

34
                /** @scrutinizer ignore-call */ 
35
                $value = $this->asDateTime($value);
Loading history...
35
            }
36
        }
37
38
        // If the attribute has a get mutator, we will call that then return what
39
        // it returns as the value, which is useful for transforming values on
40
        // retrieval from the model to a form that is more useful for usage.
41
        if ($this->hasFormMutator($key)) {
42
            return $this->mutateFormAttribute($key, $value);
43
        }
44
45
        $keys = explode('.', $key);
46
47
        if ($this->isNestedModel($keys[0])) {
48
            $relatedModel = $this->getRelation($keys[0]);
0 ignored issues
show
Bug introduced by
The method getRelation() does not exist on Hafijul233\Form\Traits\FormAccessibleTrait. Did you maybe mean getReflection()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

48
            /** @scrutinizer ignore-call */ 
49
            $relatedModel = $this->getRelation($keys[0]);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
49
50
            unset($keys[0]);
51
            $key = implode('.', $keys);
52
53
            if (method_exists($relatedModel, 'hasFormMutator') && $key !== '' && $relatedModel->hasFormMutator($key)) {
54
                return $relatedModel->getFormValue($key);
55
            }
56
57
            return data_get($relatedModel, empty($key) ? null : $key);
58
        }
59
60
        // No form mutator, let the model resolve this
61
        return data_get($this, $key);
62
    }
63
64
    /**
65
     * @param  string  $key
66
     * @return bool
67
     */
68
    public function hasFormMutator(string $key): bool
69
    {
70
        $methods = $this->getReflection()->getMethods(ReflectionMethod::IS_PUBLIC);
71
72
        $mutator = collect($methods)
73
            ->first(function (ReflectionMethod $method) use ($key) {
74
                return $method->getName() === 'form'.Str::studly($key).'Attribute';
75
            });
76
77
        return (bool) $mutator;
78
    }
79
80
    /**
81
     * Get a ReflectionClass Instance
82
     *
83
     * @return ReflectionClass
84
     */
85
    protected function getReflection(): ReflectionClass
86
    {
87
        if (! $this->reflection) {
88
            $this->reflection = new ReflectionClass($this);
89
        }
90
91
        return $this->reflection;
92
    }
93
94
    /**
95
     * @param $key
96
     * @param $value
97
     * @return mixed
98
     */
99
    private function mutateFormAttribute($key, $value)
100
    {
101
        return $this->{'form'.Str::studly($key).'Attribute'}($value);
102
    }
103
104
    /**
105
     * Check for a nested model.
106
     *
107
     * @param  string  $key
108
     * @return bool
109
     */
110
    public function isNestedModel(string $key): bool
111
    {
112
        return in_array($key, array_keys($this->getRelations()));
0 ignored issues
show
Bug introduced by
The method getRelations() does not exist on Hafijul233\Form\Traits\FormAccessibleTrait. Did you maybe mean getReflection()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

112
        return in_array($key, array_keys($this->/** @scrutinizer ignore-call */ getRelations()));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
113
    }
114
}
115