mediact /
coding-standard
| 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
|
|||
| 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 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: