1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class PHPCompatibility_Install { |
|
|
|
|
4
|
|
|
static function enable() { |
|
|
|
|
5
|
|
|
echo "Enabling PHPCompatibility\n"; |
6
|
|
|
self::make_copy(); |
7
|
|
|
self::register_in_cs(); |
8
|
|
|
} |
9
|
|
|
|
10
|
|
|
static function disable() { |
|
|
|
|
11
|
|
|
echo "Disabling PHPCompatibility\n"; |
12
|
|
|
self::remove_copy(); |
13
|
|
|
self::unregister_from_cs(); |
14
|
|
|
} |
15
|
|
|
|
16
|
|
View Code Duplication |
static function register_in_cs() { |
|
|
|
|
17
|
|
|
$installed_paths = self::get_installed_path(); |
18
|
|
|
if (in_array(__DIR__, $installed_paths)) { |
19
|
|
|
echo "Our path is already registered in PHP CodeSniffer\n"; |
20
|
|
|
} else { |
21
|
|
|
array_push($installed_paths, __DIR__); |
22
|
|
|
self::set_installed_path($installed_paths); |
23
|
|
|
echo "Registered our path in PHP CodeSniffer\n"; |
24
|
|
|
} |
25
|
|
|
} |
26
|
|
|
|
27
|
|
View Code Duplication |
static function unregister_from_cs() { |
|
|
|
|
28
|
|
|
$installed_paths = self::get_installed_path(); |
29
|
|
|
if (! in_array(__DIR__, $installed_paths)) { |
30
|
|
|
echo "Our path is not registered in PHP CodeSniffer\n"; |
31
|
|
|
} else { |
32
|
|
|
$installed_paths = array_filter($installed_paths, function ($v) { |
33
|
|
|
return $v != __DIR__; |
34
|
|
|
}); |
35
|
|
|
self::set_installed_path($installed_paths); |
36
|
|
|
echo "Unregistered our path in PHP CodeSniffer\n"; |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
static function make_copy() { |
|
|
|
|
41
|
|
|
$srcDir = __DIR__; |
42
|
|
|
$copy = __DIR__ .DIRECTORY_SEPARATOR.'PHPCompatibility'; |
43
|
|
|
|
44
|
|
|
if ( file_exists ($copy)) { |
45
|
|
|
echo "Copy hack is already in place\n"; |
46
|
|
|
return; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
mkdir($copy); |
50
|
|
|
copy(__DIR__ .DIRECTORY_SEPARATOR.'ruleset.xml', $copy.DIRECTORY_SEPARATOR.'ruleset.xml'); |
51
|
|
|
copy(__DIR__ .DIRECTORY_SEPARATOR.'Sniff.php', $copy.DIRECTORY_SEPARATOR.'Sniff.php'); |
52
|
|
|
|
53
|
|
|
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { |
54
|
|
|
$copy = str_replace('/', DIRECTORY_SEPARATOR, $copy); |
55
|
|
|
$srcDir = str_replace('/', DIRECTORY_SEPARATOR, $srcDir); |
56
|
|
|
passthru('xcopy "'.$srcDir .DIRECTORY_SEPARATOR.'Sniffs" "'.$copy.DIRECTORY_SEPARATOR.'Sniffs" /S /E /I'); |
57
|
|
|
} else { |
58
|
|
|
passthru('cp -r "'.$srcDir .DIRECTORY_SEPARATOR.'Sniffs" "'.$copy.DIRECTORY_SEPARATOR.'Sniffs"'); |
59
|
|
|
} |
60
|
|
|
echo "Created copy hack\n"; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
static function remove_copy() { |
|
|
|
|
64
|
|
|
$copy = __DIR__ .DIRECTORY_SEPARATOR.'PHPCompatibility'; |
65
|
|
|
|
66
|
|
|
if ( ! file_exists ($copy)) { |
67
|
|
|
echo "No copy hack to remove\n"; |
68
|
|
|
return; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { |
72
|
|
|
$copy = str_replace('/', DIRECTORY_SEPARATOR, $copy); |
73
|
|
|
passthru('rmdir /S /Q "'.$copy.'"'); |
74
|
|
|
} else { |
75
|
|
|
passthru('rm -rf "'.$copy.'"'); |
76
|
|
|
} |
77
|
|
|
echo "Copy hack removed\n"; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
static function get_installed_path() { |
|
|
|
|
81
|
|
|
$installed_paths = PHP_CodeSniffer::getConfigData('installed_paths'); |
82
|
|
|
if ( $installed_paths === NULL or strlen($installed_paths) == 0 ) { |
|
|
|
|
83
|
|
|
// Because: explode(',' , NULL) == array('') |
|
|
|
|
84
|
|
|
// and we assert no data is empty array |
85
|
|
|
return array(); |
86
|
|
|
} |
87
|
|
|
return explode(',', $installed_paths); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
static function set_installed_path($array) { |
|
|
|
|
91
|
|
|
if(count($array) == 0) { |
92
|
|
|
PHP_CodeSniffer::setConfigData('installed_paths', NULL); |
93
|
|
|
} else { |
94
|
|
|
PHP_CodeSniffer::setConfigData('installed_paths', implode(',', $array)); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.