Passed
Push — master ( f2cb43...4e9f60 )
by Björn
56s queued 10s
created

SuppressingTrait   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 75
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
getFile() 0 1 ?
A getSniffName() 0 14 2
getStackPos() 0 1 ?
A getSuppressHelper() 0 8 2
A isSniffSuppressed() 0 8 1
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) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $sniffName of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch 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:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
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