comparison vendor/drupal/coder/coder_sniffer/Drupal/Sniffs/Classes/UseLeadingBackslashSniff.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 129ea1e6d783
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2 /**
3 * Drupal_Sniffs_Classes_UseLeadingBackslashSniff.
4 *
5 * @category PHP
6 * @package PHP_CodeSniffer
7 * @link http://pear.php.net/package/PHP_CodeSniffer
8 */
9
10 /**
11 * Use statements to import classes must not begin with "\".
12 *
13 * @category PHP
14 * @package PHP_CodeSniffer
15 * @link http://pear.php.net/package/PHP_CodeSniffer
16 */
17 class Drupal_Sniffs_Classes_UseLeadingBackslashSniff implements PHP_CodeSniffer_Sniff
18 {
19
20
21 /**
22 * Returns an array of tokens this test wants to listen for.
23 *
24 * @return array
25 */
26 public function register()
27 {
28 return array(T_USE);
29
30 }//end register()
31
32
33 /**
34 * Processes this test, when one of its tokens is encountered.
35 *
36 * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
37 * @param int $stackPtr The position of the current token in
38 * the stack passed in $tokens.
39 *
40 * @return void
41 */
42 public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
43 {
44 $tokens = $phpcsFile->getTokens();
45
46 // Only check use statements in the global scope.
47 if (empty($tokens[$stackPtr]['conditions']) === false) {
48 return;
49 }
50
51 $startPtr = $phpcsFile->findNext(
52 PHP_CodeSniffer_Tokens::$emptyTokens,
53 ($stackPtr + 1),
54 null,
55 true
56 );
57
58 if ($startPtr !== null && $tokens[$startPtr]['code'] === T_NS_SEPARATOR) {
59 $error = 'When importing a class with "use", do not include a leading \\';
60 $fix = $phpcsFile->addFixableError($error, $startPtr, 'SeparatorStart');
61 if ($fix === true) {
62 $phpcsFile->fixer->replaceToken($startPtr, '');
63 }
64 }
65
66 }//end process()
67
68
69 }//end class