ClassNamesWithoutPreSlashSniff   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 4 1
A process() 0 12 3
A isExcludedClassName() 0 7 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\Namespaces;
11
12
use PHP_CodeSniffer_File;
13
use PHP_CodeSniffer_Sniff;
14
15
16
/**
17
 * Rules:
18
 * - Class name after new/instanceof should not start with slash
19
 */
20
final class ClassNamesWithoutPreSlashSniff implements PHP_CodeSniffer_Sniff
21
{
22
23
	/**
24
	 * @var string
25
	 */
26
	const NAME = 'ZenifyCodingStandard.Namespaces.ClassNamesWithoutPreSlash';
27
28
	/**
29
	 * @var string[]
30
	 */
31
	private $excludedClassNames = [
32
		'DateTime', 'stdClass', 'splFileInfo', 'Exception'
33
	];
34
35
36
	/**
37
	 * @return int[]
38
	 */
39 1
	public function register() : array
40
	{
41 1
		return [T_NEW, T_INSTANCEOF];
42
	}
43
44
45
	/**
46
	 * @param PHP_CodeSniffer_File $file
47
	 * @param int $position
48
	 */
49 1
	public function process(PHP_CodeSniffer_File $file, $position)
50
	{
51 1
		$tokens = $file->getTokens();
52 1
		$classNameStart = $tokens[$position + 2]['content'];
53
54 1
		if ($classNameStart === '\\') {
55 1
			if ($this->isExcludedClassName($tokens[$position + 3]['content'])) {
56 1
				return;
57
			}
58 1
			$file->addError('Class name after new/instanceof should not start with slash.', $position);
59
		}
60 1
	}
61
62
63
	/**
64
	 * @param string $className
65
	 * @return bool
66
	 */
67 1
	private function isExcludedClassName($className)
68
	{
69 1
		if (in_array($className, $this->excludedClassNames)) {
70 1
			return TRUE;
71
		}
72 1
		return FALSE;
73
	}
74
75
}
76