Completed
Pull Request — master (#230)
by Juliette
03:24
created

TernaryOperatorsSniffTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 55
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 1
dl 55
loc 55
rs 10
c 0
b 0
f 0

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
 * Ternary Operators Sniff test file
4
 *
5
 * @package PHPCompatibility
6
 */
7
8
9
/**
10
 * Ternary Operators Sniff tests
11
 *
12
 * @uses BaseSniffTest
13
 * @package PHPCompatibility
14
 * @author Jansen Price <[email protected]>
15
 */
16
class TernaryOperatorsSniffTest extends BaseSniffTest
17
{
18
    /**
19
     * Sniffed file
20
     *
21
     * @var PHP_CodeSniffer_File
22
     */
23
    protected $_sniffFile;
24
25
    /**
26
     * Test ternary operators that are acceptable in all PHP versions.
27
     *
28
     * @group ternaryOperators
29
     *
30
     * @return void
31
     */
32
    public function testStandardTernaryOperators()
33
    {
34
        $this->_sniffFile = $this->sniffFile('sniff-examples/ternary_operator.php');
35
        $this->assertNoViolation($this->_sniffFile, 5);
36
    }
37
38
    /**
39
     * 5.2 doesn't support elvis operator.
40
     *
41
     * @group ternaryOperators
42
     *
43
     * @return void
44
     */
45
    public function testMissingMiddleExpression5dot2()
46
    {
47
        $this->_sniffFile = $this->sniffFile('sniff-examples/ternary_operator.php', '5.2-5.4');
48
        $this->assertWarning($this->_sniffFile, 8,
49
                "Middle may not be omitted from ternary operators in PHP < 5.3");
50
        $this->assertWarning($this->_sniffFile, 10,
51
                "Middle may not be omitted from ternary operators in PHP < 5.3");
52
    }
53
54
    /**
55
     * 5.3 does support elvis operator.
56
     *
57
     * @group ternaryOperators
58
     *
59
     * @return void
60
     */
61
    public function testMissingMiddleExpression5dot3()
62
    {
63
        $this->_sniffFile = $this->sniffFile('sniff-examples/ternary_operator.php', '5.3');
64
        $this->assertNoViolation($this->_sniffFile, 8,
65
                "Middle may not be omitted from ternary operators in PHP < 5.3");
66
        $this->assertNoViolation($this->_sniffFile, 10,
67
                "Middle may not be omitted from ternary operators in PHP < 5.3");
68
    }
69
70
}
71