ModuleLoaderTest   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 81
Duplicated Lines 14.81 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A tearDown() 0 3 1
A testModuleLoaderGetModules() 0 5 1
A testModuleLoaderAdd() 6 6 1
A testModuleLoaderAddWithException() 0 5 1
A testModuleLoaderGetModule() 6 6 1
A testModuleLoaderRemove() 0 5 1
A testModuleLoaderGetModulesInfo() 0 9 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
 * The MIT License
5
 *
6
 * Copyright 2016 Alejandro Peña Florentín ([email protected]).
7
 *
8
 * Permission is hereby granted, free of charge, to any person obtaining a copy
9
 * of this software and associated documentation files (the "Software"), to deal
10
 * in the Software without restriction, including without limitation the rights
11
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
 * copies of the Software, and to permit persons to whom the Software is
13
 * furnished to do so, subject to the following conditions:
14
 *
15
 * The above copyright notice and this permission notice shall be included in
16
 * all copies or substantial portions of the Software.
17
 *
18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24
 * THE SOFTWARE.
25
 */
26
27
namespace Tight\Tests;
28
29
/**
30
 * Description of ModuleLoaderTest
31
 *
32
 * @author Alejandro Peña Florentín ([email protected])
33
 */
34
class ModuleLoaderTest extends \PHPUnit_Framework_TestCase
35
{
36
37
    private $loader;
38
    private $testModule;
39
40
    public function setUp() {
41
42
        $this->loader = new \Tight\Modules\ModuleLoader(new \Tight\Tight);
43
        $this->testModule = new TestLoaderModule("TestModule");
44
    }
45
46
    public function tearDown() {
47
        
48
    }
49
50
    /**
51
     * @test
52
     */
53
    public function testModuleLoaderGetModules() {
54
        $expected = [];
55
        $this->assertEquals($expected, $this->loader->getModules());
56
        $this->assertNull($this->loader->getModule("undefinedModule"));
57
    }
58
59
    /**
60
     * @test
61
     * @depends testModuleLoaderGetModules
62
     */
63 View Code Duplication
    public function testModuleLoaderAdd() {
0 ignored issues
show
Duplication introduced by
This method 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...
64
        $key = $this->testModule->getModuleName();
65
        $expected = [$key => $this->testModule];
66
        $this->loader->add($this->testModule);
67
        $this->assertEquals($expected, $this->loader->getModules());
68
    }
69
70
    /**
71
     * @test
72
     * @depends testModuleLoaderAdd
73
     * @expectedException \Tight\Exception\ModuleException
74
     */
75
    public function testModuleLoaderAddWithException() {
76
        $this->loader->add($this->testModule);
77
        $this->setExpectedException("\Tight\Exception\ModuleException");
78
        $this->loader->add($this->testModule);
79
    }
80
81
    /**
82
     * @test
83
     * @depends testModuleLoaderAdd
84
     */
85 View Code Duplication
    public function testModuleLoaderGetModule() {
0 ignored issues
show
Duplication introduced by
This method 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...
86
        $this->loader->add($this->testModule);
87
        $modName = $this->testModule->getModuleName();
88
        $expected = $this->testModule;
89
        $this->assertEquals($expected, $this->loader->getModule($modName));
90
    }
91
92
    /**
93
     * @test
94
     * @depends testModuleLoaderAdd
95
     */
96
    public function testModuleLoaderRemove() {
97
        $this->loader->add($this->testModule);
98
        $this->assertTrue($this->loader->remove($this->testModule->getModuleName()));
99
        $this->assertFalse($this->loader->remove($this->testModule->getModuleName()));
100
    }
101
    /**
102
     * @test
103
     * @depends testModuleLoaderAdd
104
     */
105
    public function testModuleLoaderGetModulesInfo(){
106
        $expected[0] = [
0 ignored issues
show
Coding Style Comprehensibility introduced by
$expected was never initialized. Although not strictly required by PHP, it is generally a good practice to add $expected = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
107
            "name" => "TestModule",
108
            "version"=> "v1.0"
109
        ];
110
        $this->assertEquals([],$this->loader->getModulesInfo());
111
        $this->loader->add($this->testModule);
112
        $this->assertEquals($expected,$this->loader->getModulesInfo());
113
    }
114
}
115
116
class TestLoaderModule extends \Tight\Modules\AbstractModule
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
117
{
118
119
    public function onConfigChange() {
120
        
121
    }
122
123
    public function onLoad() {
124
        
125
    }
126
127
    public function onRemove() {
128
        
129
    }
130
131
}
132