1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Zenify |
5
|
|
|
* Copyright (c) 2012 Tomas Votruba (http://tomasvotruba.cz) |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace ZenifyCodingStandard\Sniffs\ControlStructures; |
9
|
|
|
|
10
|
|
|
use PHP_CodeSniffer_File; |
11
|
|
|
use PHP_CodeSniffer_Sniff; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Rules: |
16
|
|
|
* - Yoda condition should not be used; switch expression order |
17
|
|
|
*/ |
18
|
|
|
final class YodaConditionSniff implements PHP_CodeSniffer_Sniff |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
const MESSAGE_ERROR = 'Yoda condition should not be used; switch expression order'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var int |
28
|
|
|
*/ |
29
|
|
|
private $position; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var PHP_CodeSniffer_File |
33
|
|
|
*/ |
34
|
|
|
private $file; |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritdoc} |
39
|
|
|
*/ |
40
|
1 |
|
public function register() |
41
|
|
|
{ |
42
|
|
|
return [ |
43
|
1 |
|
T_IS_IDENTICAL, |
44
|
1 |
|
T_IS_NOT_IDENTICAL, |
45
|
1 |
|
T_IS_EQUAL, |
46
|
1 |
|
T_IS_NOT_EQUAL, |
47
|
1 |
|
T_GREATER_THAN, |
48
|
1 |
|
T_LESS_THAN, |
49
|
1 |
|
T_IS_GREATER_OR_EQUAL, |
50
|
1 |
|
T_IS_SMALLER_OR_EQUAL |
51
|
|
|
]; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
1 |
|
public function process(PHP_CodeSniffer_File $file, $position) |
59
|
|
|
{ |
60
|
1 |
|
$this->file = $file; |
61
|
1 |
|
$this->position = $position; |
62
|
|
|
|
63
|
1 |
|
$previousNonEmptyToken = $this->getPreviousNonEmptyToken(); |
64
|
|
|
|
65
|
1 |
|
if ($previousNonEmptyToken) { |
66
|
1 |
|
if ($this->isExpressionToken($previousNonEmptyToken)) { |
|
|
|
|
67
|
1 |
|
$file->addError(self::MESSAGE_ERROR, $position); |
68
|
|
|
} |
69
|
|
|
} |
70
|
1 |
|
} |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return array|bool |
75
|
|
|
*/ |
76
|
1 |
|
private function getPreviousNonEmptyToken() |
77
|
|
|
{ |
78
|
1 |
|
$leftTokenPosition = $this->file->findPrevious(T_WHITESPACE, ($this->position - 1), NULL, TRUE); |
79
|
1 |
|
$tokens = $this->file->getTokens(); |
80
|
1 |
|
if ($leftTokenPosition) { |
81
|
1 |
|
return $tokens[$leftTokenPosition]; |
82
|
|
|
} |
83
|
|
|
return FALSE; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return bool |
89
|
|
|
*/ |
90
|
1 |
|
private function isExpressionToken(array $token) |
91
|
|
|
{ |
92
|
1 |
|
if (in_array($token['code'], [T_MINUS, T_NULL, T_FALSE, T_TRUE, T_LNUMBER, T_CONSTANT_ENCAPSED_STRING])) { |
93
|
1 |
|
return TRUE; |
94
|
|
|
} |
95
|
1 |
|
return FALSE; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
} |
99
|
|
|
|
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.