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

BlockPropertyCommentSniff::process()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 16
ccs 11
cts 11
cp 1
rs 9.4285
cc 3
eloc 10
nc 3
nop 2
crap 3
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
 * - Block comment should be used instead of one liner.
17
 */
18
final class BlockPropertyCommentSniff implements PHP_CodeSniffer_Sniff
19
{
20
21
	/**
22
	 * @var PHP_CodeSniffer_File
23
	 */
24
	private $file;
25
26
	/**
27
	 * @var array
28
	 */
29
	private $tokens;
30
31
32
	/**
33
	 * {@inheritdoc}
34
	 */
35 1
	public function register()
36
	{
37 1
		return [T_DOC_COMMENT_OPEN_TAG];
38
	}
39
40
41
	/**
42
	 * {@inheritdoc}
43
	 */
44 1
	public function process(PHP_CodeSniffer_File $file, $position)
45
	{
46 1
		$this->file = $file;
47 1
		$this->tokens = $file->getTokens();
48
49 1
		$closeTagPosition = $file->findNext(T_DOC_COMMENT_CLOSE_TAG, $position + 1);
50 1
		if ($this->isPropertyOrMethodComment($closeTagPosition) === FALSE) {
0 ignored issues
show
Security Bug introduced by
It seems like $closeTagPosition defined by $file->findNext(T_DOC_CO...OSE_TAG, $position + 1) on line 49 can also be of type false; however, ZenifyCodingStandard\Sni...opertyOrMethodComment() does only seem to accept integer, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
51 1
			return;
52
53 1
		} elseif ($this->isSingleLineDoc($position, $closeTagPosition) === FALSE) {
0 ignored issues
show
Security Bug introduced by
It seems like $closeTagPosition defined by $file->findNext(T_DOC_CO...OSE_TAG, $position + 1) on line 49 can also be of type false; however, ZenifyCodingStandard\Sni...niff::isSingleLineDoc() does only seem to accept integer, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
54 1
			return;
55
		}
56
57 1
		$error = 'Block comment should be used instead of one liner';
58 1
		$file->addError($error, $position);
59 1
	}
60
61
62
	/**
63
	 * @param int $position
64
	 * @return bool
65
	 */
66 1
	private function isPropertyOrMethodComment($position)
67
	{
68 1
		$nextPropertyOrMethodPosition = $this->file->findNext([T_VARIABLE, T_FUNCTION], $position + 1);
69
70 1
		if ($nextPropertyOrMethodPosition && $this->tokens[$nextPropertyOrMethodPosition]['code'] !== T_FUNCTION) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $nextPropertyOrMethodPosition of type integer|false is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
71 1
			if ($this->isVariableOrPropertyUse($nextPropertyOrMethodPosition) === TRUE) {
72 1
				return FALSE;
73
			}
74
75 1
			if (($this->tokens[$position]['line'] + 1) === $this->tokens[$nextPropertyOrMethodPosition]['line']) {
76 1
				return TRUE;
77
			}
78
		}
79
80
		return FALSE;
81
	}
82
83
84
	/**
85
	 * @param int $openTagPosition
86
	 * @param int $closeTagPosition
87
	 * @return bool
88
	 */
89 1
	private function isSingleLineDoc($openTagPosition, $closeTagPosition)
90
	{
91 1
		$lines = $this->tokens[$closeTagPosition]['line'] - $this->tokens[$openTagPosition]['line'];
92 1
		if ($lines < 2) {
93 1
			return TRUE;
94
		}
95 1
		return FALSE;
96
	}
97
98
99
	/**
100
	 * @param int $position
101
	 * @return bool
102
	 */
103 1
	private function isVariableOrPropertyUse($position)
104
	{
105 1
		$previous = $this->file->findPrevious(T_OPEN_CURLY_BRACKET, $position);
106 1
		if ($previous) {
107 1
			$previous = $this->file->findPrevious(T_OPEN_CURLY_BRACKET, $previous - 1);
108 1
			if ($this->tokens[$previous]['code'] === T_OPEN_CURLY_BRACKET) { // used in method
109 1
				return TRUE;
110
			}
111
		}
112 1
		return FALSE;
113
	}
114
115
}
116