Completed
Pull Request — master (#2)
by Mathieu
05:06 queued 01:16
created

DBObjectProtected::getProtectedVariable()   A

Complexity

Conditions 6
Paths 7

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 6.0493

Importance

Changes 0
Metric Value
cc 6
eloc 8
nc 7
nop 1
dl 0
loc 18
rs 9.2222
c 0
b 0
f 0
ccs 8
cts 9
cp 0.8889
crap 6.0493
1
<?php declare(strict_types=1);
2
namespace Suricate\Traits;
3
4
use Suricate\DBObject;
5
6
trait DBObjectProtected
7
{
8
    protected $protectedVariables           = [];
9
    protected $protectedValues              = [];
10
    protected $loadedProtectedVariables     = [];
11
12
    /**
13
     * @param string $name
14
     */
15 1
    private function getProtectedVariable(string $name)
16
    {
17
        // Variable exists, and is already loaded
18 1
        if (isset($this->protectedValues[$name]) && $this->isProtectedVariableLoaded($name)) {
19
            return $this->protectedValues[$name];
20
        }
21
        // Variable has not been loaded
22 1
        if (!$this->isProtectedVariableLoaded($name)) {
23 1
            if ($this->accessToProtectedVariable($name)) {
24 1
                $this->markProtectedVariableAsLoaded($name);
25
            }
26
        }
27
28 1
        if (isset($this->protectedValues[$name])) {
29 1
            return $this->protectedValues[$name];
30
        }
31
32 1
        return null;
33
    }
34
    /**
35
     * Mark a protected variable as loaded
36
     * @param  string $name varialbe name
37
     *
38
     * @return DBObject
39
     */
40 1
    public function markProtectedVariableAsLoaded(string $name)
41
    {
42 1
        if ($this->isProtectedVariable($name)) {
43 1
            $this->loadedProtectedVariables[$name] = true;
44
        }
45
46 1
        return $this;
47
    }
48
49
    /**
50
    * Check if variable is a protected variable
51
    * @param  string  $name variable name
52
    * @return boolean
53
    */
54 8
    public function isProtectedVariable(string $name): bool
55
    {
56 8
        return in_array($name, $this->protectedVariables);
57
    }
58
59
    /**
60
     * Check if a protected variable already have been loaded
61
     * @param  string  $name Variable name
62
     * @return boolean
63
     */
64 1
    protected function isProtectedVariableLoaded(string $name): bool
65
    {
66 1
        return isset($this->loadedProtectedVariables[$name]);
67
    }
68
69
    protected function accessToProtectedVariable(string $name): bool
0 ignored issues
show
Unused Code introduced by
The parameter $name is not used and could be removed. ( Ignorable by Annotation )

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

69
    protected function accessToProtectedVariable(/** @scrutinizer ignore-unused */ string $name): bool

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
70
    {
71
        return false;
72
    }
73
}