Completed
Pull Request — master (#19)
by
unknown
07:50
created

AbstractDocSniff::processToken()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 36
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 36
ccs 22
cts 22
cp 1
rs 8.439
c 0
b 0
f 0
cc 5
eloc 21
nc 3
nop 0
crap 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BestIt\Sniffs\Commenting;
6
7
use BestIt\CodeSniffer\Helper\DocDescriptionHelper;
8
use BestIt\CodeSniffer\Helper\DocHelper;
9
use BestIt\CodeSniffer\Helper\DocSummaryHelper;
10
use BestIt\CodeSniffer\Helper\DocTagHelper;
11
use BestIt\CodeSniffer\Helper\PropertyHelper;
12
use BestIt\CodeSniffer\AbstractSniff;
13
14
/**
15
 * Class AbstractDocSniff
16
 *
17
 * @package BestIt\Sniffs\Commenting
18
 * @author Nick Lubisch <[email protected]>
19
 */
20
abstract class AbstractDocSniff extends AbstractSniff
21
{
22
    /**
23
     * Defines the maximum line length.
24
     *
25
     * @var int
26
     */
27
    const MAX_LINE_LENGTH = 120;
28
29
    /**
30
     * Code that there is no immediate doc comment found before class.
31
     *
32
     * @var string
33
     */
34
    const CODE_NO_IMMEDIATE_DOC_FOUND = 'NoImmediateDocFound';
35
36
    /**
37
     * Message that there is no immediate doc comment found before class.
38
     *
39
     * @var string
40
     */
41
    const MESSAGE_NO_IMMEDIATE_DOC_FOUND = 'No immediate doc comment found before class.';
42
43
    /**
44
     * Code that there is no summary in doc comment.
45
     *
46
     * @var string
47
     */
48
    const CODE_NO_SUMMARY = 'NoSummary';
49
50
    /**
51
     * Message that there is no summary in doc comment.
52
     *
53
     * @var string
54
     */
55
    const MESSAGE_NO_SUMMARY = 'There must be a summary for the doc comment.';
56
57
    /**
58
     * Code that there is no summary in doc comment.
59
     *
60
     * @var string
61
     */
62
    const CODE_SUMMARY_NOT_FIRST = 'SummaryNotFirst';
63
64
    /**
65
     * Message that there is no summary in doc comment.
66
     *
67
     * @var string
68
     */
69
    const MESSAGE_SUMMARY_NOT_FIRST = 'The summary must be the first statement in a comment.';
70
71
    /**
72
     * Code that the summary is too long.
73
     *
74
     * @var string
75
     */
76
    const CODE_SUMMARY_TOO_LONG = 'SummaryTooLong';
77
78
    /**
79
     * Message that there is no summary in doc comment.
80
     *
81
     * @var string
82
     */
83
    const MESSAGE_SUMMARY_TOO_LONG = 'The summary line must not be longer than 120 chars.';
84
85
    /**
86
     * Code that the summary is not multi line.
87
     *
88
     * @var string
89
     */
90
    const CODE_COMMENT_NOT_MULTI_LINE = 'CommentNotMultiLine';
91
92
    /**
93
     * Message that comment is not multi line.
94
     *
95
     * @var string
96
     */
97
    const MESSAGE_COMMENT_NOT_MULTI_LINE = 'Comment is not multi line.';
98
99
    /**
100
     * Code that there is no line after summary.
101
     *
102
     * @var string
103
     */
104
    const CODE_NO_LINE_AFTER_SUMMARY = 'NoLineAfterSummary';
105
106
    /**
107
     * Message that there is no line after summary.
108
     *
109
     * @var string
110
     */
111
    const MESSAGE_NO_LINE_AFTER_SUMMARY = 'There is no empty line after the summary.';
112
113
    /**
114
     * Code that the line after the summary is not empty.
115
     *
116
     * @var string
117
     */
118
    const CODE_LINE_AFTER_SUMMARY_NOT_EMPTY = 'LineAfterSummaryNotEmpty';
119
120
    /**
121
     * Message that the line after the summary is not empty.
122
     *
123
     * @var string
124
     */
125
    const MESSAGE_LINE_AFTER_SUMMARY_NOT_EMPTY = 'The line after the summary is not empty.';
126
127
    /**
128
     * Code that no comment description is found.
129
     *
130
     * @var string
131
     */
132
    const CODE_DESCRIPTION_NOT_FOUND = 'DescriptionNotFound';
133
134
    /**
135
     * Message that no comment description is found.
136
     *
137
     * @var string
138
     */
139
    const MESSAGE_DESCRIPTION_NOT_FOUND = 'There must be an comment description.';
140
141
    /**
142
     * Code that there is no empty line after description.
143
     *
144
     * @var string
145
     */
146
    const CODE_NO_LINE_AFTER_DESCRIPTION = 'NoLineAfterDescription';
147
148
    /**
149
     * Message that there is no empty line after description.
150
     *
151
     * @var string
152
     */
153
    const MESSAGE_NO_LINE_AFTER_DESCRIPTION = 'There must be an empty line after description.';
154
155
    /**
156
     * Code that there is no empty line after description.
157
     *
158
     * @var string
159
     */
160
    const CODE_MUCH_LINES_AFTER_DESCRIPTION = 'MuchLinesAfterDescription';
161
162
    /**
163
     * Message that there is no empty line after description.
164
     *
165
     * @var string
166
     */
167
    const MESSAGE_MUCH_LINES_AFTER_DESCRIPTION = 'There must be exactly one empty line after description.';
168
169
    /**
170
     * Code that the description line is too long.
171
     *
172
     * @var string
173
     */
174
    const CODE_DESCRIPTION_TOO_LONG = 'DescriptionTooLong';
175
176
    /**
177
     * Message that the description line is too long.
178
     *
179
     * @var string
180
     */
181
    const MESSAGE_DESCRIPTION_TOO_LONG = 'The description exceeds the maximum length of 120 chars.';
182
183
    /**
184
     * Code that comment tag is not allowed.
185
     *
186
     * @var string
187
     */
188
    const CODE_TAG_NOT_ALLOWED = 'TagNotAllowed';
189
190
    /**
191
     * Message that comment tag is not allowed.
192
     *
193
     * @var string
194
     */
195
    const MESSAGE_TAG_NOT_ALLOWED = 'The comment tag "%s" is not allowed.';
196
197
    /**
198
     * Code that comment tag must appear minimum x times.
199
     *
200
     * @var string
201
     */
202
    const CODE_TAG_OCCURRENCE_MIN = 'TagOccurrenceMin';
203
204
    /**
205
     * Message that comment tag must appear minimum x times.
206
     *
207
     * @var string
208
     */
209
    const MESSAGE_TAG_OCCURRENCE_MIN = 'The comment tag "%s" must appear minimum %d times.';
210
211
    /**
212
     * Code that comment tag must appear maximum x times.
213
     *
214
     * @var string
215
     */
216
    const CODE_TAG_OCCURRENCE_MAX = 'TagOccurrenceMax';
217
218
    /**
219
     * Message that comment tag must appear maximum x times.
220
     *
221
     * @var string
222
     */
223
    const MESSAGE_TAG_OCCURRENCE_MAX = 'The comment tag "%s" must appear maximum %d times.';
224
225
    /**
226
     * Code that comment tag has the wrong position.
227
     *
228
     * @var string
229
     */
230
    const CODE_TAG_WRONG_POSITION = 'TagWrongPosition';
231
232
    /**
233
     * Message that comment tag has the wrong position.
234
     *
235
     * @var string
236
     */
237
    const MESSAGE_TAG_WRONG_POSITION = 'This tag has the wrong position. Expected "%s"';
238
239
    /**
240
     * Code that the summary starts with an capital letter.
241
     *
242
     * @var string
243
     */
244
    const CODE_SUMMARY_UC_FIRST = 'SummaryUcFirst';
245
246
    /**
247
     * Message that the summary starts with an capital letter.
248
     *
249
     * @var string
250
     */
251
    const MESSAGE_SUMMARY_UC_FIRST = 'The first letter of the summary is not uppercase';
252
253
    /**
254
     * Code that the description starts with an capital letter.
255
     *
256
     * @var string
257
     */
258
    const CODE_DESCRIPTION_UC_FIRST = 'DescriptionUcFirst';
259
260
    /**
261
     * Message that the description starts with an capital letter.
262
     *
263
     * @var string
264
     */
265
    const MESSAGE_DESCRIPTION_UC_FIRST = 'The first letter of the description is not uppercase';
266
267
    /**
268
     * Code that there is no line after given tag.
269
     *
270
     * @var string
271
     */
272
    const CODE_NO_LINE_AFTER_TAG = 'NoLineAfterTag';
273
274
    /**
275
     * Message that there is no line after given tag.
276
     *
277
     * @var string
278
     */
279
    const MESSAGE_NO_LINE_AFTER_TAG = 'There is no blank line after tag %s';
280
281
    /**
282
     * Code that there are too much lines after given tag.
283
     *
284
     * @var string
285
     */
286
    const CODE_MUCH_LINES_AFTER_TAG = 'MuchLinesAfterTag';
287
288
    /**
289
     * Message that there are too much lines after given tag.
290
     *
291
     * @var string
292
     */
293
    const MESSAGE_MUCH_LINES_AFTER_TAG = 'There are too much blank lines after tag %s';
294
295
    /**
296
     * Code that the tag content format is invalid.
297
     *
298
     * @var string
299
     */
300
    const CODE_TAG_CONTENT_FORMAT_INVALID = 'TagFormatContentInvalid';
301
302
    /**
303
     * Message that the tag content format is invalid.
304
     *
305
     * @var string
306
     */
307
    const MESSAGE_TAG_CONTENT_FORMAT_INVALID = '"%s"-Tag format is invalid. Expected: "%s"';
308
309
    /**
310
     * DocHelper for comment
311
     *
312
     * @var DocHelper
313
     */
314
    private $docHelper;
315
316
    /**
317
     * Helper for comment summary.
318
     *
319
     * @var DocSummaryHelper
320
     */
321
    protected $summaryHelper;
322
323
    /**
324
     * The doc comment description helper.
325
     *
326
     * @var DocDescriptionHelper
327
     */
328
    private $descriptionHelper;
329
330
    /**
331
     * The doc comment helper
332
     *
333
     * @var DocTagHelper
334
     */
335
    private $tagHelper;
336
337
    /**
338
     * Indicator if a description is required.
339
     *
340
     * @var bool
341
     */
342
    public $descriptionRequired = false;
343
344
    /**
345
     * Returns an array of registered tokens.
346
     *
347
     * @return int[] Returns array of tokens to listen for
348
     */
349 121
    public function getRegisteredTokens(): array
350
    {
351 121
        return $this->getListenedTokens();
352
    }
353
354
    /**
355
     * Processes a found registered token.
356
     *
357
     * @return void
358
     */
359 121
    public function processToken(): void
360
    {
361 121
        $propertyHelper = new PropertyHelper($this->getFile());
362
363 121
        if ($this->getListenerToken()['code'] === T_VARIABLE
364 26
            && !$propertyHelper->isProperty($this->getListenerPointer())
365
        ) {
366 1
            return;
367
        }
368
369 121
        $this->docHelper = new DocHelper($this->getFile(), $this->getListenerPointer());
370 121
        $this->summaryHelper = new DocSummaryHelper($this->getFile(), $this->docHelper);
371 121
        $this->descriptionHelper = new DocDescriptionHelper(
372 121
            $this->getFile(),
373 121
            $this->docHelper,
374 121
            $this->summaryHelper
375
        );
376 121
        $this->tagHelper = new DocTagHelper($this->getFile(), $this->docHelper, $this->getListenerPointer());
377
378 121
        if (!$this->docHelper->checkCommentExists($this->getListenerPointer())
379 117
            || !$this->docHelper->checkCommentMultiLine()
380
        ) {
381 8
            return;
382
        }
383
384 113
        $this->summaryHelper->checkCommentSummary();
385
386 113
        $this->descriptionHelper->checkCommentDescription(
387 113
            $this->descriptionRequired
388
        );
389
390 113
        $this->tagHelper->checkCommentTags(
391 113
            $this->getTagMetadata(),
392 113
            $this->getDisallowedTags()
393
        );
394 113
    }
395
396
    /**
397
     * Returns TagHelper
398
     *
399
     * @return DocTagHelper Returns doc comment tag helper for callable function
400
     */
401 65
    public function getTagHelper(): DocTagHelper
402
    {
403 65
        return $this->tagHelper;
404
    }
405
406
    /**
407
     * Returns which tokens should be listened to.
408
     *
409
     * @return int[] List of tokens to listen for
410
     */
411
    abstract public function getListenedTokens(): array;
412
413
    /**
414
     * Returns allowed tag occurrences.
415
     *
416
     * @return array List of tag metadata
417
     */
418
    abstract public function getTagMetadata(): array;
419
420
    /**
421
     * Returns an array of disallowed tags.
422
     *
423
     * @return array List of disallowed tags
424
     */
425
    abstract public function getDisallowedTags(): array;
426
}
427