Completed
Pull Request — master (#126)
by Ryan
04:29 queued 01:40
created

PHPCompatibility_Sniff   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 85
Duplicated Lines 30.59 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 6
Bugs 1 Features 0
Metric Value
c 6
b 1
f 0
dl 26
loc 85
rs 10
wmc 12
lcom 0
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
B getTestVersion() 0 36 6
A supportsAbove() 13 13 3
A supportsBelow() 13 13 3

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
 * PHPCompatibility_Sniff.
4
 *
5
 * PHP version 5.6
6
 *
7
 * @category  PHP
8
 * @package   PHPCompatibility
9
 * @author    Wim Godden <[email protected]>
10
 * @copyright 2014 Cu.be Solutions bvba
11
 */
12
13
/**
14
 * PHPCompatibility_Sniff.
15
 *
16
 * @category  PHP
17
 * @package   PHPCompatibility
18
 * @author    Wim Godden <[email protected]>
19
 * @version   1.1.0
20
 * @copyright 2014 Cu.be Solutions bvba
21
 */
22
abstract class PHPCompatibility_Sniff implements PHP_CodeSniffer_Sniff
23
{
24
25
/* The testVersion configuration variable may be in any of the following formats:
26
 * 1) Omitted/empty, in which case no version is specified.  This effectively
27
 *    disables all the checks provided by this standard.
28
 * 2) A single PHP version number, e.g. "5.4" in which case the standard checks that
29
 *    the code will run on that version of PHP (no deprecated features or newer
30
 *    features being used).
31
 * 3) A range, e.g. "5.0-5.5", in which case the standard checks the code will run
32
 *    on all PHP versions in that range, and that it doesn't use any features that
33
 *    were deprecated by the final version in the list, or which were not available
34
 *    for the first version in the list.
35
 * PHP version numbers should always be in Major.Minor format.  Both "5", "5.3.2"
36
 * would be treated as invalid, and ignored.
37
 * This standard doesn't support checking against PHP4, so the minimum version that
38
 * is recognised is "5.0".
39
 */
40
41
    private function getTestVersion()
42
    {
43
        /**
44
         * var $testVersion will hold an array containing min/max version of PHP
45
         *   that we are checking against (see above).  If only a single version
46
         *   number is specified, then this is used as both the min and max.
47
         */
48
        static $arrTestVersions;
49
50
        if (!isset($testVersion)) {
0 ignored issues
show
Bug introduced by
The variable $testVersion seems only to be defined at a later point. As such the call to isset() seems to always evaluate to false.

This check marks calls to isset(...) or empty(...) that are found before the variable itself is defined. These will always have the same result.

This is likely the result of code being shifted around. Consider removing these calls.

Loading history...
51
            $testVersion = PHP_CodeSniffer::getConfigData('testVersion');
52
            $testVersion = trim($testVersion);
53
54
            $arrTestVersions = array(null, null);
55
            if (preg_match('/^\d+\.\d+$/', $testVersion)) {
56
                $arrTestVersions = array($testVersion, $testVersion);
57
            }
58
            elseif (preg_match('/^(\d+\.\d+)\s*-\s*(\d+\.\d+)$/', $testVersion,
59
                               $matches))
60
            {
61
                if (version_compare($matches[1], $matches[2], ">")) {
62
                    trigger_error("Invalid range in testVersion setting: '"
63
                                  . $testVersion . "'", E_USER_WARNING);
64
                }
65
                else {
66
                    $arrTestVersions = array($matches[1], $matches[2]);
67
                }
68
            }
69
            elseif (!$testVersion == "") {
70
                trigger_error("Invalid testVersion setting: '" . $testVersion
71
                              . "'", E_USER_WARNING);
72
            }
73
        }
74
75
        return $arrTestVersions;
76
    }
77
78 View Code Duplication
    public function supportsAbove($phpVersion)
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...
79
    {
80
        $testVersion = $this->getTestVersion();
81
        $testVersion = $testVersion[1];
82
83
        if (is_null($testVersion)
84
            || version_compare($testVersion, $phpVersion) >= 0
85
        ) {
86
            return true;
87
        } else {
88
            return false;
89
        }
90
    }//end supportsAbove()
91
92 View Code Duplication
    public function supportsBelow($phpVersion)
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...
93
    {
94
        $testVersion = $this->getTestVersion();
95
        $testVersion = $testVersion[0];
96
97
        if (!is_null($testVersion)
98
            && version_compare($testVersion, $phpVersion) <= 0
99
        ) {
100
            return true;
101
        } else {
102
            return false;
103
        }
104
    }//end supportsBelow()
105
106
}//end class
107