|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of Zenify |
|
7
|
|
|
* Copyright (c) 2012 Tomas Votruba (http://tomasvotruba.cz) |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace ZenifyCodingStandard\Sniffs\WhiteSpace; |
|
11
|
|
|
|
|
12
|
|
|
use PHP_CodeSniffer_File; |
|
13
|
|
|
use PHP_CodeSniffer_Sniff; |
|
14
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Rules: |
|
18
|
|
|
* - Not operator (!) should be surrounded by spaces. |
|
19
|
|
|
*/ |
|
20
|
|
|
final class ExclamationMarkSniff implements PHP_CodeSniffer_Sniff |
|
21
|
|
|
{ |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
const NAME = 'ZenifyCodingStandard.WhiteSpace.ExclamationMark'; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var PHP_CodeSniffer_File |
|
30
|
|
|
*/ |
|
31
|
|
|
private $file; |
|
32
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @return int[] |
|
36
|
|
|
*/ |
|
37
|
2 |
|
public function register() : array |
|
38
|
|
|
{ |
|
39
|
2 |
|
return [T_BOOLEAN_NOT]; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param PHP_CodeSniffer_File $file |
|
45
|
|
|
* @param int $position |
|
46
|
|
|
*/ |
|
47
|
2 |
|
public function process(PHP_CodeSniffer_File $file, $position) |
|
48
|
|
|
{ |
|
49
|
2 |
|
$this->file = $file; |
|
50
|
|
|
|
|
51
|
2 |
|
$tokens = $file->getTokens(); |
|
52
|
2 |
|
$hasSpaceBefore = $tokens[$position - 1]['code'] === T_WHITESPACE; |
|
53
|
2 |
|
$hasSpaceAfter = $tokens[$position + 1]['code'] === T_WHITESPACE; |
|
54
|
|
|
|
|
55
|
2 |
|
if ( ! $hasSpaceBefore || ! $hasSpaceAfter) { |
|
56
|
2 |
|
$error = 'Not operator (!) should be surrounded by spaces.'; |
|
57
|
2 |
|
$fix = $file->addFixableError($error, $position); |
|
58
|
2 |
|
if ($fix) { |
|
59
|
1 |
|
$this->fixSpacesAroundExclamationMark($position, $hasSpaceBefore, $hasSpaceAfter); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
2 |
|
} |
|
63
|
|
|
|
|
64
|
|
|
|
|
65
|
1 |
|
private function fixSpacesAroundExclamationMark(int $position, bool $isSpaceBefore, bool $isSpaceAfter) |
|
66
|
|
|
{ |
|
67
|
1 |
|
if ( ! $isSpaceBefore) { |
|
68
|
1 |
|
$this->file->fixer->addContentBefore($position, ' '); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
1 |
|
if ( ! $isSpaceAfter) { |
|
72
|
1 |
|
$this->file->fixer->addContentBefore($position + 1, ' '); |
|
73
|
|
|
} |
|
74
|
1 |
|
} |
|
75
|
|
|
|
|
76
|
|
|
} |
|
77
|
|
|
|