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\Commenting; |
11
|
|
|
|
12
|
|
|
use PHP_CodeSniffer_File; |
13
|
|
|
use PHP_CodeSniffer_Sniff; |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Rules: |
18
|
|
|
* - Block comment should be used instead of one liner. |
19
|
|
|
*/ |
20
|
|
|
final class BlockPropertyCommentSniff implements PHP_CodeSniffer_Sniff |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
const NAME = 'ZenifyCodingStandard.Commenting.BlockPropertyComment'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var PHP_CodeSniffer_File |
30
|
|
|
*/ |
31
|
|
|
private $file; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
private $tokens; |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return int[] |
41
|
|
|
*/ |
42
|
2 |
|
public function register() : array |
43
|
|
|
{ |
44
|
2 |
|
return [T_DOC_COMMENT_OPEN_TAG]; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param PHP_CodeSniffer_File $file |
50
|
|
|
* @param int $position |
51
|
|
|
*/ |
52
|
2 |
|
public function process(PHP_CodeSniffer_File $file, $position) |
53
|
|
|
{ |
54
|
2 |
|
$this->file = $file; |
55
|
2 |
|
$this->tokens = $file->getTokens(); |
56
|
|
|
|
57
|
2 |
|
$closeTagPosition = $file->findNext(T_DOC_COMMENT_CLOSE_TAG, $position + 1); |
58
|
2 |
|
if ($this->isPropertyOrMethodComment($closeTagPosition) === FALSE) { |
|
|
|
|
59
|
1 |
|
return; |
60
|
|
|
|
61
|
2 |
|
} elseif ($this->isSingleLineDoc($position, $closeTagPosition) === FALSE) { |
|
|
|
|
62
|
2 |
|
return; |
63
|
|
|
} |
64
|
|
|
|
65
|
2 |
|
$fix = $file->addFixableError('Block comment should be used instead of one liner', $position); |
66
|
|
|
|
67
|
2 |
|
if ($fix) { |
68
|
1 |
|
$this->changeSingleLineDocToDocBlock($position); |
69
|
|
|
} |
70
|
2 |
|
} |
71
|
|
|
|
72
|
|
|
|
73
|
2 |
|
private function isPropertyOrMethodComment(int $position) : bool |
74
|
|
|
{ |
75
|
2 |
|
$nextPropertyOrMethodPosition = $this->file->findNext([T_VARIABLE, T_FUNCTION], $position + 1); |
76
|
|
|
|
77
|
2 |
|
if ($nextPropertyOrMethodPosition && $this->tokens[$nextPropertyOrMethodPosition]['code'] !== T_FUNCTION) { |
|
|
|
|
78
|
2 |
|
if ($this->isVariableOrPropertyUse($nextPropertyOrMethodPosition) === TRUE) { |
79
|
1 |
|
return FALSE; |
80
|
|
|
} |
81
|
|
|
|
82
|
2 |
|
if (($this->tokens[$position]['line'] + 1) === $this->tokens[$nextPropertyOrMethodPosition]['line']) { |
83
|
2 |
|
return TRUE; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return FALSE; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
|
91
|
2 |
|
private function isSingleLineDoc(int $openTagPosition, int $closeTagPosition) : bool |
92
|
|
|
{ |
93
|
2 |
|
$lines = $this->tokens[$closeTagPosition]['line'] - $this->tokens[$openTagPosition]['line']; |
94
|
2 |
|
if ($lines < 2) { |
95
|
2 |
|
return TRUE; |
96
|
|
|
} |
97
|
2 |
|
return FALSE; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
|
101
|
2 |
|
private function isVariableOrPropertyUse(int $position) : bool |
102
|
|
|
{ |
103
|
2 |
|
$previous = $this->file->findPrevious(T_OPEN_CURLY_BRACKET, $position); |
104
|
2 |
|
if ($previous) { |
105
|
2 |
|
$previous = $this->file->findPrevious(T_OPEN_CURLY_BRACKET, $previous - 1); |
106
|
2 |
|
if ($this->tokens[$previous]['code'] === T_OPEN_CURLY_BRACKET) { // used in method |
107
|
1 |
|
return TRUE; |
108
|
|
|
} |
109
|
|
|
} |
110
|
2 |
|
return FALSE; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
|
114
|
1 |
|
private function changeSingleLineDocToDocBlock(int $position) |
115
|
|
|
{ |
116
|
1 |
|
$commentEndPosition = $this->tokens[$position]['comment_closer']; |
117
|
|
|
|
118
|
1 |
|
$empty = [T_DOC_COMMENT_WHITESPACE, T_DOC_COMMENT_STAR]; |
119
|
1 |
|
$shortPosition = $this->file->findNext($empty, $position + 1, $commentEndPosition, TRUE); |
120
|
|
|
|
121
|
|
|
// indent content after /** to indented new line |
122
|
1 |
|
$this->file->fixer->addContentBefore($shortPosition, PHP_EOL . "\t" . ' * '); |
|
|
|
|
123
|
|
|
|
124
|
|
|
// remove spaces |
125
|
1 |
|
$this->file->fixer->replaceToken($position + 1, ''); |
126
|
1 |
|
$spacelessContent = trim($this->tokens[$commentEndPosition - 1]['content']); |
127
|
1 |
|
$this->file->fixer->replaceToken($commentEndPosition - 1, $spacelessContent); |
128
|
|
|
|
129
|
|
|
// indent end to indented newline |
130
|
1 |
|
$this->file->fixer->replaceToken($commentEndPosition, PHP_EOL . "\t" . ' */'); |
131
|
1 |
|
} |
132
|
|
|
|
133
|
|
|
} |
134
|
|
|
|
This check looks for type mismatches where the missing type is
false
. This is usually indicative of an error condtion.Consider the follow example
This function either returns a new
DateTime
object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returnedfalse
before passing on the value to another function or method that may not be able to handle afalse
.