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

NamespaceDeclarationSniff   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 120
Duplicated Lines 15 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 97.5%

Importance

Changes 0
Metric Value
wmc 13
c 0
b 0
f 0
lcom 1
cbo 1
dl 18
loc 120
ccs 39
cts 40
cp 0.975
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 4 1
B process() 18 33 5
A getLinesToNextUse() 0 12 3
A getLinesToNextClass() 0 13 2
A isInsideClass() 0 8 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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