VarPropertyCommentSniff   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 86.36%

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 2
dl 0
loc 66
ccs 19
cts 22
cp 0.8636
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A processMemberVar() 0 10 2
A processVariable() 0 3 1
A processVariableInString() 0 3 1
B getPropertyComment() 0 22 4
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
	 * @var string
28
	 */
29
	const NAME = 'ZenifyCodingStandard.Commenting.VarPropertyComment';
30
31
	/**
32
	 * @param PHP_CodeSniffer_File $file
33
	 * @param int $position
34
	 */
35 1
	protected function processMemberVar(PHP_CodeSniffer_File $file, $position)
36
	{
37 1
		$commentString = $this->getPropertyComment($file, $position);
38
39 1
		if (strpos($commentString, '@var') !== FALSE) {
40 1
			return;
41
		}
42
43 1
		$file->addError('Property should have docblock comment.', $position);
44 1
	}
45
46
47
	/**
48
	 * @param PHP_CodeSniffer_File $file
49
	 * @param int $position
50
	 */
51 1
	protected function processVariable(PHP_CodeSniffer_File $file, $position)
52
	{
53 1
	}
54
55
56
	/**
57
	 * @param PHP_CodeSniffer_File $file
58
	 * @param int $position
59
	 */
60
	protected function processVariableInString(PHP_CodeSniffer_File $file, $position)
61
	{
62
	}
63
64
65 1
	private function getPropertyComment(PHP_CodeSniffer_File $file, int $position) : string
66
	{
67 1
		$commentEnd = $file->findPrevious([T_DOC_COMMENT_CLOSE_TAG], $position);
68 1
		if ($commentEnd === FALSE) {
69 1
			return '';
70
		}
71
72 1
		$tokens = $file->getTokens();
73 1
		if ($tokens[$commentEnd]['code'] !== T_DOC_COMMENT_CLOSE_TAG) {
74
			return '';
75
76
		} else {
77
			// Make sure the comment we have found belongs to us.
78 1
			$commentFor = $file->findNext(T_VARIABLE, $commentEnd + 1);
79 1
			if ($commentFor !== $position) {
80 1
				return '';
81
			}
82
		}
83
84 1
		$commentStart = $file->findPrevious(T_DOC_COMMENT_OPEN_TAG, $position);
85 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 84 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...
86
	}
87
88
}
89