Completed
Pull Request — master (#5)
by ARCANEDEV
02:42
created

FormAccessible::getReflection()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4286
cc 2
eloc 4
nc 2
nop 0
crap 2
1
<?php namespace Arcanedev\LaravelHtml\Traits;
2
3
use ReflectionClass;
4
use ReflectionMethod;
5
6
/**
7
 * Class     FormAccessible
8
 *
9
 * @package  Arcanedev\LaravelHtml\Traits
10
 * @author   ARCANEDEV <[email protected]>
11
 */
12
trait FormAccessible
13
{
14
    /* ------------------------------------------------------------------------------------------------
15
     |  Properties
16
     | ------------------------------------------------------------------------------------------------
17
     */
18
    /**
19
     * A cached ReflectionClass instance for $this
20
     *
21
     * @var ReflectionClass
22
     */
23
    protected $reflection;
24
25
    /* ------------------------------------------------------------------------------------------------
26
     |  Main Functions
27
     | ------------------------------------------------------------------------------------------------
28
     */
29
    /**
30
     * @param string $key
31
     *
32
     * @return mixed
33
     */
34 8
    public function getFormValue($key)
35
    {
36 8
        $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?

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...
37
38
        // If the attribute is listed as a date, we will convert it to a DateTime
39
        // instance on retrieval, which makes it quite convenient to work with
40
        // date fields without having to create a mutator for each property.
41 8
        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?

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...
42 8
            if ( ! is_null($value)) {
43 8
                $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?

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...
44 6
            }
45 6
        }
46
47 8
        if ($this->hasFormMutator($key)) {
48 8
            return $this->mutateFormAttribute($key, $value);
49
        }
50
51
        return $value;
52
    }
53
54
    /* ------------------------------------------------------------------------------------------------
55
     |  Other Functions
56
     | ------------------------------------------------------------------------------------------------
57
     */
58
    /**
59
     * Check if has a form mutator.
60
     *
61
     * @param  string  $key
62
     *
63
     * @return bool
64
     */
65 8
    protected function hasFormMutator($key)
66
    {
67 8
        $methods = $this->getReflection()
68 8
            ->getMethods(ReflectionMethod::IS_PUBLIC);
69
70 8
        $mutator = collect($methods)
71 8
            ->first(function ($index, ReflectionMethod $method) use ($key) {
72 8
                return $method->getName() === 'form' . str_studly($key) . 'Attribute';
0 ignored issues
show
Bug introduced by
Consider using $method->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
73 8
            });
74
75 8
        return (bool) $mutator;
76
    }
77
78
    /**
79
     * Mutate the form attribute.
80
     *
81
     * @param  string  $key
82
     * @param  mixed   $value
83
     *
84
     * @return mixed
85
     */
86 8
    private function mutateFormAttribute($key, $value)
87
    {
88 8
        return $this->{'form' . str_studly($key) . 'Attribute'}($value);
89
    }
90
91
    /**
92
     * Get a ReflectionClass Instance
93
     *
94
     * @return ReflectionClass
95
     */
96 8
    protected function getReflection()
97
    {
98 8
        if ( ! $this->reflection) {
99 8
            $this->reflection = new ReflectionClass($this);
100 6
        }
101
102 8
        return $this->reflection;
103
    }
104
}
105