Hooks::getAttribute()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 12

Duplication

Lines 12
Ratio 100 %

Importance

Changes 0
Metric Value
dl 12
loc 12
rs 9.8666
c 0
b 0
f 0
cc 2
nc 1
nop 0
1
<?php
2
3
namespace Sofa\Eloquence\Mappable;
4
5
use BadMethodCallException;
6
7
/**
8
 * This class provides instance scope for the closures
9
 * so they can be rebound later onto the acutal model.
10
 */
11
class Hooks
12
{
13
    /**
14
     * Register hook on customWhere method.
15
     *
16
     * @codeCoverageIgnore
17
     *
18
     * @return \Closure
19
     */
20
    public function queryHook()
21
    {
22
        return function ($next, $query, $bag) {
23
            $method = $bag->get('method');
24
            $args = $bag->get('args');
25
            $column = $args->get('column');
26
27
            if ($this->hasMapping($column)) {
0 ignored issues
show
Documentation Bug introduced by
The method hasMapping does not exist on object<Sofa\Eloquence\Mappable\Hooks>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
28
                return call_user_func_array([$this, 'mappedQuery'], [$query, $method, $args]);
29
            }
30
31
            if (in_array($method, ['select', 'addSelect'])) {
32
                call_user_func_array([$this, 'mappedSelect'], [$query, $args]);
33
            }
34
35
            return $next($query, $bag);
36
        };
37
    }
38
39
    /**
40
     * Register hook on save method.
41
     *
42
     * @codeCoverageIgnore
43
     *
44
     * @return \Closure
45
     */
46
    public function save()
47
    {
48
        return function ($next, $value, $args) {
49
            $this->saveMapped();
0 ignored issues
show
Documentation Bug introduced by
The method saveMapped does not exist on object<Sofa\Eloquence\Mappable\Hooks>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
50
51
            return $next($value, $args);
52
        };
53
    }
54
55
    /**
56
     * Register hook on isset call.
57
     *
58
     * @codeCoverageIgnore
59
     *
60
     * @return \Closure
61
     */
62 View Code Duplication
    public function __issetHook()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
63
    {
64
        return function ($next, $isset, $args) {
65
            $key = $args->get('key');
66
67
            if (!$isset && $this->hasMapping($key)) {
0 ignored issues
show
Documentation Bug introduced by
The method hasMapping does not exist on object<Sofa\Eloquence\Mappable\Hooks>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
68
                return (bool) $this->mapAttribute($key);
0 ignored issues
show
Documentation Bug introduced by
The method mapAttribute does not exist on object<Sofa\Eloquence\Mappable\Hooks>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
69
            }
70
71
            return $next($isset, $args);
72
        };
73
    }
74
75
    /**
76
     * Register hook on unset call.
77
     *
78
     * @codeCoverageIgnore
79
     *
80
     * @return \Closure
81
     */
82
    public function __unsetHook()
83
    {
84
        return function ($next, $value, $args) {
85
            $key = $args->get('key');
86
87
            if ($this->hasMapping($key)) {
0 ignored issues
show
Documentation Bug introduced by
The method hasMapping does not exist on object<Sofa\Eloquence\Mappable\Hooks>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
88
                return $this->forget($key);
0 ignored issues
show
Documentation Bug introduced by
The method forget does not exist on object<Sofa\Eloquence\Mappable\Hooks>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
89
            }
90
91
            return $next($value, $args);
92
        };
93
    }
94
95
    /**
96
     * Register hook on getAttribute method.
97
     *
98
     * @codeCoverageIgnore
99
     *
100
     * @return \Closure
101
     */
102 View Code Duplication
    public function getAttribute()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
103
    {
104
        return function ($next, $value, $args) {
105
            $key = $args->get('key');
106
107
            if ($this->hasMapping($key)) {
0 ignored issues
show
Documentation Bug introduced by
The method hasMapping does not exist on object<Sofa\Eloquence\Mappable\Hooks>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
108
                $value = $this->mapAttribute($key);
0 ignored issues
show
Documentation Bug introduced by
The method mapAttribute does not exist on object<Sofa\Eloquence\Mappable\Hooks>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
109
            }
110
111
            return $next($value, $args);
112
        };
113
    }
114
115
    /**
116
     * Register hook on setAttribute method.
117
     *
118
     * @codeCoverageIgnore
119
     *
120
     * @return \Closure
121
     */
122
    public function setAttribute()
123
    {
124
        return function ($next, $value, $args) {
125
            $key = $args->get('key');
126
127
            if ($this->hasMapping($key)) {
0 ignored issues
show
Documentation Bug introduced by
The method hasMapping does not exist on object<Sofa\Eloquence\Mappable\Hooks>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
128
                return $this->setMappedAttribute($key, $value);
0 ignored issues
show
Documentation Bug introduced by
The method setMappedAttribute does not exist on object<Sofa\Eloquence\Mappable\Hooks>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
129
            }
130
131
            return $next($value, $args);
132
        };
133
    }
134
135
    public function __call($method, $params)
136
    {
137
        if (strpos($method, '__') === 0 && method_exists($this, $method . 'Hook')) {
138
            return call_user_func_array([$this, $method . 'Hook'], $params);
139
        }
140
141
        throw new BadMethodCallException("Method [{$method}] doesn't exist on this object.");
142
    }
143
}
144