Completed
Push — master ( 14c836...906b18 )
by Björn
06:09 queued 03:52
created

SuppressingTrait   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 76
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BestIt\Sniffs;
6
7
use PHP_CodeSniffer\Files\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|null
22
     */
23
    private ?SuppressHelper $suppressHelper = null;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected '?', expecting T_FUNCTION or T_CONST
Loading history...
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