aboutsummaryrefslogtreecommitdiffstats
path: root/build/code_sniffer/phpbb/Sniffs/Namespaces/UnusedUseSniff.php
blob: 0279888de1f7522ce00af50b9b93a250e64d2965 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;

/**
* Checks that each use statement is used.
*/
class phpbb_Sniffs_Namespaces_UnusedUseSniff implements Sniff
{
	/**
	* {@inheritdoc}
	*/
	public function register()
	{
		return array(T_USE);
	}

	protected function check(File $phpcsFile, $found_name, $full_name, $short_name, $line)
	{

		if ($found_name === $full_name)
		{
			$error = 'Either use statement or full name must be used.';
			$phpcsFile->addError($error, $line, 'FullName');
		}

		if ($found_name === $short_name)
		{
			return true;
		}

		return false;
	}

	/**
	* {@inheritdoc}
	*/
	public function process(File $phpcsFile, $stackPtr)
	{
		if ($this->should_ignore_use($phpcsFile, $stackPtr) === true)
		{
			return;
		}

		$tokens = $phpcsFile->getTokens();

		$class_name_start = $phpcsFile->findNext(array(T_NS_SEPARATOR, T_STRING), ($stackPtr + 1));

		$find = array(
			T_NS_SEPARATOR,
			T_STRING,
			T_WHITESPACE,
		);

		$class_name_end = $phpcsFile->findNext($find, ($stackPtr + 1), null, true);

		$aliasing_as_position = $phpcsFile->findNext(T_AS, $class_name_end, null, false, null, true);
		if ($aliasing_as_position !== false)
		{
			$alias_position = $phpcsFile->findNext(T_STRING, $aliasing_as_position, null, false, null, true);
			$class_name_short = $tokens[$alias_position]['content'];
			$class_name_full = $phpcsFile->getTokensAsString($class_name_start, ($class_name_end - $class_name_start - 1));
		}
		else
		{
			$class_name_full = $phpcsFile->getTokensAsString($class_name_start, ($class_name_end - $class_name_start));
			$class_name_short = $tokens[$class_name_end - 1]['content'];
		}

		$ok = false;

		// Checks in simple statements (new, instanceof and extends)
		foreach (array(T_INSTANCEOF, T_NEW, T_EXTENDS) as $keyword)
		{
			$old_simple_statement = $stackPtr;
			while (($simple_statement = $phpcsFile->findNext($keyword, ($old_simple_statement + 1))) !== false)
			{
				$old_simple_statement = $simple_statement;

				$simple_class_name_start = $phpcsFile->findNext(array(T_NS_SEPARATOR, T_STRING), ($simple_statement + 1));

				if ($simple_class_name_start === false) {
					continue;
				}

				$simple_class_name_end = $phpcsFile->findNext($find, ($simple_statement + 1), null, true);

				$simple_class_name = trim($phpcsFile->getTokensAsString($simple_class_name_start, ($simple_class_name_end - $simple_class_name_start)));

				$ok = $this->check($phpcsFile, $simple_class_name, $class_name_full, $class_name_short, $simple_statement) ? true : $ok;
			}
		}

		// Checks paamayim nekudotayim
		$old_paamayim_nekudotayim = $stackPtr;
		while (($paamayim_nekudotayim = $phpcsFile->findNext(T_PAAMAYIM_NEKUDOTAYIM, ($old_paamayim_nekudotayim + 1))) !== false)
		{
			$old_paamayim_nekudotayim = $paamayim_nekudotayim;

			$paamayim_nekudotayim_class_name_start = $phpcsFile->findPrevious($find, $paamayim_nekudotayim - 1, null, true);
			$paamayim_nekudotayim_class_name_end = $paamayim_nekudotayim - 1;

			$paamayim_nekudotayim_class_name = trim($phpcsFile->getTokensAsString($paamayim_nekudotayim_class_name_start + 1, ($paamayim_nekudotayim_class_name_end - $paamayim_nekudotayim_class_name_start)));

			$ok = $this->check($phpcsFile, $paamayim_nekudotayim_class_name, $class_name_full, $class_name_short, $paamayim_nekudotayim) ? true : $ok;
		}

		// Checks in implements
		$old_implements = $stackPtr;
		while (($implements = $phpcsFile->findNext(T_IMPLEMENTS, ($old_implements + 1))) !== false)
		{
			$old_implements = $implements;

			$old_implemented_class = $implements;
			while (($implemented_class = $phpcsFile->findNext(T_STRING, ($old_implemented_class + 1), null, false, null, true)) !== false)
			{
				$old_implemented_class = $implemented_class;

				$implements_class_name_start = $phpcsFile->findNext(array(T_NS_SEPARATOR, T_STRING), ($implemented_class - 1));
				$implements_class_name_end = $phpcsFile->findNext($find, ($implemented_class - 1), null, true);

				$implements_class_name = trim($phpcsFile->getTokensAsString($implements_class_name_start, ($implements_class_name_end - $implements_class_name_start)));

				$ok = $this->check($phpcsFile, $implements_class_name, $class_name_full, $class_name_short, $implements) ? true : $ok;
			}
		}

		$old_docblock = $stackPtr;
		while (($docblock = $phpcsFile->findNext(T_DOC_COMMENT_CLOSE_TAG, ($old_docblock + 1))) !== false)
		{
			$old_docblock = $docblock;
			$ok = $this->checkDocblock($phpcsFile, $docblock, $tokens, $class_name_full, $class_name_short) ? true : $ok;
		}

		// Checks in type hinting
		$old_function_declaration = $stackPtr;
		while (($function_declaration = $phpcsFile->findNext(T_FUNCTION, ($old_function_declaration + 1))) !== false)
		{
			$old_function_declaration = $function_declaration;

			// Check type hint
			$params = $phpcsFile->getMethodParameters($function_declaration);
			foreach ($params as $param)
			{
				$ok = $this->check($phpcsFile, $param['type_hint'], $class_name_full, $class_name_short, $function_declaration) ? true : $ok;
			}
		}

		// Checks in catch blocks
		$old_catch = $stackPtr;
		while (($catch = $phpcsFile->findNext(T_CATCH, ($old_catch + 1))) !== false)
		{
			$old_catch = $catch;

			$caught_class_name_start = $phpcsFile->findNext(array(T_NS_SEPARATOR, T_STRING), $catch + 1);
			$caught_class_name_end = $phpcsFile->findNext($find, $caught_class_name_start + 1, null, true);

			$caught_class_name = trim($phpcsFile->getTokensAsString($caught_class_name_start, ($caught_class_name_end - $caught_class_name_start)));

			$ok = $this->check($phpcsFile, $caught_class_name, $class_name_full, $class_name_short, $catch) ? true : $ok;
		}

		if (!$ok)
		{
			$error = 'There must not be unused USE statements.';
			$phpcsFile->addError($error, $stackPtr, 'Unused');
		}
	}

	/**
	* Check if this use statement is part of the namespace block.
	*
	* @param File $phpcsFile The file being scanned.
	* @param int                  $stackPtr  The position of the current token in
	*                                        the stack passed in $tokens.
	*
	* @return bool
	*/
	private function should_ignore_use(File $phpcsFile, $stackPtr)
	{
		$tokens = $phpcsFile->getTokens();

		// Ignore USE keywords inside closures.
		$next = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
		if ($tokens[$next]['code'] === T_OPEN_PARENTHESIS)
		{
			return true;
		}

		// Ignore USE keywords for traits.
		if ($phpcsFile->hasCondition($stackPtr, array(T_CLASS, T_TRAIT)) === true)
		{
			return true;
		}

		return false;

	}

	/**
	 * @param File $phpcsFile
	 * @param int $field
	 * @param array $tokens
	 * @param string $class_name_full
	 * @param string $class_name_short
	 * @param bool $ok
	 *
	 * @return bool
	 */
	private function checkDocblock(File $phpcsFile, $comment_end, $tokens, $class_name_full, $class_name_short)
	{
		$ok = false;

		$comment_start = $tokens[$comment_end]['comment_opener'];
		foreach ($tokens[$comment_start]['comment_tags'] as $tag)
		{
			if (!in_array($tokens[$tag]['content'], array('@param', '@var', '@return', '@throws'), true))
			{
				continue;
			}

			$classes = $tokens[($tag + 2)]['content'];
			$space = strpos($classes, ' ');
			if ($space !== false)
			{
				$classes = substr($classes, 0, $space);
			}

			$tab = strpos($classes, "\t");
			if ($tab !== false)
			{
				$classes = substr($classes, 0, $tab);
			}

			$classes = explode('|', str_replace('[]', '', $classes));
			foreach ($classes as $class)
			{
				$ok = $this->check($phpcsFile, $class, $class_name_full, $class_name_short, $tokens[$tag + 2]['line']) ? true : $ok;
			}
		}

		return $ok;
	}
}