These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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
|
||||||||||||
11 | static function enable() { |
|||||||||||
0 ignored issues
–
show
|
||||||||||||
12 | echo "Enabling PHPCompatibility\n"; |
|||||||||||
13 | self::make_copy(); |
|||||||||||
14 | self::register_in_cs(); |
|||||||||||
15 | } |
|||||||||||
16 | ||||||||||||
17 | static function disable() { |
|||||||||||
0 ignored issues
–
show
|
||||||||||||
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
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. ![]() |
||||||||||||
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
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. ![]() |
||||||||||||
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
|
||||||||||||
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
|
||||||||||||
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
|
||||||||||||
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):
The difference between these is the order in which they are executed. In most cases,
you would want to use a boolean operator like 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-FlowOne 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 // 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. ![]() |
||||||||||||
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. ![]() |
||||||||||||
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
|
||||||||||||
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 |
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.