InterfaceNameSniff::getInterfaceName()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 9
Ratio 100 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
dl 9
loc 9
ccs 4
cts 5
cp 0.8
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
crap 2.032
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\Naming;
11
12
use PHP_CodeSniffer_File;
13
use PHP_CodeSniffer_Sniff;
14
15
16
/**
17
 * Rules:
18
 * - Interface should have suffix "Interface"
19
 */
20
final class InterfaceNameSniff implements PHP_CodeSniffer_Sniff
21
{
22
23
	/**
24
	 * @var string
25
	 */
26
	const NAME = 'ZenifyCodingStandard.Naming.InterfaceName';
27
28
	/**
29
	 * @var PHP_CodeSniffer_File
30
	 */
31
	private $file;
32
33
	/**
34
	 * @var int
35
	 */
36
	private $position;
37
38
39
	/**
40
	 * @return int[]
41
	 */
42 1
	public function register() : array
43
	{
44 1
		return [T_INTERFACE];
45
	}
46
47
48
	/**
49
	 * @param PHP_CodeSniffer_File $file
50
	 * @param int $position
51
	 */
52 1
	public function process(PHP_CodeSniffer_File $file, $position)
53
	{
54 1
		$this->file = $file;
55 1
		$this->position = $position;
56
57 1
		$interfaceName = $this->getInterfaceName();
58 1
		if ((strlen($interfaceName) - strlen('Interface')) === strrpos($interfaceName, 'Interface')) {
59 1
			return;
60
		}
61
62 1
		$file->addError('Interface should have suffix "Interface".', $position);
63 1
	}
64
65
66
	/**
67
	 * @return string|FALSE
68
	 */
69 1 View Code Duplication
	private function getInterfaceName()
70
	{
71 1
		$namePosition = $this->file->findNext(T_STRING, $this->position);
72 1
		if ( ! $namePosition) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $namePosition of type integer|false is loosely compared to false; this is ambiguous if the integer can be zero. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
73
			return FALSE;
74
		}
75
76 1
		return $this->file->getTokens()[$namePosition]['content'];
77
	}
78
79
}
80