Test Failed
Pull Request — master (#2)
by Ashoka
03:06
created

PhpDocCommentTrait::findCommentStartIndex()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 7
rs 9.4285
1
<?php
2
/**
3
 * Copyright MediaCT. All rights reserved.
4
 * https://www.mediact.nl
5
 */
6
namespace Mediact\CodingStandard;
7
8
use PHP_CodeSniffer\Files\File;
0 ignored issues
show
Bug introduced by
The type PHP_CodeSniffer\Files\File was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use PHP_CodeSniffer\Util\Tokens;
0 ignored issues
show
Bug introduced by
The type PHP_CodeSniffer\Util\Tokens was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
11
trait PhpDocCommentTrait
12
{
13
    /**
14
     * Gets the end of a PHPDoc comment that is directly above an element.
15
     *
16
     * @param File $file
17
     * @param int  $elementIndex
18
     *
19
     * @return bool|int
20
     */
21
    protected function findCommentEndIndex(File $file, $elementIndex)
22
    {
23
        $searchTypes   = array_merge(Tokens::$methodPrefixes, [T_WHITESPACE]);
24
        $previousToken = $file->findPrevious($searchTypes, $elementIndex - 1, null, true);
25
        if ($previousToken
26
            && $file->getTokens()[$previousToken]['code'] == T_DOC_COMMENT_CLOSE_TAG
0 ignored issues
show
Bug introduced by
The constant Mediact\CodingStandard\T_DOC_COMMENT_CLOSE_TAG was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
27
        ) {
28
            return $previousToken;
29
        }
30
31
        return false;
32
    }
33
34
    /**
35
     * Gets the start of a PHPDoc comment based on the end index.
36
     *
37
     * @param File $file
38
     * @param int  $commentEnd
39
     *
40
     * @return bool|int
41
     */
42
    protected function findCommentStartIndex(File $file, $commentEnd)
43
    {
44
        if (!$commentEnd) {
45
            return false;
46
        }
47
48
        return $file->getTokens()[$commentEnd]['comment_opener'];
49
    }
50
51
52
    /**
53
     * Gets the index of a PHPDoc tag.
54
     *
55
     * @param File   $file
56
     * @param int    $commentStart
57
     * @param string $tagName
58
     *
59
     * @return bool|int
60
     */
61
    protected function findSingleCommentTagIndex(File $file, $commentStart, $tagName)
62
    {
63
        $indexes = $this->findCommentTagIndexes($file, $commentStart, $tagName);
64
        return count($indexes)
65
            ? array_shift($indexes)
66
            : false;
67
    }
68
69
    /**
70
     * Gets the indexes of a PHPDoc tag.
71
     *
72
     * @param File   $file
73
     * @param int    $commentStart
74
     * @param string $tagName
75
     *
76
     * @return array
77
     */
78
    protected function findCommentTagIndexes(File $file, $commentStart, $tagName)
79
    {
80
        $indexes = [];
81
        $tokens  = $file->getTokens();
82
83
        foreach ($tokens[$commentStart]['comment_tags'] as $index) {
84
            if ($tokens[$index]['content'] === $tagName) {
85
                $indexes[] = $index;
86
            }
87
        }
88
89
        return $indexes;
90
    }
91
}
92