Completed
Pull Request — master (#41)
by Tomáš
07:50 queued 04:44
created

NamespaceDeclarationSniff::getLinesToNextUse()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
ccs 6
cts 6
cp 1
rs 9.4285
cc 3
eloc 7
nc 2
nop 0
crap 3
1
<?php
2
3
/*
4
 * This file is part of Zenify
5
 * Copyright (c) 2012 Tomas Votruba (http://tomasvotruba.cz)
6
 */
7
8
namespace ZenifyCodingStandard\Sniffs\Namespaces;
9
10
use PHP_CodeSniffer_File;
11
use PHP_CodeSniffer_Sniff;
12
13
14
/**
15
 * Rules:
16
 * - There must be x empty line(s) after the namespace declaration or y empty line(s) followed by use statement.
17
 */
18
final class NamespaceDeclarationSniff implements PHP_CodeSniffer_Sniff
19
{
20
21
	/**
22
	 * @var int
23
	 */
24
	public $emptyLinesAfterNamespace = 2;
25
26
	/**
27
	 * @var int
28
	 */
29
	private $emptyLinesBeforeUseStatement;
30
31
	/**
32
	 * @var PHP_CodeSniffer_File
33
	 */
34
	private $file;
35
36
	/**
37
	 * @var int
38
	 */
39
	private $position;
40
41
42
	/**
43
	 * {@inheritdoc}
44
	 */
45 1
	public function register()
46
	{
47 1
		return [T_NAMESPACE];
48
	}
49
50
51
	/**
52
	 * {@inheritdoc}
53
	 */
54 1
	public function process(PHP_CodeSniffer_File $file, $position)
55
	{
56 1
		$this->file = $file;
57 1
		$this->position = $position;
58
59
		// Fix type
60 1
		$this->emptyLinesAfterNamespace = (int) $this->emptyLinesAfterNamespace;
61 1
		$this->emptyLinesBeforeUseStatement = (int) $this->emptyLinesAfterNamespace - 1;
62
63 1
		$linesToNextUse = $this->getLinesToNextUse();
64 1
		$linesToNextClass = $this->getLinesToNextClass();
65
66 1
		if ($linesToNextUse) {
67 1 View Code Duplication
			if ($linesToNextUse !== $this->emptyLinesBeforeUseStatement) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
68 1
				$error = 'There should be %s empty line(s) from namespace to use statement; %s found';
69
				$data = [
70 1
					$this->emptyLinesBeforeUseStatement,
71 1
					$linesToNextUse
72
				];
73 1
				$file->addError($error, $position, 'BlankLineAfter', $data);
74
			}
75
76 1 View Code Duplication
		} elseif ($linesToNextClass) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
77 1
			if ($linesToNextClass !== $this->emptyLinesAfterNamespace) {
78 1
				$error = 'There should be %s empty line(s) after the namespace declaration; %s found';
79
				$data = [
80 1
					$this->emptyLinesAfterNamespace,
81 1
					$linesToNextClass
82
				];
83 1
				$file->addError($error, $position, 'BlankLineAfter', $data);
84
			}
85
		}
86 1
	}
87
88
89
	/**
90
	 * @return bool|int
91
	 */
92 1
	private function getLinesToNextUse()
93
	{
94 1
		$tokens = $this->file->getTokens();
95 1
		$nextUse = $this->file->findNext(T_USE, $this->position, NULL, FALSE);
96
97 1
		if ($tokens[$nextUse]['line'] === 1 || $this->isInsideClass($nextUse)) {
98 1
			return FALSE;
99
100
		} else {
101 1
			return $tokens[$nextUse]['line'] - $tokens[$this->position]['line'] - 1;
102
		}
103
	}
104
105
106
	/**
107
	 * @return bool|int
108
	 */
109 1
	private function getLinesToNextClass()
110
	{
111 1
		$tokens = $this->file->getTokens();
112 1
		$nextClass = $this->file->findNext(
113 1
			[T_CLASS, T_INTERFACE, T_TRAIT, T_DOC_COMMENT_OPEN_TAG], $this->position, NULL, FALSE
114
		);
115 1
		if ($tokens[$nextClass]['line'] === 1) {
116
			return FALSE;
117
118
		} else {
119 1
			return $tokens[$nextClass]['line'] - $tokens[$this->position]['line'] - 1;
120
		}
121
	}
122
123
124
	/**
125
	 * @param $position
126
	 * @return bool
127
	 */
128 1
	private function isInsideClass($position)
129
	{
130 1
		$prevClassPosition = $this->file->findPrevious(T_CLASS, $position, NULL, FALSE);
131 1
		if ($prevClassPosition) {
132 1
			return TRUE;
133
		}
134 1
		return FALSE;
135
	}
136
137
}
138