PhpDocCommentTrait::findCommentTagIndexes()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 6
c 0
b 0
f 0
nc 3
nop 3
dl 0
loc 12
ccs 0
cts 7
cp 0
crap 12
rs 10
1
<?php
2
3
/**
4
 * Copyright MediaCT. All rights reserved.
5
 * https://www.mediact.nl
6
 */
7
8
namespace Mediact\CodingStandard;
9
10
use PHP_CodeSniffer\Files\File;
11
use PHP_CodeSniffer\Util\Tokens;
12
13
trait PhpDocCommentTrait
14
{
15
    /**
16
     * Gets the end of a PHPDoc comment that is directly above an element.
17
     *
18
     * @param File $file
19
     * @param int  $elementIndex
20
     *
21
     * @return bool|int
22
     */
23
    protected function findCommentEndIndex(File $file, $elementIndex)
24
    {
25
        $searchTypes   = array_merge(Tokens::$methodPrefixes, [T_WHITESPACE]);
26
        $previousToken = $file->findPrevious($searchTypes, $elementIndex - 1, null, true);
27
        if (
28
            $previousToken
0 ignored issues
show
Bug Best Practice introduced by
The expression $previousToken of type false|integer is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
29
            && $file->getTokens()[$previousToken]['code'] == T_DOC_COMMENT_CLOSE_TAG
30
        ) {
31
            return $previousToken;
32
        }
33
34
        return false;
35
    }
36
37
    /**
38
     * Gets the start of a PHPDoc comment based on the end index.
39
     *
40
     * @param File $file
41
     * @param int  $commentEnd
42
     *
43
     * @return bool|int
44
     */
45
    protected function findCommentStartIndex(File $file, $commentEnd)
46
    {
47
        if (!$commentEnd) {
48
            return false;
49
        }
50
51
        return $file->getTokens()[$commentEnd]['comment_opener'];
52
    }
53
54
55
    /**
56
     * Gets the index of a PHPDoc tag.
57
     *
58
     * @param File   $file
59
     * @param int    $commentStart
60
     * @param string $tagName
61
     *
62
     * @return bool|int
63
     */
64
    protected function findSingleCommentTagIndex(File $file, $commentStart, $tagName)
65
    {
66
        $indexes = $this->findCommentTagIndexes($file, $commentStart, $tagName);
67
        return count($indexes)
68
            ? array_shift($indexes)
69
            : false;
70
    }
71
72
    /**
73
     * Gets the indexes of a PHPDoc tag.
74
     *
75
     * @param File   $file
76
     * @param int    $commentStart
77
     * @param string $tagName
78
     *
79
     * @return array
80
     */
81
    protected function findCommentTagIndexes(File $file, $commentStart, $tagName)
82
    {
83
        $indexes = [];
84
        $tokens  = $file->getTokens();
85
86
        foreach ($tokens[$commentStart]['comment_tags'] as $index) {
87
            if ($tokens[$index]['content'] === $tagName) {
88
                $indexes[] = $index;
89
            }
90
        }
91
92
        return $indexes;
93
    }
94
}
95