1 | <?php |
||
16 | trait SuppressingTrait |
||
17 | { |
||
18 | /** |
||
19 | * The used suppresshelper. |
||
20 | * |
||
21 | * @var SuppressHelper|null |
||
22 | */ |
||
23 | private ?SuppressHelper $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 | $sniffClassName = preg_replace( |
||
42 | '/Sniff$/', |
||
43 | '', |
||
44 | str_replace(['\\', '.Sniffs'], ['.', ''], static::class) |
||
45 | ); |
||
46 | |||
47 | if ($sniffName) { |
||
48 | $sniffClassName .= '.' . $sniffName; |
||
49 | } |
||
50 | |||
51 | return $sniffClassName; |
||
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 | * @param int|null $stackPos Do you want ot overload the position for the which position the sniff is suppressed. |
||
80 | * |
||
81 | * @return bool Returns true if the sniff is suppressed. |
||
82 | */ |
||
83 | protected function isSniffSuppressed(?string $rule = null, ?int $stackPos = null): bool |
||
84 | { |
||
85 | return $this->getSuppressHelper()->isSniffSuppressed( |
||
86 | $this->getFile(), |
||
87 | $stackPos ?? $this->getStackPos(), |
||
88 | $this->getSniffName($rule) |
||
89 | ); |
||
90 | } |
||
91 | } |
||
92 |