Test Failed
Push — master ( 2a75d8...e4b8e4 )
by Julien
12:49
created

Attribute::getAttribute()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 14
rs 10
1
<?php
2
3
/**
4
 * This file is part of the Zemit Framework.
5
 *
6
 * (c) Zemit Team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE.txt
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Zemit\Mvc\Model;
13
14
use Phalcon\Mvc\ModelInterface;
15
use Phalcon\Text;
16
use Zemit\Mvc\Model\AbstractTrait\AbstractMetaData;
17
18
trait Attribute
19
{
20
    use AbstractMetaData;
21
    
22
    /**
23
     * Method to get attribute from getters or the readAttribute
24
     * @param string $attribute
25
     * @return mixed|null
26
     */
27
    public function getAttribute(string $attribute)
28
    {
29
        assert($this instanceof ModelInterface);
30
        if ($this->getModelsMetaData()->hasAttribute($this, $attribute)) {
31
            
32
            $method = 'get' . ucfirst(Text::camelize($attribute));
33
            if (method_exists($this, $method)) {
34
                return $this->$method();
35
            }
36
            
37
            return $this->readAttribute($attribute);
0 ignored issues
show
Bug introduced by
The method readAttribute() does not exist on Phalcon\Mvc\ModelInterface. It seems like you code against a sub-type of Phalcon\Mvc\ModelInterface such as Phalcon\Mvc\Model or Zemit\Models\Session or Zemit\Models\Audit or Zemit\Models\Role. ( Ignorable by Annotation )

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

37
            return $this->/** @scrutinizer ignore-call */ readAttribute($attribute);
Loading history...
Bug introduced by
It seems like readAttribute() 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

37
            return $this->/** @scrutinizer ignore-call */ readAttribute($attribute);
Loading history...
38
        }
39
        
40
        return null;
41
    }
42
    
43
    /**
44
     * Method to set attribute from setter or the writeAttribute
45
     * @param string $attribute
46
     * @param mixed|null $value
47
     * @return mixed|null
48
     */
49
    public function setAttribute(string $attribute, $value): void
50
    {
51
        assert($this instanceof ModelInterface);
52
        if ($this->getModelsMetaData()->hasAttribute($this, $attribute)) {
53
            
54
            $method = 'set' . ucfirst(Text::camelize($attribute));
55
            if (method_exists($this, $method)) {
56
                $this->$method();
57
            }
58
            
59
            $this->writeAttribute($attribute, $value);
0 ignored issues
show
Bug introduced by
The method writeAttribute() does not exist on Phalcon\Mvc\ModelInterface. It seems like you code against a sub-type of Phalcon\Mvc\ModelInterface such as Phalcon\Mvc\Model or Zemit\Models\Session or Zemit\Models\Audit or Zemit\Models\Role. ( Ignorable by Annotation )

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

59
            $this->/** @scrutinizer ignore-call */ 
60
                   writeAttribute($attribute, $value);
Loading history...
Bug introduced by
It seems like writeAttribute() 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

59
            $this->/** @scrutinizer ignore-call */ 
60
                   writeAttribute($attribute, $value);
Loading history...
60
        }
61
    }
62
}
63