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\ControlStructures; |
11
|
|
|
|
12
|
|
|
use PHP_CodeSniffer_File; |
13
|
|
|
use PHP_CodeSniffer_Sniff; |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Rules: |
18
|
|
|
* - New class statement should not have empty parentheses. |
19
|
|
|
*/ |
20
|
|
|
final class NewClassSniff implements PHP_CodeSniffer_Sniff |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
const NAME = 'ZenifyCodingStandard.ControlStructures.NewClass'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var PHP_CodeSniffer_File |
30
|
|
|
*/ |
31
|
|
|
private $file; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var int |
35
|
|
|
*/ |
36
|
|
|
private $position; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var int |
40
|
|
|
*/ |
41
|
|
|
private $openParenthesisPosition; |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return int[] |
46
|
|
|
*/ |
47
|
2 |
|
public function register() : array |
48
|
|
|
{ |
49
|
2 |
|
return [T_NEW]; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param PHP_CodeSniffer_File $file |
55
|
|
|
* @param int $position |
56
|
|
|
*/ |
57
|
2 |
|
public function process(PHP_CodeSniffer_File $file, $position) |
58
|
|
|
{ |
59
|
2 |
|
$this->file = $file; |
60
|
2 |
|
$this->position = $position; |
61
|
|
|
|
62
|
2 |
|
if ( ! $this->hasEmptyParentheses()) { |
63
|
2 |
|
return; |
64
|
|
|
} |
65
|
|
|
|
66
|
2 |
|
$fix = $file->addFixableError('New class statement should not have empty parentheses', $position); |
67
|
2 |
|
if ($fix) { |
68
|
1 |
|
$this->removeParenthesesFromClassStatement($position); |
69
|
|
|
} |
70
|
2 |
|
} |
71
|
|
|
|
72
|
|
|
|
73
|
2 |
|
private function hasEmptyParentheses() : bool |
74
|
|
|
{ |
75
|
2 |
|
$tokens = $this->file->getTokens(); |
76
|
2 |
|
$nextPosition = $this->position; |
77
|
|
|
|
78
|
|
|
do { |
79
|
2 |
|
$nextPosition++; |
80
|
2 |
|
} while ( ! $this->doesContentContains($tokens[$nextPosition]['content'], [';', '(', ',', ')'])); |
81
|
|
|
|
82
|
2 |
|
if ($tokens[$nextPosition]['content'] === '(') { |
83
|
2 |
|
if ($tokens[$nextPosition + 1]['content'] === ')') { |
84
|
2 |
|
$this->openParenthesisPosition = $nextPosition; |
85
|
2 |
|
return TRUE; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
2 |
|
return FALSE; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
|
93
|
2 |
|
private function doesContentContains(string $content, array $chars) : bool |
94
|
|
|
{ |
95
|
2 |
|
foreach ($chars as $char) { |
96
|
2 |
|
if ($content === $char) { |
97
|
2 |
|
return TRUE; |
98
|
|
|
} |
99
|
|
|
} |
100
|
2 |
|
return FALSE; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
|
104
|
1 |
|
private function removeParenthesesFromClassStatement(int $position) |
|
|
|
|
105
|
|
|
{ |
106
|
1 |
|
$this->file->fixer->replaceToken($this->openParenthesisPosition, ''); |
107
|
1 |
|
$this->file->fixer->replaceToken($this->openParenthesisPosition + 1, ''); |
108
|
1 |
|
} |
109
|
|
|
|
110
|
|
|
} |
111
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.