Completed
Pull Request — master (#117)
by Simone
02:36
created

RuleTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 84
Duplicated Lines 35.71 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 2
dl 30
loc 84
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testIvalidInitializationThrowAndException() 0 4 1
A test() 0 15 1
A testInvalidTypeIsNotAllowed() 0 8 1
A testCheckFunctionReturnRuleValidityAsBoolean() 0 11 1
A testIs() 15 15 1
A testIsNot() 15 15 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
 * This file is part of sensorario/resources repository
5
 *
6
 * (c) Simone Gentili <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sensorario\Resources\Test\Resources;
13
14
use Sensorario\Resources\Rulers\Rule;
15
16
class RuleTest extends \PHPUnit_Framework_TestCase
17
{
18
    /**
19
     * @expectedException \LogicException
20
     * @expectedExceptionMessage rule type is not defined
21
     */
22
    public function testIvalidInitializationThrowAndException()
23
    {
24
        $rule = Rule::fromArray([]);
0 ignored issues
show
Unused Code introduced by
$rule is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
25
    }
26
27
    public function test()
28
    {
29
        $rule = Rule::fromArray(
30
            $ruleConfiguration = [
31
                Rule::TYPE_OBJECT => [
32
                    '\DateTime',
33
                ]
34
            ]
35
        );
36
37
        $this->assertEquals(
38
            $ruleConfiguration,
39
            $rule->asArray()
40
        );
41
    }
42
43
    /**
44
     * @expectedException \Sensorario\Resources\Exceptions\InvalidTypeException
45
     * @expectedExceptionMessageRegex #Oops! Invalid configuration!!!Type `foo` is not valid.#
46
     */
47
    public function testInvalidTypeIsNotAllowed()
48
    {
49
        $rule = Rule::fromArray([
50
            'foo' => [ ]
51
        ]);
52
53
        $rule->ensureRuleNameIsValid();
54
    }
55
56
    public function testCheckFunctionReturnRuleValidityAsBoolean()
57
    {
58
        $rule = Rule::fromArray([
59
            'foo' => [ ]
60
        ]);
61
62
        $this->assertSame(
63
            false,
64
            $rule->isValid()
65
        );
66
    }
67
68 View Code Duplication
    public function testIs()
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...
69
    {
70
        $rule = Rule::fromArray(
71
            $ruleConfiguration = [
72
                Rule::TYPE_OBJECT => [
73
                    '\DateTime',
74
                ]
75
            ]
76
        );
77
78
        $this->assertSame(
79
            true,
80
            $rule->is(Rule::TYPE_OBJECT)
81
        );
82
    }
83
84 View Code Duplication
    public function testIsNot()
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...
85
    {
86
        $rule = Rule::fromArray(
87
            $ruleConfiguration = [
88
                Rule::TYPE_OBJECT => [
89
                    '\DateTime',
90
                ]
91
            ]
92
        );
93
94
        $this->assertSame(
95
            false,
96
            $rule->isNot(Rule::TYPE_OBJECT)
97
        );
98
    }
99
}
100