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

isLastProperty()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.1406

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 6
cts 8
cp 0.75
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 2
crap 3.1406
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
 * - Between properties and methods should be x empty line(s).
19
 */
20
final class PropertiesMethodsMutualSpacingSniff implements PHP_CodeSniffer_Sniff
21
{
22
23
	/**
24
	 * @var int
25
	 */
26
	public $blankLinesInBetween = 2;
27
28
29
	/**
30
	 * {@inheritdoc}
31
	 */
32 1
	public function register()
33
	{
34 1
		return [T_VARIABLE];
35
	}
36
37
38
	/**
39
	 * {@inheritdoc}
40
	 */
41 1
	public function process(PHP_CodeSniffer_File $file, $position)
42
	{
43
		// Fix type
44 1
		$this->blankLinesInBetween = (int) $this->blankLinesInBetween;
45
46 1
		if ($this->isLastProperty($file, $position) === FALSE) {
47
			return;
48
		}
49
50 1
		if ($this->areMethodsPresent($file, $position) === FALSE) {
51
			return;
52
		}
53
54 1
		$tokens = $file->getTokens();
55 1
		$next = $file->findNext([T_DOC_COMMENT_OPEN_TAG, T_FUNCTION], $position);
56
57 1
		$endOfProperty = $this->getEndOfProperty($file, $position);
58 1
		$blankLines = $tokens[$next]['line'] - $tokens[$endOfProperty]['line'] - 1;
59 1
		if ($blankLines !== $this->blankLinesInBetween) {
60 1
			$error = 'Between properties and methods should be %s empty line(s); %s found.';
61
			$data = [
62 1
				$this->blankLinesInBetween,
63 1
				$blankLines
64
			];
65 1
			$file->addError($error, $position, NULL, $data);
66
		}
67 1
	}
68
69
70 1
	private function isLastProperty(PHP_CodeSniffer_File $file, int $position) : bool
71
	{
72 1
		if ($this->isInsideMethod($file, $position)) {
73
			return FALSE;
74
		}
75
76 1
		$tokens = $file->getTokens();
77 1
		$next = $file->findNext([T_VARIABLE, T_FUNCTION], $position + 1);
78 1
		if ($tokens[$next]['code'] === T_VARIABLE) {
79
			return FALSE;
80
		}
81
82 1
		return TRUE;
83
	}
84
85
86 1 View Code Duplication
	private function isInsideMethod(PHP_CodeSniffer_File $file, int $position) : bool
87
	{
88 1
		$previousMethod = $file->findPrevious(T_FUNCTION, $position);
89 1
		$tokens = $file->getTokens();
90 1
		if ($tokens[$previousMethod]['code'] === T_FUNCTION) {
91
			return TRUE;
92
		}
93 1
		return FALSE;
94
	}
95
96
97 1 View Code Duplication
	private function areMethodsPresent(PHP_CodeSniffer_File $file, int $position) : bool
98
	{
99 1
		$next = $file->findNext(T_FUNCTION, $position + 1);
100 1
		$tokens = $file->getTokens();
101 1
		if ($tokens[$next]['code'] === T_FUNCTION) {
102 1
			return TRUE;
103
		}
104
		return FALSE;
105
	}
106
107
108 1
	private function getEndOfProperty(PHP_CodeSniffer_File $file, int $position) : int
109
	{
110 1
		$tokens = $file->getTokens();
111
112 1
		$arrayPosition = $file->findNext(T_ARRAY, $position);
113 1
		if ($tokens[$arrayPosition]['line'] === $tokens[$position]['line']) {
114
			if ($tokens[$arrayPosition]['parenthesis_closer']) {
115
				return $tokens[$arrayPosition]['parenthesis_closer'];
116
			}
117
		}
118
119 1
		$openShortArrayPosition = $file->findNext(T_OPEN_SHORT_ARRAY, $position);
120 1
		if ($tokens[$openShortArrayPosition]['line'] === $tokens[$position]['line']) {
121 1
			return $tokens[$openShortArrayPosition]['bracket_closer'];
122
		}
123
124 1
		return $position;
125
	}
126
127
}
128