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

ModuleVarsObjectTrait   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 77
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
c 2
b 0
f 0
lcom 1
cbo 0
dl 77
loc 77
ccs 0
cts 24
cp 0
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setModuleVar() 4 4 1
A unsetModuleVar() 4 4 1
A getModuleVar() 5 5 1
A getModuleVars() 4 4 1
A hasModuleVar() 5 5 1
A clearModuleVars() 4 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * \AppserverIo\Server\Traits\ModuleVarsObjectTrait
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
/**
24
 * Trait ModuleVarsObjectTrait
25
 *
26
 * @author    Johann Zelger <[email protected]>
27
 * @copyright 2015 TechDivision GmbH <[email protected]>
28
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
29
 * @link      https://github.com/appserver-io/server
30
 * @link      http://www.appserver.io
31
 */
32 View Code Duplication
trait ModuleVarsObjectTrait
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...
33
{
34
35
    /**
36
     * Sets a value to specific module var
37
     *
38
     * @param string $moduleVar The module var to set
39
     * @param string $value     The value to module var
40
     *
41
     * @return void
42
     */
43
    public function setModuleVar($moduleVar, $value)
44
    {
45
        $this->moduleVars->add($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...
46
    }
47
48
    /**
49
     * Unsets a specific module var
50
     *
51
     * @param string $moduleVar The module var to unset
52
     *
53
     * @return void
54
     */
55
    public function unsetModuleVar($moduleVar)
56
    {
57
        $this->moduleVars->remove($moduleVar);
58
    }
59
60
    /**
61
     * Returns a value for specific module var
62
     *
63
     * @param string $moduleVar The module var to get value for
64
     *
65
     * @throws \AppserverIo\Server\Exceptions\ServerException
66
     *
67
     * @return mixed The value to given module var
68
     */
69
    public function getModuleVar($moduleVar)
70
    {
71
        // get from hash map
72
        return $this->moduleVars->get($moduleVar);
73
    }
74
75
76
    /**
77
     * Returns all the module vars as array key value pair format
78
     *
79
     * @return array The module vars as array
80
     */
81
    public function getModuleVars()
82
    {
83
        return $this->moduleVars->toIndexedArray();
84
    }
85
86
    /**
87
     * Checks if value exists for given module var
88
     *
89
     * @param string $moduleVar The module var to check
90
     *
91
     * @return boolean Weather it has moduleVar (true) or not (false)
92
     */
93
    public function hasModuleVar($moduleVar)
94
    {
95
        // check if var is set
96
        return $this->moduleVars->exists($moduleVar);
97
    }
98
99
    /**
100
     * Clears the module vars storage
101
     *
102
     * @return void
103
     */
104
    public function clearModuleVars()
105
    {
106
        $this->moduleVars->clear();
107
    }
108
}
109