1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of Zenify |
7
|
|
|
* Copyright (c) 2012 Tomas Votruba (http://tomasvotruba.cz) |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace ZenifyCodingStandard\Sniffs\ControlStructures; |
11
|
|
|
|
12
|
|
|
use PHP_CodeSniffer_File; |
13
|
|
|
use PHP_CodeSniffer_Sniff; |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Rules: |
18
|
|
|
* - Yoda condition should not be used; switch expression order |
19
|
|
|
*/ |
20
|
|
|
final class YodaConditionSniff implements PHP_CodeSniffer_Sniff |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
const NAME = 'ZenifyCodingStandard.ControlStructures.YodaCondition'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var int |
30
|
|
|
*/ |
31
|
|
|
private $position; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var PHP_CodeSniffer_File |
35
|
|
|
*/ |
36
|
|
|
private $file; |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return int[] |
41
|
|
|
*/ |
42
|
1 |
|
public function register() : array |
43
|
|
|
{ |
44
|
|
|
return [ |
45
|
1 |
|
T_IS_IDENTICAL, |
46
|
1 |
|
T_IS_NOT_IDENTICAL, |
47
|
1 |
|
T_IS_EQUAL, |
48
|
1 |
|
T_IS_NOT_EQUAL, |
49
|
1 |
|
T_GREATER_THAN, |
50
|
1 |
|
T_LESS_THAN, |
51
|
1 |
|
T_IS_GREATER_OR_EQUAL, |
52
|
1 |
|
T_IS_SMALLER_OR_EQUAL |
53
|
|
|
]; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param PHP_CodeSniffer_File $file |
59
|
|
|
* @param int $position |
60
|
|
|
*/ |
61
|
1 |
|
public function process(PHP_CodeSniffer_File $file, $position) |
62
|
|
|
{ |
63
|
1 |
|
$this->file = $file; |
64
|
1 |
|
$this->position = $position; |
65
|
|
|
|
66
|
1 |
|
$previousNonEmptyToken = $this->getPreviousNonEmptyToken(); |
67
|
|
|
|
68
|
1 |
|
if ( ! $previousNonEmptyToken) { |
69
|
|
|
return; |
70
|
|
|
} |
71
|
|
|
|
72
|
1 |
|
if ( ! $this->isExpressionToken($previousNonEmptyToken)) { |
|
|
|
|
73
|
1 |
|
return; |
74
|
|
|
} |
75
|
|
|
|
76
|
1 |
|
$file->addError('Yoda condition should not be used; switch expression order', $position); |
77
|
1 |
|
} |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return array|bool |
82
|
|
|
*/ |
83
|
1 |
|
private function getPreviousNonEmptyToken() |
84
|
|
|
{ |
85
|
1 |
|
$leftTokenPosition = $this->file->findPrevious(T_WHITESPACE, ($this->position - 1), NULL, TRUE); |
86
|
1 |
|
$tokens = $this->file->getTokens(); |
87
|
1 |
|
if ($leftTokenPosition) { |
88
|
1 |
|
return $tokens[$leftTokenPosition]; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return FALSE; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
|
95
|
1 |
|
private function isExpressionToken(array $token) : bool |
96
|
|
|
{ |
97
|
1 |
|
return in_array($token['code'], [T_MINUS, T_NULL, T_FALSE, T_TRUE, T_LNUMBER, T_CONSTANT_ENCAPSED_STRING]); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
} |
101
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.