Completed
Pull Request — master (#235)
by
unknown
02:57
created

PHPCompatibility_Install::enable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
1
<?php 
2
3
/**
4
 * Install functions
5
 * 
6
 * Used through vandor/bin/phpcompat_(en|dis)able
7
 *
8
 */
9
10
class PHPCompatibility_Install {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
11
    static function enable() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
12
        echo "Enabling PHPCompatibility\n";
13
        self::make_copy();
14
        self::register_in_cs();
15
    }
16
    
17
    static function disable() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
18
        echo "Disabling PHPCompatibility\n";
19
        self::remove_copy();
20
        self::unregister_from_cs();
21
    }
22
    
23 View Code Duplication
    static function register_in_cs() { 
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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...
24
        $installed_paths = self::get_installed_path();
25
        if (in_array(__DIR__, $installed_paths)) {
26
            echo "Our path is already registered in PHP CodeSniffer\n";
27
        } else {
28
            array_push($installed_paths, __DIR__);
29
            self::set_installed_path($installed_paths);
30
            echo "Registered our path in PHP CodeSniffer\n";
31
        }
32
    }
33
    
34 View Code Duplication
    static function unregister_from_cs() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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...
35
        $installed_paths = self::get_installed_path();
36
        if (! in_array(__DIR__, $installed_paths)) {
37
            echo "Our path is not registered in PHP CodeSniffer\n";
38
        } else {
39
            $installed_paths = array_filter($installed_paths, function ($v) {
40
                return $v != __DIR__;
41
            });
42
            self::set_installed_path($installed_paths);
43
            echo "Unregistered our path in PHP CodeSniffer\n";
44
        }
45
    }
46
47
    static function make_copy() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
48
        $srcDir = __DIR__;
49
        $copy = __DIR__ .DIRECTORY_SEPARATOR.'PHPCompatibility';
50
        
51
        if ( file_exists ($copy)) {
52
            echo "Copy workaround is already in place\n";
53
            return;
54
        }
55
        
56
        mkdir($copy);
57
        copy(__DIR__ .DIRECTORY_SEPARATOR.'ruleset.xml', $copy.DIRECTORY_SEPARATOR.'ruleset.xml');
58
        copy(__DIR__ .DIRECTORY_SEPARATOR.'Sniff.php', $copy.DIRECTORY_SEPARATOR.'Sniff.php');
59
        
60
        if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
61
            $copy = str_replace('/', DIRECTORY_SEPARATOR, $copy);
62
            $srcDir = str_replace('/', DIRECTORY_SEPARATOR, $srcDir);
63
            passthru('xcopy "'.$srcDir .DIRECTORY_SEPARATOR.'Sniffs" "'.$copy.DIRECTORY_SEPARATOR.'Sniffs" /S /E /I');
64
        } else {
65
            passthru('cp -r "'.$srcDir .DIRECTORY_SEPARATOR.'Sniffs" "'.$copy.DIRECTORY_SEPARATOR.'Sniffs"');
66
        }
67
        echo "Created copy workaround\n";
68
    }    
69
    
70
    static function remove_copy() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
71
        $copy = __DIR__ .DIRECTORY_SEPARATOR.'PHPCompatibility';
72
        
73
        if ( ! file_exists ($copy)) {
74
            echo "No copy workaround to remove\n";
75
            return;
76
        }
77
        
78
        if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
79
            $copy = str_replace('/', DIRECTORY_SEPARATOR, $copy);
80
            passthru('rmdir /S /Q "'.$copy.'"');
81
        } else {
82
            passthru('rm -rf "'.$copy.'"');
83
        }
84
        echo "Copy workaround removed\n";
85
    }
86
87
    static function get_installed_path() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
88
        $installed_paths = PHP_CodeSniffer::getConfigData('installed_paths');
89
        if ( $installed_paths === NULL or strlen($installed_paths) == 0 ) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
90
            // Because: explode(',' , NULL) == array('')
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
91
            // and we assert no data is empty array
92
            return array();
93
        }
94
        return explode(',', $installed_paths);
95
    }
96
97
    static function set_installed_path($array) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
98
        if(count($array) == 0) {
99
            PHP_CodeSniffer::setConfigData('installed_paths', NULL);
100
        } else {
101
            PHP_CodeSniffer::setConfigData('installed_paths', implode(',', $array));
102
        }
103
    }
104
}
105