DocBlockSniff   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 4 1
A process() 0 18 4
A isInlineComment() 0 7 2
A isIndentationInFrontCorrect() 0 11 3
A isIndentationInsideCorrect() 0 14 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\WhiteSpace;
11
12
use PHP_CodeSniffer_File;
13
use PHP_CodeSniffer_Sniff;
14
15
16
/**
17
 * Rules:
18
 * - DocBlock lines should start with space (except first one)
19
 */
20
final class DocBlockSniff implements PHP_CodeSniffer_Sniff
21
{
22
23
	/**
24
	 * @var string
25
	 */
26
	const NAME = 'ZenifyCodingStandard.WhiteSpace.DocBlock';
27
28
	/**
29
	 * @var PHP_CodeSniffer_File
30
	 */
31
	private $file;
32
33
	/**
34
	 * @var int
35
	 */
36
	private $position;
37
38
	/**
39
	 * @var array[]
40
	 */
41
	private $tokens;
42
43
44
	/**
45
	 * @return int[]
46
	 */
47 1
	public function register() : array
48
	{
49 1
		return [T_DOC_COMMENT_STAR, T_DOC_COMMENT_CLOSE_TAG];
50
	}
51
52
53
	/**
54
	 * @param PHP_CodeSniffer_File $file
55
	 * @param int $position
56
	 */
57 1
	public function process(PHP_CodeSniffer_File $file, $position)
58
	{
59 1
		$this->file = $file;
60 1
		$this->position = $position;
61 1
		$this->tokens = $file->getTokens();
62
63 1
		if ($this->isInlineComment()) {
64 1
			return;
65
		}
66
67 1
		if ( ! $this->isIndentationInFrontCorrect()) {
68 1
			$file->addError('DocBlock lines should start with space (except first one)', $position);
69
		}
70
71 1
		if ( ! $this->isIndentationInsideCorrect()) {
72 1
			$file->addError('Indentation in DocBlock should be one space followed by tabs (if necessary)', $position);
73
		}
74 1
	}
75
76
77 1
	private function isInlineComment() : bool
78
	{
79 1
		if ($this->tokens[$this->position - 1]['code'] !== T_DOC_COMMENT_WHITESPACE) {
80 1
			return TRUE;
81
		}
82 1
		return FALSE;
83
	}
84
85
86 1
	private function isIndentationInFrontCorrect() : bool
87
	{
88 1
		$tokens = $this->file->getTokens();
89 1
		if ($tokens[$this->position - 1]['content'] === ' ') {
90 1
			return TRUE;
91
		}
92 1
		if ((strlen($tokens[$this->position - 1]['content']) % 2) === 0) {
93 1
			return TRUE;
94
		}
95 1
		return FALSE;
96
	}
97
98
99 1
	private function isIndentationInsideCorrect() : bool
100
	{
101 1
		$tokens = $this->file->getTokens();
102 1
		if ($tokens[$this->position + 1]['code'] === 'PHPCS_T_DOC_COMMENT_WHITESPACE') {
103 1
			$content = $tokens[$this->position + 1]['content'];
104 1
			$content = rtrim($content, "\n");
105 1
			if ( strlen($content) > 1
106 1
				&& $content !== ' ' . str_repeat("\t", strlen($content) - 1)
107
			) {
108 1
				return FALSE;
109
			}
110
		}
111 1
		return TRUE;
112
	}
113
114
}
115