Completed
Push — master ( e19264...2ec4ff )
by Tomáš
49:11 queued 44:57
created

VarPropertyCommentSniff::getPropertyComment()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4.0092

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 22
ccs 11
cts 12
cp 0.9167
rs 8.9197
cc 4
eloc 13
nc 4
nop 2
crap 4.0092
1
<?php
2
3
declare(strict_types = 1);
4
5
/*
6
 * This file is part of Zenify
7
 * Copyright (c) 2012 Tomas Votruba (http://tomasvotruba.cz)
8
 */
9
10
namespace ZenifyCodingStandard\Sniffs\Commenting;
11
12
use PHP_CodeSniffer_File;
13
use PHP_CodeSniffer_Standards_AbstractVariableSniff;
14
15
16
/**
17
 * Rules:
18
 * - Property should have docblock comment (except for {@inheritdoc}).
19
 *
20
 * @see PHP_CodeSniffer_Standards_AbstractVariableSniff is used, because it's very difficult to
21
 * separate properties from variables (in args, method etc.). This class does is for us.
22
 */
23
final class VarPropertyCommentSniff extends PHP_CodeSniffer_Standards_AbstractVariableSniff
24
{
25
26
	/**
27
	 * @param PHP_CodeSniffer_File $file
28
	 * @param int $position
29
	 */
30 1
	protected function processMemberVar(PHP_CodeSniffer_File $file, $position)
31
	{
32 1
		$commentString = $this->getPropertyComment($file, $position);
33
34 1
		if (strpos($commentString, '@var') !== FALSE) {
35 1
			return;
36
		}
37
38 1
		$file->addError('Property should have docblock comment.', $position);
39 1
	}
40
41
42
	/**
43
	 * @param PHP_CodeSniffer_File $file
44
	 * @param int $position
45
	 */
46 1
	protected function processVariable(PHP_CodeSniffer_File $file, $position)
47
	{
48 1
	}
49
50
51
	/**
52
	 * @param PHP_CodeSniffer_File $file
53
	 * @param int $position
54
	 */
55
	protected function processVariableInString(PHP_CodeSniffer_File $file, $position)
56
	{
57
	}
58
59
60 1
	private function getPropertyComment(PHP_CodeSniffer_File $file, int $position) : string
61
	{
62 1
		$commentEnd = $file->findPrevious([T_DOC_COMMENT_CLOSE_TAG], $position);
63 1
		if ($commentEnd === FALSE) {
64 1
			return '';
65
		}
66
67 1
		$tokens = $file->getTokens();
68 1
		if ($tokens[$commentEnd]['code'] !== T_DOC_COMMENT_CLOSE_TAG) {
69
			return '';
70
71
		} else {
72
			// Make sure the comment we have found belongs to us.
73 1
			$commentFor = $file->findNext(T_VARIABLE, $commentEnd + 1);
74 1
			if ($commentFor !== $position) {
75 1
				return '';
76
			}
77
		}
78
79 1
		$commentStart = $file->findPrevious(T_DOC_COMMENT_OPEN_TAG, $position);
80 1
		return $file->getTokensAsString($commentStart, $commentEnd - $commentStart + 1);
0 ignored issues
show
Bug introduced by
It seems like $commentStart defined by $file->findPrevious(T_DO...NT_OPEN_TAG, $position) on line 79 can also be of type boolean; however, PHP_CodeSniffer_File::getTokensAsString() does only seem to accept integer, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
81
	}
82
83
}
84