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

VarPropertyCommentSniff   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 86.36%

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A processVariable() 0 3 1
A processMemberVar() 0 10 2
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
	 * @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