|
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 MESSAGE_ERROR = 'Yoda condition should not be used; switch expression order'; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var int |
|
30
|
|
|
*/ |
|
31
|
|
|
private $position; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var PHP_CodeSniffer_File |
|
35
|
|
|
*/ |
|
36
|
|
|
private $file; |
|
37
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* {@inheritdoc} |
|
41
|
|
|
*/ |
|
42
|
1 |
|
public function register() |
|
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
|
|
|
* {@inheritdoc} |
|
59
|
|
|
*/ |
|
60
|
1 |
|
public function process(PHP_CodeSniffer_File $file, $position) |
|
61
|
|
|
{ |
|
62
|
1 |
|
$this->file = $file; |
|
63
|
1 |
|
$this->position = $position; |
|
64
|
|
|
|
|
65
|
1 |
|
$previousNonEmptyToken = $this->getPreviousNonEmptyToken(); |
|
66
|
|
|
|
|
67
|
1 |
|
if ($previousNonEmptyToken) { |
|
68
|
1 |
|
if ($this->isExpressionToken($previousNonEmptyToken)) { |
|
|
|
|
|
|
69
|
1 |
|
$file->addError(self::MESSAGE_ERROR, $position); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
1 |
|
} |
|
73
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @return array|bool |
|
77
|
|
|
*/ |
|
78
|
1 |
|
private function getPreviousNonEmptyToken() |
|
79
|
|
|
{ |
|
80
|
1 |
|
$leftTokenPosition = $this->file->findPrevious(T_WHITESPACE, ($this->position - 1), NULL, TRUE); |
|
81
|
1 |
|
$tokens = $this->file->getTokens(); |
|
82
|
1 |
|
if ($leftTokenPosition) { |
|
83
|
1 |
|
return $tokens[$leftTokenPosition]; |
|
84
|
|
|
} |
|
85
|
|
|
return FALSE; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
|
|
89
|
1 |
|
private function isExpressionToken(array $token) : bool |
|
90
|
|
|
{ |
|
91
|
1 |
|
if (in_array($token['code'], [T_MINUS, T_NULL, T_FALSE, T_TRUE, T_LNUMBER, T_CONSTANT_ENCAPSED_STRING])) { |
|
92
|
1 |
|
return TRUE; |
|
93
|
|
|
} |
|
94
|
1 |
|
return FALSE; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
} |
|
98
|
|
|
|
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.