Completed
Push — master ( 1a6e67...071a29 )
by Tomáš
02:38
created

BlockPropertyCommentSniff   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 97.73%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 2
dl 0
loc 114
ccs 43
cts 44
cp 0.9773
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 4 1
A isSingleLineDoc() 0 8 2
A changeSingleLineDocToDocBlock() 0 18 1
A process() 0 19 4
B isPropertyOrMethodComment() 0 16 5
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 string
25
	 */
26
	const NAME = 'ZenifyCodingStandard.Commenting.BlockPropertyComment';
27
28
	/**
29
	 * @var PHP_CodeSniffer_File
30
	 */
31
	private $file;
32
33
	/**
34
	 * @var array
35
	 */
36
	private $tokens;
37
38
39
	/**
40
	 * @return int[]
41
	 */
42 2
	public function register() : array
43
	{
44 2
		return [T_DOC_COMMENT_OPEN_TAG];
45
	}
46
47
48
	/**
49
	 * @param PHP_CodeSniffer_File $file
50
	 * @param int $position
51
	 */
52 2
	public function process(PHP_CodeSniffer_File $file, $position)
53
	{
54 2
		$this->file = $file;
55 2
		$this->tokens = $file->getTokens();
56
57 2
		$closeTagPosition = $file->findNext(T_DOC_COMMENT_CLOSE_TAG, $position + 1);
58 2
		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 57 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...
59 1
			return;
60
61 2
		} 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 57 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...
62 2
			return;
63
		}
64
65 2
		$fix = $file->addFixableError('Block comment should be used instead of one liner', $position);
66
67 2
		if ($fix) {
68 1
			$this->changeSingleLineDocToDocBlock($position);
69
		}
70 2
	}
71
72
73 2
	private function isPropertyOrMethodComment(int $position) : bool
74
	{
75 2
		$nextPropertyOrMethodPosition = $this->file->findNext([T_VARIABLE, T_FUNCTION], $position + 1);
76
77 2
		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...
78 2
			if ($this->isVariableOrPropertyUse($nextPropertyOrMethodPosition) === TRUE) {
79 1
				return FALSE;
80
			}
81
82 2
			if (($this->tokens[$position]['line'] + 1) === $this->tokens[$nextPropertyOrMethodPosition]['line']) {
83 2
				return TRUE;
84
			}
85
		}
86
87
		return FALSE;
88
	}
89
90
91 2
	private function isSingleLineDoc(int $openTagPosition, int $closeTagPosition) : bool
92
	{
93 2
		$lines = $this->tokens[$closeTagPosition]['line'] - $this->tokens[$openTagPosition]['line'];
94 2
		if ($lines < 2) {
95 2
			return TRUE;
96
		}
97 2
		return FALSE;
98
	}
99
100
101 2
	private function isVariableOrPropertyUse(int $position) : bool
102
	{
103 2
		$previous = $this->file->findPrevious(T_OPEN_CURLY_BRACKET, $position);
104 2
		if ($previous) {
105 2
			$previous = $this->file->findPrevious(T_OPEN_CURLY_BRACKET, $previous - 1);
106 2
			if ($this->tokens[$previous]['code'] === T_OPEN_CURLY_BRACKET) { // used in method
107 1
				return TRUE;
108
			}
109
		}
110 2
		return FALSE;
111
	}
112
113
114 1
	private function changeSingleLineDocToDocBlock(int $position)
115
	{
116 1
		$commentEndPosition = $this->tokens[$position]['comment_closer'];
117
118 1
		$empty = [T_DOC_COMMENT_WHITESPACE, T_DOC_COMMENT_STAR];
119 1
		$shortPosition = $this->file->findNext($empty, $position + 1, $commentEndPosition, TRUE);
120
121
		// indent content after /** to indented new line
122 1
		$this->file->fixer->addContentBefore($shortPosition, PHP_EOL . "\t" . ' * ');
0 ignored issues
show
Security Bug introduced by
It seems like $shortPosition defined by $this->file->findNext($e...mmentEndPosition, TRUE) on line 119 can also be of type false; however, PHP_CodeSniffer_Fixer::addContentBefore() 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...
123
124
		// remove spaces
125 1
		$this->file->fixer->replaceToken($position + 1, '');
126 1
		$spacelessContent = trim($this->tokens[$commentEndPosition - 1]['content']);
127 1
		$this->file->fixer->replaceToken($commentEndPosition - 1, $spacelessContent);
128
129
		// indent end to indented newline
130 1
		$this->file->fixer->replaceToken($commentEndPosition, PHP_EOL . "\t" . ' */');
131 1
	}
132
133
}
134