Completed
Push — master ( 0da2a2...86dea6 )
by Loban
01:58
created

QuickAccessBehavior   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 59
ccs 23
cts 23
cp 1
rs 10
wmc 7
lcom 2
cbo 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A attach() 0 6 1
A beforeGetValue() 0 10 2
A afterDecodeValue() 0 11 3
A replace() 0 6 1
1
<?php
2
/**
3
 * @link https://github.com/LAV45/yii2-settings
4
 * @copyright Copyright (c) 2016 LAV45
5
 * @author Alexey Loban <[email protected]>
6
 * @license http://opensource.org/licenses/BSD-3-Clause
7
 */
8
9
namespace lav45\settings\behaviors;
10
11
use yii\base\Behavior;
12
use lav45\settings\Settings;
13
use lav45\settings\events\GetEvent;
14
use lav45\settings\events\DecodeEvent;
15
use lav45\settings\helpers\ArrayHelper;
16
17
/**
18
 * Class QuickAccessBehavior
19
 * @package lav45\settings\behaviors
20
 *
21
 * @property Settings $owner
22
 */
23
class QuickAccessBehavior extends Behavior
24
{
25
    /**
26
     * @var string
27
     */
28
    private $_originKey;
29
30
    /**
31
     * @param Settings $owner
32
     */
33 4
    public function attach($owner)
34
    {
35 4
        parent::attach($owner);
36 4
        $owner->on(Settings::EVENT_BEFORE_GET, [$this, 'beforeGetValue'], null, false);
37 4
        $owner->on(Settings::EVENT_AFTER_DECODE_VALUE, [$this, 'afterDecodeValue']);
38 4
    }
39
40
    /**
41
     * @param GetEvent $event
42
     */
43 4
    public function beforeGetValue(GetEvent $event)
44
    {
45 4
        $key = $event->key;
46 4
        if (($pos = strpos($key, '.')) === false) {
47 2
            $this->_originKey = null;
48
        } else {
49 3
            $this->_originKey = $key;
0 ignored issues
show
Documentation Bug introduced by
It seems like $key can also be of type array. However, the property $_originKey is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
50 3
            $event->key = substr($key, 0, $pos);
51
        }
52 4
    }
53
54
    /**
55
     * @param DecodeEvent $event
56
     */
57 3
    public function afterDecodeValue(DecodeEvent $event)
58
    {
59 3
        if ($this->_originKey === null) {
60 2
            return;
61
        }
62
63 2
        $key = substr($this->_originKey, strlen($event->key) + 1);
64 2
        if ($key !== false) {
65 2
            $event->value = ArrayHelper::getValue($event->value, $key, $event->default);
66
        }
67 2
    }
68
69
    /**
70
     * @param string|array $key
71
     * @param string $path
72
     * @param mixed $value
73
     * @return bool
74
     */
75 1
    public function replace($key, $path, $value)
76
    {
77 1
        $data = $this->owner->get($key, []);
78 1
        $data = ArrayHelper::setValue($data, $path, $value);
79 1
        return $this->owner->set($key, $data);
80
    }
81
}