Passed
Pull Request — master (#19)
by
unknown
02:19
created

AbstractDocSniff::processToken()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 36
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 5.0023

Importance

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