comparison vendor/drupal/coder/coder_sniffer/Drupal/Sniffs/Classes/ClassCreateInstanceSniff.php @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents 4c8ae668cc8c
children
comparison
equal deleted inserted replaced
16:c2387f117808 17:129ea1e6d783
4 * 4 *
5 * @category PHP 5 * @category PHP
6 * @package PHP_CodeSniffer 6 * @package PHP_CodeSniffer
7 * @link http://pear.php.net/package/PHP_CodeSniffer 7 * @link http://pear.php.net/package/PHP_CodeSniffer
8 */ 8 */
9
10 namespace Drupal\Sniffs\Classes;
11
12 use PHP_CodeSniffer\Files\File;
13 use PHP_CodeSniffer\Sniffs\Sniff;
14 use PHP_CodeSniffer\Util\Tokens;
9 15
10 /** 16 /**
11 * Class create instance Test. 17 * Class create instance Test.
12 * 18 *
13 * Checks the declaration of the class is correct. 19 * Checks the declaration of the class is correct.
14 * 20 *
15 * @category PHP 21 * @category PHP
16 * @package PHP_CodeSniffer 22 * @package PHP_CodeSniffer
17 * @link http://pear.php.net/package/PHP_CodeSniffer 23 * @link http://pear.php.net/package/PHP_CodeSniffer
18 */ 24 */
19 class Drupal_Sniffs_Classes_ClassCreateInstanceSniff implements PHP_CodeSniffer_Sniff 25 class ClassCreateInstanceSniff implements Sniff
20 { 26 {
21 27
22 28
23 /** 29 /**
24 * Returns an array of tokens this test wants to listen for. 30 * Returns an array of tokens this test wants to listen for.
33 39
34 40
35 /** 41 /**
36 * Processes this test, when one of its tokens is encountered. 42 * Processes this test, when one of its tokens is encountered.
37 * 43 *
38 * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. 44 * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
39 * @param int $stackPtr The position of the current token in the 45 * @param int $stackPtr The position of the current token in the
40 * stack passed in $tokens. 46 * stack passed in $tokens.
41 * 47 *
42 * @return void 48 * @return void
43 */ 49 */
44 public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) 50 public function process(File $phpcsFile, $stackPtr)
45 { 51 {
46 $tokens = $phpcsFile->getTokens(); 52 $tokens = $phpcsFile->getTokens();
47 53
48 $commaOrColon = $phpcsFile->findNext([T_SEMICOLON, T_COLON, T_COMMA], ($stackPtr + 1)); 54 $commaOrColon = $phpcsFile->findNext([T_SEMICOLON, T_COLON, T_COMMA], ($stackPtr + 1));
49 if ($commaOrColon === false) { 55 if ($commaOrColon === false) {
54 // Search for an opening parenthesis in the current statement until the 60 // Search for an opening parenthesis in the current statement until the
55 // next semicolon or comma. 61 // next semicolon or comma.
56 $nextParenthesis = $phpcsFile->findNext(T_OPEN_PARENTHESIS, ($stackPtr + 1), $commaOrColon); 62 $nextParenthesis = $phpcsFile->findNext(T_OPEN_PARENTHESIS, ($stackPtr + 1), $commaOrColon);
57 if ($nextParenthesis === false) { 63 if ($nextParenthesis === false) {
58 $error = 'Calling class constructors must always include parentheses'; 64 $error = 'Calling class constructors must always include parentheses';
59 $constructor = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 1), null, true, null, true); 65 $constructor = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true, null, true);
60 // We can invoke the fixer if we know this is a static constructor 66 // We can invoke the fixer if we know this is a static constructor
61 // function call or constructor calls with namespaces, example 67 // function call or constructor calls with namespaces, example
62 // "new \DOMDocument;" or constructor with class names in variables 68 // "new \DOMDocument;" or constructor with class names in variables
63 // "new $controller;". 69 // "new $controller;".
64 if ($tokens[$constructor]['code'] === T_STRING 70 if ($tokens[$constructor]['code'] === T_STRING
68 ) { 74 ) {
69 // Scan to the end of possible string\namespace parts. 75 // Scan to the end of possible string\namespace parts.
70 $nextConstructorPart = $constructor; 76 $nextConstructorPart = $constructor;
71 while (true) { 77 while (true) {
72 $nextConstructorPart = $phpcsFile->findNext( 78 $nextConstructorPart = $phpcsFile->findNext(
73 PHP_CodeSniffer_Tokens::$emptyTokens, 79 Tokens::$emptyTokens,
74 ($nextConstructorPart + 1), 80 ($nextConstructorPart + 1),
75 null, 81 null,
76 true, 82 true,
77 null, 83 null,
78 true 84 true