Completed
Push — master ( 998c8e...4e843c )
by Hong
03:08
created

DelegatorTrait   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 131
wmc 12
lcom 1
cbo 0
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A addRegistry() 0 15 2
A hasInLookup() 0 11 3
A getFromLookup() 0 9 3
A removeFromLookup() 0 12 4
hasInRegistry() 0 4 ?
getFromRegistry() 0 4 ?
1
<?php
2
/**
3
 * Phossa Project
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  Library
8
 * @package   Phossa2\Shared
9
 * @copyright Copyright (c) 2016 phossa.com
10
 * @license   http://mit-license.org/ MIT License
11
 * @link      http://www.phossa.com/
12
 */
13
/*# declare(strict_types=1); */
14
15
namespace Phossa2\Shared\Delegator;
16
17
/**
18
 * DelegatorTrait
19
 *
20
 * Implementation of DelegatorInterface
21
 *
22
 * @package Phossa2\Shared
23
 * @author  Hong Zhang <[email protected]>
24
 * @see     DelegatorInterface
25
 * @version 2.0.8
26
 * @since   2.0.8  added
27
 * @since   2.0.15 modified, moved to new namespace
28
 */
29
trait DelegatorTrait
30
{
31
    /**
32
     * lookup pool of registries
33
     *
34
     * @var    array
35
     * @access protected
36
     */
37
    protected $lookup_pool = [];
38
39
    /**
40
     * cached found key
41
     *
42
     * @var    string
43
     * @access private
44
     */
45
    private $cache_key;
46
47
    /**
48
     * cached lookup registry for found $cache_key
49
     *
50
     * @var    object
51
     * @access private
52
     */
53
    private $cache_reg;
54
55
    /**
56
     * Append one registry to lookup pool
57
     *
58
     * @param  object $registry
59
     * @return $this
60
     * @access protected
61
     */
62
    protected function addRegistry($registry)
63
    {
64
        // remove it if exists already
65
        $this->removeFromLookup($registry);
66
67
        // set delegator in registry
68
        if ($registry instanceof DelegatorAwareInterface) {
69
            $registry->setDelegator($this);
70
        }
71
72
        // append to the pool
73
        $this->lookup_pool[] = $registry;
74
75
        return $this;
76
    }
77
78
    /**
79
     * check existence in the whole lookup pool
80
     *
81
     * @param  string $key
82
     * @return bool
83
     * @access protected
84
     */
85
    protected function hasInLookup(/*# string */ $key)/*# : bool */
86
    {
87
        foreach ($this->lookup_pool as $registry) {
88
            if ($this->hasInRegistry($registry, $key)) {
0 ignored issues
show
Documentation introduced by
$key is of type string, but the function expects a object<Phossa2\Shared\Delegator\name>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
89
                $this->cache_key = $key;
90
                $this->cache_reg = $registry;
91
                return true;
92
            }
93
        }
94
        return false;
95
    }
96
97
    /**
98
     * get from lookup pool, return NULL if not found
99
     *
100
     * @param  string $key
101
     * @return mixed|null
102
     * @access protected
103
     */
104
    protected function getFromLookup(/*# string */ $key)
105
    {
106
        // found already ? or try find
107
        if ($key === $this->cache_key || $this->hasInLookup($key)) {
108
            return $this->getFromRegistry($this->cache_reg, $key);
0 ignored issues
show
Documentation introduced by
$key is of type string, but the function expects a object<Phossa2\Shared\Delegator\name>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
109
        }
110
        // not found
111
        return null;
112
    }
113
114
    /**
115
     * Remove one registry from the pool
116
     *
117
     * @param  object $registry
118
     * @return $this
119
     * @access protected
120
     */
121
    protected function removeFromLookup($registry)
122
    {
123
        foreach ($this->lookup_pool as $idx => $reg) {
124
            if ($registry === $reg) {
125
                if ($reg instanceof DelegatorAwareInterface) {
126
                    $reg->setDelegator(null);
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a object<Phossa2\Shared\De...tor\DelegatorInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
127
                }
128
                unset($this->lookup_pool[$idx]);
129
            }
130
        }
131
        return $this;
132
    }
133
134
    /**
135
     * Try HAS in registry
136
     *
137
     * @param  object $registry
138
     * @param  name $key
139
     * @return bool
140
     * @access protected
141
     */
142
    abstract protected function hasInRegistry(
143
        $registry,
144
        /*# string */ $key
145
    )/*# : bool */;
146
147
    /**
148
     * Try GET from registry
149
     *
150
     * @param  object $registry
151
     * @param  name $key
152
     * @return mixed|null
153
     * @access protected
154
     */
155
    abstract protected function getFromRegistry(
156
        $registry,
157
        /*# string */ $key
158
    );
159
}
160