1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace BestIt\Sniffs; |
6
|
|
|
|
7
|
|
|
use BestIt\CodeSniffer\File; |
8
|
|
|
use SlevomatCodingStandard\Helpers\SuppressHelper; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Helps you check if a sniff is suppressed. |
12
|
|
|
* |
13
|
|
|
* @author blange <[email protected]> |
14
|
|
|
* @package BestIt\Sniffs |
15
|
|
|
*/ |
16
|
|
|
trait SuppressingTrait |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* The used suppresshelper. |
20
|
|
|
* |
21
|
|
|
* @var SuppressHelper |
22
|
|
|
*/ |
23
|
|
|
private $suppressHelper = null; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Type-safe getter for the file. |
27
|
|
|
* |
28
|
|
|
* @return File |
29
|
|
|
*/ |
30
|
|
|
abstract protected function getFile(): File; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Get the sniff name. |
34
|
|
|
* |
35
|
|
|
* @param string|null $sniffName If there is an optional sniff name. |
36
|
|
|
* |
37
|
|
|
* @return string Returns the special sniff name in the code sniffer context. |
38
|
|
|
*/ |
39
|
|
|
private function getSniffName(?string $sniffName = null): string |
40
|
|
|
{ |
41
|
|
|
$sniffFQCN = preg_replace( |
42
|
|
|
'/Sniff$/', |
43
|
|
|
'', |
44
|
|
|
str_replace(['\\', '.Sniffs'], ['.', ''], static::class) |
45
|
|
|
); |
46
|
|
|
|
47
|
|
|
if ($sniffName) { |
|
|
|
|
48
|
|
|
$sniffFQCN .= '.' . $sniffName; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $sniffFQCN; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Type-safe getter for the stack position. |
56
|
|
|
* |
57
|
|
|
* @return int |
58
|
|
|
*/ |
59
|
|
|
abstract protected function getStackPos(): int; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Returns the used suppress helper. |
63
|
|
|
* |
64
|
|
|
* @return SuppressHelper The suppress helper. |
65
|
|
|
*/ |
66
|
|
|
private function getSuppressHelper(): SuppressHelper |
67
|
|
|
{ |
68
|
|
|
if (!$this->suppressHelper) { |
69
|
|
|
$this->suppressHelper = new SuppressHelper(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $this->suppressHelper; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Returns true if this sniff or a rule of this sniff is suppressed with the slevomat suppress annotation. |
77
|
|
|
* |
78
|
|
|
* @param null|string $rule The optional rule. |
79
|
|
|
* |
80
|
|
|
* @return bool Returns true if the sniff is suppressed. |
81
|
|
|
*/ |
82
|
|
|
protected function isSniffSuppressed(?string $rule = null): bool |
83
|
|
|
{ |
84
|
|
|
return $this->getSuppressHelper()->isSniffSuppressed( |
85
|
|
|
$this->getFile(), |
86
|
|
|
$this->getStackPos(), |
87
|
|
|
$this->getSniffName($rule) |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: