SpaceAroundConcatSniff   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 4
dl 0
loc 118
c 0
b 0
f 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A fixDefaultProblem() 0 20 3
A processToken() 0 14 3
A register() 0 4 1
A setUp() 0 10 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BestIt\Sniffs\Formatting;
6
7
use BestIt\CodeSniffer\CodeError;
8
use BestIt\CodeSniffer\CodeWarning;
9
use BestIt\Sniffs\AbstractSniff;
10
use const T_STRING_CONCAT;
11
use const T_WHITESPACE;
12
13
/**
14
 * Checks for the space around concat dots and fixes them if they are missing.
15
 *
16
 * @author blange <[email protected]>
17
 * @package BestIt\Sniffs\Formatting
18
 */
19
class SpaceAroundConcatSniff extends AbstractSniff
20
{
21
    /**
22
     * You MUST wrap your concat-dot with a whitespace char.
23
     *
24
     * @var string
25
     */
26
    public const CODE_MISSING_SPACE_AROUND_CONCAT = 'MissingSpaceAroundConcat';
27
28
    /**
29
     * The message to the user for this error.
30
     *
31
     * @var string
32
     */
33
    private const MESSAGE_MISSING_SPACE_AROUND_CONCAT = 'Please wrap your concatinations with a whitespace char.';
34
35
    /**
36
     * Is the next token whitespace?
37
     *
38
     * @var bool|void
39
     */
40
    private $nextIsWhitespace;
41
42
    /**
43
     * Is the prev token whitespace?
44
     *
45
     * @var void|bool
46
     */
47
    private $prevIsWhitespace;
48
49
    /**
50
     * Adds whitespace around the concats.
51
     *
52
     * @param CodeWarning $warning
53
     *
54
     * @return void
55
     */
56
    protected function fixDefaultProblem(CodeWarning $warning): void
57
    {
58
        $newContent = '';
59
60
        if (!$this->prevIsWhitespace) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->prevIsWhitespace of type null|boolean is loosely compared to false; this is ambiguous if the boolean can be false. You might want to explicitly use !== null instead.

If an expression can have both false, and null as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.

$a = canBeFalseAndNull();

// Instead of
if ( ! $a) { }

// Better use one of the explicit versions:
if ($a !== null) { }
if ($a !== false) { }
if ($a !== null && $a !== false) { }
Loading history...
61
            $newContent = ' ';
62
        }
63
64
        $newContent .= '.';
65
66
        if (!$this->nextIsWhitespace) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->nextIsWhitespace of type boolean|null is loosely compared to false; this is ambiguous if the boolean can be false. You might want to explicitly use !== null instead.

If an expression can have both false, and null as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.

$a = canBeFalseAndNull();

// Instead of
if ( ! $a) { }

// Better use one of the explicit versions:
if ($a !== null) { }
if ($a !== false) { }
if ($a !== null && $a !== false) { }
Loading history...
67
            $newContent .= ' ';
68
        }
69
70
        $fixer = $this->getFile()->fixer;
71
72
        $fixer->beginChangeset();
73
        $fixer->replaceToken($this->stackPos, $newContent);
74
        $fixer->endChangeset();
75
    }
76
77
    /**
78
     * Processes the token.
79
     *
80
     * @throws CodeError
81
     *
82
     * @return void
83
     *
84
     */
85
    protected function processToken(): void
86
    {
87
        if (!(($this->nextIsWhitespace) && ($this->prevIsWhitespace))) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->nextIsWhitespace of type boolean|null is loosely compared to false; this is ambiguous if the boolean can be false. You might want to explicitly use !== null instead.

If an expression can have both false, and null as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.

$a = canBeFalseAndNull();

// Instead of
if ( ! $a) { }

// Better use one of the explicit versions:
if ($a !== null) { }
if ($a !== false) { }
if ($a !== null && $a !== false) { }
Loading history...
Bug Best Practice introduced by
The expression $this->prevIsWhitespace of type null|boolean is loosely compared to false; this is ambiguous if the boolean can be false. You might want to explicitly use !== null instead.

If an expression can have both false, and null as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.

$a = canBeFalseAndNull();

// Instead of
if ( ! $a) { }

// Better use one of the explicit versions:
if ($a !== null) { }
if ($a !== false) { }
if ($a !== null && $a !== false) { }
Loading history...
88
            $error = new CodeError(
89
                static::CODE_MISSING_SPACE_AROUND_CONCAT,
90
                self::MESSAGE_MISSING_SPACE_AROUND_CONCAT,
91
                $this->stackPos
92
            );
93
94
            $error->isFixable(true);
95
96
            throw $error;
97
        }
98
    }
99
100
    /**
101
     * Registers the tokens that this sniff wants to listen for.
102
     *
103
     * An example return value for a sniff that wants to listen for whitespace
104
     * and any comments would be:
105
     *
106
     * <code>
107
     *    return array(
108
     *            T_WHITESPACE,
109
     *            T_DOC_COMMENT,
110
     *            T_COMMENT,
111
     *           );
112
     * </code>
113
     *
114
     * @return int[]
115
     */
116
    public function register(): array
117
    {
118
        return [T_STRING_CONCAT];
119
    }
120
121
    /**
122
     * Sets up the sniff.
123
     *
124
     * @return void
125
     */
126
    protected function setUp(): void
127
    {
128
        parent::setUp();
129
130
        $nextToken = $this->tokens[$this->stackPos + 1];
131
        $prevToken = $this->tokens[$this->stackPos - 1];
132
133
        $this->nextIsWhitespace = $nextToken['code'] === T_WHITESPACE;
134
        $this->prevIsWhitespace = $prevToken['code'] === T_WHITESPACE;
135
    }
136
}
137