Completed
Pull Request — master (#41)
by Tomáš
07:50 queued 04:44
created

ComponentFactoryCommentSniff::hasMethodComment()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
3
/*
4
 * This file is part of Zenify
5
 * Copyright (c) 2012 Tomas Votruba (http://tomasvotruba.cz)
6
 */
7
8
namespace ZenifyCodingStandard\Sniffs\Commenting;
9
10
use PHP_CodeSniffer_File;
11
use PHP_CodeSniffer_Sniff;
12
13
14
/**
15
 * Rules:
16
 * - CreateComponent* method should have a doc comment.
17
 * - CreateComponent* method should have a return tag.
18
 * - Return tag should contain type.
19
 */
20
final class ComponentFactoryCommentSniff implements PHP_CodeSniffer_Sniff
21
{
22
23
	/**
24
	 * @var int
25
	 */
26
	private $position;
27
28
	/**
29
	 * @var PHP_CodeSniffer_File
30
	 */
31
	private $file;
32
33
	/**
34
	 * @var array
35
	 */
36
	private $tokens;
37
38
39
	/**
40
	 * {@inheritdoc}
41
	 */
42 1
	public function register()
43
	{
44 1
		return [T_FUNCTION];
45
	}
46
47
48
	/**
49
	 * {@inheritdoc}
50
	 */
51 1
	public function process(PHP_CodeSniffer_File $file, $position)
52
	{
53 1
		$this->file = $file;
54 1
		$this->position = $position;
55 1
		$this->tokens = $file->getTokens();
56
57 1
		if ( ! $this->isComponentFactoryMethod()) {
58
			return;
59
		}
60
61 1
		$commentEnd = $this->getCommentEnd();
62 1
		if ( ! $this->hasMethodComment($commentEnd)) {
0 ignored issues
show
Bug introduced by
It seems like $commentEnd defined by $this->getCommentEnd() on line 61 can also be of type boolean; however, ZenifyCodingStandard\Sni...iff::hasMethodComment() 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...
63 1
			$file->addError('CreateComponent* method should have a doc comment', $position);
64 1
			return;
65
		}
66
67 1
		$commentStart = $this->tokens[$commentEnd]['comment_opener'];
68 1
		$this->processReturnTag($commentStart);
69 1
	}
70
71
72
	/**
73
	 * @return bool
74
	 */
75 1
	private function isComponentFactoryMethod()
76
	{
77 1
		$functionName = $this->file->getDeclarationName($this->position);
78 1
		return (strpos($functionName, 'createComponent') === 0);
79
	}
80
81
82
	/**
83
	 * @return bool|int
84
	 */
85 1
	private function getCommentEnd()
86
	{
87 1
		return $this->file->findPrevious(T_WHITESPACE, ($this->position - 3), NULL, TRUE);
88
	}
89
90
91
	/**
92
	 * @param int $position
93
	 * @return bool
94
	 */
95 1
	private function hasMethodComment($position)
96
	{
97 1
		if ($this->tokens[$position]['code'] === T_DOC_COMMENT_CLOSE_TAG) {
98 1
			return TRUE;
99
		}
100 1
		return FALSE;
101
	}
102
103
104
	/**
105
	 * @param int $commentStartPosition
106
	 */
107 1
	private function processReturnTag($commentStartPosition)
108
	{
109 1
		$return = NULL;
110 1
		foreach ($this->tokens[$commentStartPosition]['comment_tags'] as $tag) {
111 1
			if ($this->tokens[$tag]['content'] === '@return') {
112 1
				$return = $tag;
113
			}
114
		}
115 1
		if ($return !== NULL) {
116 1
			$content = $this->tokens[($return + 2)]['content'];
117 1
			if (empty($content) === TRUE || $this->tokens[($return + 2)]['code'] !== T_DOC_COMMENT_STRING) {
118 1
				$error = 'Return tag should contain type';
119 1
				$this->file->addError($error, $return);
120
			}
121
122
		} else {
123 1
			$error = 'CreateComponent* method should have a @return tag';
124 1
			$this->file->addError($error, $this->tokens[$commentStartPosition]['comment_closer']);
125
		}
126 1
	}
127
128
}
129