Completed
Push — master ( 9b18dc...e19264 )
by Tomáš
18s
created

BlockPropertyCommentSniff   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 96.97%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 1
dl 0
loc 85
ccs 32
cts 33
cp 0.9697
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 4 1
A process() 0 16 3
B isPropertyOrMethodComment() 0 16 5
A isSingleLineDoc() 0 8 2
A isVariableOrPropertyUse() 0 11 3
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_Sniff;
14
15
16
/**
17
 * Rules:
18
 * - Block comment should be used instead of one liner.
19
 */
20
final class BlockPropertyCommentSniff implements PHP_CodeSniffer_Sniff
21
{
22
23
	/**
24
	 * @var PHP_CodeSniffer_File
25
	 */
26
	private $file;
27
28
	/**
29
	 * @var array
30
	 */
31
	private $tokens;
32
33
34
	/**
35
	 * {@inheritdoc}
36
	 */
37 1
	public function register()
38
	{
39 1
		return [T_DOC_COMMENT_OPEN_TAG];
40
	}
41
42
43
	/**
44
	 * {@inheritdoc}
45
	 */
46 1
	public function process(PHP_CodeSniffer_File $file, $position)
47
	{
48 1
		$this->file = $file;
49 1
		$this->tokens = $file->getTokens();
50
51 1
		$closeTagPosition = $file->findNext(T_DOC_COMMENT_CLOSE_TAG, $position + 1);
52 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 51 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...
53 1
			return;
54
55 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 51 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...
56 1
			return;
57
		}
58
59 1
		$error = 'Block comment should be used instead of one liner';
60 1
		$file->addError($error, $position);
61 1
	}
62
63
64 1
	private function isPropertyOrMethodComment(int $position) : bool
65
	{
66 1
		$nextPropertyOrMethodPosition = $this->file->findNext([T_VARIABLE, T_FUNCTION], $position + 1);
67
68 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...
69 1
			if ($this->isVariableOrPropertyUse($nextPropertyOrMethodPosition) === TRUE) {
70 1
				return FALSE;
71
			}
72
73 1
			if (($this->tokens[$position]['line'] + 1) === $this->tokens[$nextPropertyOrMethodPosition]['line']) {
74 1
				return TRUE;
75
			}
76
		}
77
78
		return FALSE;
79
	}
80
81
82 1
	private function isSingleLineDoc(int $openTagPosition, int $closeTagPosition) : bool
83
	{
84 1
		$lines = $this->tokens[$closeTagPosition]['line'] - $this->tokens[$openTagPosition]['line'];
85 1
		if ($lines < 2) {
86 1
			return TRUE;
87
		}
88 1
		return FALSE;
89
	}
90
91
92 1
	private function isVariableOrPropertyUse(int $position) : bool
93
	{
94 1
		$previous = $this->file->findPrevious(T_OPEN_CURLY_BRACKET, $position);
95 1
		if ($previous) {
96 1
			$previous = $this->file->findPrevious(T_OPEN_CURLY_BRACKET, $previous - 1);
97 1
			if ($this->tokens[$previous]['code'] === T_OPEN_CURLY_BRACKET) { // used in method
98 1
				return TRUE;
99
			}
100
		}
101 1
		return FALSE;
102
	}
103
104
}
105