aboutsummaryrefslogtreecommitdiffstats
path: root/build/code_sniffer/phpbb/Sniffs/ControlStructures/OpeningParenthesisSniff.php
blob: d4ecd6cb95d88dc05b7aceb2f26055f329bd1af1 (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
<?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 there is exactly one space between the keyword and the opening
 * parenthesis of a control structures.
 */
class phpbb_Sniffs_ControlStructures_OpeningParenthesisSniff implements Sniff
{
	/**
	 * Registers the tokens that this sniff wants to listen for.
	 */
	public function register()
	{
		return array(
			T_IF,
			T_FOREACH,
			T_WHILE,
			T_FOR,
			T_SWITCH,
			T_ELSEIF,
			T_CATCH,
		);
	}

	/**
	 * Processes this test, when one of its tokens is encountered.
	 *
	 * @param File $phpcsFile The file being scanned.
	 * @param int                  $stackPtr  The position of the current token in the
	 *                                        stack passed in $tokens.
	 *
	 * @return void
	 */
	public function process(File $phpcsFile, $stackPtr)
	{
		$tokens = $phpcsFile->getTokens();

		if ($tokens[$stackPtr + 1]['content'] === '(')
		{
			$error = 'There should be exactly one space between the keyword and opening parenthesis';
			$phpcsFile->addError($error, $stackPtr, 'NoSpaceBeforeOpeningParenthesis');
		}
		else if ($tokens[$stackPtr + 1]['content'] !== ' ')
		{
			$error = 'There should be exactly one space between the keyword and opening parenthesis';
			$phpcsFile->addError($error, $stackPtr, 'IncorrectSpaceBeforeOpeningParenthesis');
		}
	}
}