AbstractClassNameSniff   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 70
Duplicated Lines 12.86 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 94.74%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 9
loc 70
ccs 18
cts 19
cp 0.9474
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 4 1
A process() 0 15 3
A isClassAbstract() 0 5 1
A getClassName() 9 9 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
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
 * - Abstract class should have prefix "Abstract"
19
 */
20
final class AbstractClassNameSniff implements PHP_CodeSniffer_Sniff
21
{
22
23
	/**
24
	 * @var string
25
	 */
26
	const NAME = 'ZenifyCodingStandard.Naming.AbstractClassName';
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_CLASS];
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
		if ( ! $this->isClassAbstract()) {
58 1
			return;
59
		}
60
61 1
		if (strpos($this->getClassName(), 'Abstract') === 0) {
62 1
			return;
63
		}
64
65 1
		$file->addError('Abstract class should have prefix "Abstract".', $position);
66 1
	}
67
68
69 1
	private function isClassAbstract() : bool
70
	{
71 1
		$classProperties = $this->file->getClassProperties($this->position);
72 1
		return $classProperties['is_abstract'];
73
	}
74
75
76
	/**
77
	 * @return string|FALSE
78
	 */
79 1 View Code Duplication
	private function getClassName()
80
	{
81 1
		$namePosition = $this->file->findNext(T_STRING, $this->position);
82 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...
83
			return FALSE;
84
		}
85
86 1
		return $this->file->getTokens()[$namePosition]['content'];
87
	}
88
89
}
90