Completed
Pull Request — master (#29)
by Tim
13:43
created

ModuleVarsArrayTrait::clearModuleVars()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 6
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 6
loc 6
ccs 0
cts 4
cp 0
rs 9.4285
cc 2
eloc 3
nc 2
nop 0
crap 6
1
<?php
2
3
/**
4
 * \AppserverIo\Server\Traits\ModuleVarsArrayTrait
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Johann Zelger <[email protected]>
15
 * @copyright 2015 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/appserver-io/server
18
 * @link      http://www.appserver.io
19
 */
20
21
namespace AppserverIo\Server\Traits;
22
23
use AppserverIo\Server\Exceptions\ServerException;
24
25
/**
26
 * Trait ModuleVarsArrayTrait
27
 *
28
 * @author    Johann Zelger <[email protected]>
29
 * @copyright 2015 TechDivision GmbH <[email protected]>
30
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
31
 * @link      https://github.com/appserver-io/server
32
 * @link      http://www.appserver.io
33
 */
34 View Code Duplication
trait ModuleVarsArrayTrait
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
35
{
36
37
    /**
38
     * Sets a value to specific module var
39
     *
40
     * @param string $moduleVar The module var to set
41
     * @param string $value     The value to module var
42
     *
43
     * @return void
44
     */
45
    public function setModuleVar($moduleVar, $value)
46
    {
47
        if (!is_null($value)) {
48
            $this->moduleVars[$moduleVar] = $value;
0 ignored issues
show
Bug introduced by
The property moduleVars does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
49
        }
50
    }
51
52
    /**
53
     * Unsets a specific module var
54
     *
55
     * @param string $moduleVar The module var to unset
56
     *
57
     * @return void
58
     */
59
    public function unsetModuleVar($moduleVar)
60
    {
61
        if (isset($this->moduleVars[$moduleVar])) {
62
            unset($this->moduleVars[$moduleVar]);
63
        }
64
    }
65
66
    /**
67
     * Returns a value for specific module var
68
     *
69
     * @param string $moduleVar The module var to get value for
70
     *
71
     * @throws \AppserverIo\Server\Exceptions\ServerException
72
     *
73
     * @return mixed The value to given module var
74
     */
75
    public function getModuleVar($moduleVar)
76
    {
77
        // check if var is set
78
        if (isset($this->moduleVars[$moduleVar])) {
79
            // return vars value
80
            return $this->moduleVars[$moduleVar];
81
        }
82
        // throw exception
83
        throw new ServerException("Module var '$moduleVar'' does not exist.", 500);
84
    }
85
86
87
    /**
88
     * Returns all the module vars as array key value pair format
89
     *
90
     * @return array The module vars as array
91
     */
92
    public function getModuleVars()
93
    {
94
        return $this->moduleVars;
95
    }
96
97
    /**
98
     * Checks if value exists for given module var
99
     *
100
     * @param string $moduleVar The module var to check
101
     *
102
     * @return boolean Weather it has moduleVar (true) or not (false)
103
     */
104
    public function hasModuleVar($moduleVar)
105
    {
106
        // check if var is set
107
        if (!isset($this->moduleVars[$moduleVar])) {
108
            return false;
109
        }
110
111
        return true;
112
    }
113
114
    /**
115
     * Clears the module vars storage
116
     *
117
     * @return void
118
     */
119
    public function clearModuleVars()
120
    {
121
        foreach ($this->moduleVars as $key => $value) {
122
            unset($this->moduleVars[$key]);
123
        }
124
    }
125
}
126