Completed
Push — master ( 37e69c...58ba32 )
by Hong
03:12
created

ShareableTrait::splitScope()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 2
eloc 7
nc 2
nop 1
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\Shareable;
16
17
use Phossa2\Shared\Message\Message;
18
use Phossa2\Shared\Exception\RuntimeException;
19
20
/**
21
 * Implementation of ShareableInterface
22
 *
23
 * @package Phossa2\Shared
24
 * @author  Hong Zhang <[email protected]>
25
 * @see     ShareableInterface
26
 * @version 2.0.10
27
 * @since   2.0.10 added
28
 */
29
trait ShareableTrait
30
{
31
    /**
32
     * Shareables' pool
33
     *
34
     * @var    ShareableInstance[]
35
     * @access protected
36
     * @staticvar
37
     */
38
    protected static $shareables = [];
39
40
    /**
41
     * Is this shared instance, store its $scope here
42
     *
43
     * @var    string
44
     * @access protected
45
     */
46
    protected $shared_in;
47
48
    /**
49
     * Scopes of this instance belongs to
50
     *
51
     * @var    string[]
52
     * @access protected
53
     */
54
    protected $scopes = [];
55
56
    /*
57
     * {@inheritDoc}
58
     */
59
    public static function getShareable(
60
        /*# string */ $scope = ''
61
    )/*# : ShareableInterface */ {
62
        if (!static::hasShareable($scope)) {
63
            (new static())->setShareable($scope);
64
        }
65
        return self::$shareables[get_called_class()][$scope];
66
    }
67
68
    /**
69
     * {@inheritDoc}
70
     */
71
    public function setShareable(/*# string */ $scope = '')
72
    {
73
        // this $scope has shareable already or $this is a shareable
74
        if (static::hasShareable($scope) || $this->isShareable() !== false) {
75
            throw new RuntimeException(
76
                Message::get(Message::MSG_SHAREABLE_FAIL, $scope),
77
                Message::MSG_SHAREABLE_FAIL
78
            );
79
        }
80
81
        $this->shared_in = $scope;
82
        self::$shareables[get_class($this)][$scope] = $this;
83
84
        return $this;
85
    }
86
87
    /**
88
     * {@inheritDoc}
89
     */
90
    public function isShareable()
91
    {
92
        return is_null($this->shared_in) ? false : $this->shared_in;
93
    }
94
95
    /**
96
     * {@inheritDoc}
97
     */
98
    public function addScope(/*# string */ $scope)
99
    {
100
        if ($this->isShareable() === false &&
101
            !in_array($scope, $this->scopes)
102
        ) {
103
            $this->scopes[] = $scope;
104
        }
105
        return $this;
106
    }
107
108
    /**
109
     * Checking with $scope explicitly
110
     *
111
     * {@inheritDoc}
112
     */
113
    public function hasScope(/*# string */ $scope = '')/*# : bool */
114
    {
115
        return in_array($scope, $this->getScopes());
116
    }
117
118
    /**
119
     * {@inheritDoc}
120
     */
121
    public function getShareables()/*# : array */
122
    {
123
        $result = [];
124
        foreach ($this->getScopes() as $scope) {
125
            $result[] = static::getShareable($scope);
126
        }
127
        return $result;
128
    }
129
130
    /**
131
     * Test existense of shareable in $scope
132
     *
133
     * @param  string $scope
134
     * @return bool
135
     * @access protected
136
     */
137
    protected static function hasShareable(
138
        /*# string */ $scope = ''
139
    )/*# : bool */ {
140
        return isset(self::$shareables[get_called_class()][$scope]);
141
    }
142
143
    /**
144
     * Get all unique scopes for $this
145
     *
146
     * @param  string|array $scopes
0 ignored issues
show
Bug introduced by
There is no parameter named $scopes. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
147
     * @return array
148
     * @access protected
149
     */
150
    protected function getScopes()/*# : array */
151
    {
152
        // alway add global scope ''
153
        if (!in_array('', $this->scopes)) {
154
            $this->addScope('');
155
        }
156
        return $this->scopes;
157
    }
158
}
159