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

IfElseTryCatchFinallySniff::findFirstPositionInCurrentLine()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
crap 2
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
use ZenifyCodingStandard\Helper\PositionFinder;
15
use ZenifyCodingStandard\Helper\Whitespace\EmptyLinesResizer;
16
17
18
/**
19
 * Rules:
20
 * - Else/elseif/catch/finally statement should be preceded by x empty line(s)
21
 */
22
final class IfElseTryCatchFinallySniff implements PHP_CodeSniffer_Sniff
23
{
24
25
	/**
26
	 * @var string
27
	 */
28
	const NAME = 'ZenifyCodingStandard.WhiteSpace.IfElseTryCatchFinally';
29
30
	/**
31
	 * @var int
32
	 */
33
	private $requiredEmptyLineCountBeforeStatement = 1;
34
35
	/**
36
	 * @var PHP_CodeSniffer_File
37
	 */
38
	private $file;
39
40
	/**
41
	 * @var int
42
	 */
43
	private $position;
44
45
	/**
46
	 * @var array
47
	 */
48
	private $tokens;
49
50
51
	/**
52
	 * @return int[]
53
	 */
54 2
	public function register() : array
55
	{
56 2
		return [T_ELSE, T_ELSEIF, T_CATCH, T_FINALLY];
57
	}
58
59
60
	/**
61
	 * @param PHP_CodeSniffer_File $file
62
	 * @param int $position
63
	 */
64 2
	public function process(PHP_CodeSniffer_File $file, $position)
65
	{
66 2
		$this->file = $file;
67 2
		$this->position = $position;
68 2
		$this->tokens = $file->getTokens();
69
70
		// Fix type
71 2
		$this->requiredEmptyLineCountBeforeStatement = (int) $this->requiredEmptyLineCountBeforeStatement;
72
73 2
		$emptyLineCountBeforeStatement = $this->getEmptyLinesCountBefore();
74 2
		if ($emptyLineCountBeforeStatement === $this->requiredEmptyLineCountBeforeStatement) {
75 2
			return;
76
		}
77
78 2
		$error = sprintf(
79 2
			'%s statement should be preceded by %s empty line(s); %s found',
80 2
			ucfirst($this->tokens[$position]['content']),
81 2
			$this->requiredEmptyLineCountBeforeStatement,
82
			$emptyLineCountBeforeStatement
83
		);
84 2
		$fix = $file->addFixableError($error, $position);
85 2
		if ($fix) {
86 1
			EmptyLinesResizer::resizeLines(
87
				$file,
88 1
				PositionFinder::findFirstPositionInCurrentLine($this->file, $position),
89
				$emptyLineCountBeforeStatement,
90 1
				$this->requiredEmptyLineCountBeforeStatement
91
			);
92
		}
93 2
	}
94
95
96 2
	private function getEmptyLinesCountBefore() : int
97
	{
98 2
		$currentLine = $this->tokens[$this->position]['line'];
99 2
		$previousPosition = $this->position;
100
101
		do {
102 2
			$previousPosition--;
103
		} while (
104 2
			$currentLine === $this->tokens[$previousPosition]['line']
105 2
			|| $this->tokens[$previousPosition]['code'] === T_WHITESPACE
106
		);
107
108 2
		return $this->tokens[$this->position]['line'] - $this->tokens[$previousPosition]['line'] - 1;
109
	}
110
111
}
112