@@ -16,17 +16,17 @@ |
||
16 | 16 | * |
17 | 17 | * @return bool |
18 | 18 | */ |
19 | - protected function shouldIgnoreUse(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { |
|
19 | + protected function shouldIgnoreUse( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) { |
|
20 | 20 | $tokens = $phpcsFile->getTokens(); |
21 | 21 | |
22 | 22 | // Ignore USE keywords inside closures. |
23 | - $next = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true); |
|
24 | - if ($tokens[$next]['code'] === T_OPEN_PARENTHESIS) { |
|
23 | + $next = $phpcsFile->findNext( T_WHITESPACE, ( $stackPtr + 1 ), null, true ); |
|
24 | + if ( $tokens[ $next ][ 'code' ] === T_OPEN_PARENTHESIS ) { |
|
25 | 25 | return true; |
26 | 26 | } |
27 | 27 | |
28 | 28 | // Ignore USE keywords for traits. |
29 | - if ($phpcsFile->hasCondition($stackPtr, [T_CLASS, T_TRAIT]) === true) { |
|
29 | + if ( $phpcsFile->hasCondition( $stackPtr, [ T_CLASS, T_TRAIT ] ) === true ) { |
|
30 | 30 | return true; |
31 | 31 | } |
32 | 32 |
@@ -28,47 +28,47 @@ discard block |
||
28 | 28 | * @param array $argv |
29 | 29 | * @throws \Exception |
30 | 30 | */ |
31 | - public function __construct($argv) { |
|
31 | + public function __construct( $argv ) { |
|
32 | 32 | |
33 | - $file = !empty($argv[1]) ? $argv[1] : null; |
|
34 | - if (!$file || !file_exists($file)) { |
|
35 | - throw new Exception('Please provide a valid file.'); |
|
33 | + $file = ! empty( $argv[ 1 ] ) ? $argv[ 1 ] : null; |
|
34 | + if ( ! $file || ! file_exists( $file ) ) { |
|
35 | + throw new Exception( 'Please provide a valid file.' ); |
|
36 | 36 | } |
37 | - $file = realpath($file); |
|
37 | + $file = realpath( $file ); |
|
38 | 38 | |
39 | - $this->root = dirname(dirname(__DIR__)) . DIRECTORY_SEPARATOR; |
|
39 | + $this->root = dirname( dirname( __DIR__ ) ) . DIRECTORY_SEPARATOR; |
|
40 | 40 | $this->path = $file; |
41 | - $this->verbose = !empty($argv[2]) && in_array($argv[2], ['--verbose', '-v']); |
|
41 | + $this->verbose = ! empty( $argv[ 2 ] ) && in_array( $argv[ 2 ], [ '--verbose', '-v' ] ); |
|
42 | 42 | } |
43 | 43 | |
44 | 44 | /** |
45 | 45 | * @return void |
46 | 46 | */ |
47 | 47 | public function tokenize() { |
48 | - $_SERVER['argv'] = []; |
|
49 | - $_SERVER['argv'][] = '--encoding=utf8'; |
|
48 | + $_SERVER[ 'argv' ] = [ ]; |
|
49 | + $_SERVER[ 'argv' ][ ] = '--encoding=utf8'; |
|
50 | 50 | |
51 | 51 | $standard = $this->root . self::STANDARD; |
52 | - $_SERVER['argv'][] = '--standard=' . $standard; |
|
53 | - |
|
54 | - $_SERVER['argv'][] = $this->path; |
|
55 | - $_SERVER['argc'] = count($_SERVER['argv']); |
|
56 | - $res = []; |
|
57 | - $tokens = $this->_getTokens($this->path); |
|
58 | - $array = file($this->path); |
|
59 | - foreach ($array as $key => $row) { |
|
60 | - $res[] = rtrim($row); |
|
61 | - $tokenStrings = $this->_tokenize($key + 1, $tokens); |
|
62 | - if ($tokenStrings) { |
|
63 | - foreach ($tokenStrings as $string) { |
|
64 | - $res[] = '// ' . $string; |
|
52 | + $_SERVER[ 'argv' ][ ] = '--standard=' . $standard; |
|
53 | + |
|
54 | + $_SERVER[ 'argv' ][ ] = $this->path; |
|
55 | + $_SERVER[ 'argc' ] = count( $_SERVER[ 'argv' ] ); |
|
56 | + $res = [ ]; |
|
57 | + $tokens = $this->_getTokens( $this->path ); |
|
58 | + $array = file( $this->path ); |
|
59 | + foreach ( $array as $key => $row ) { |
|
60 | + $res[ ] = rtrim( $row ); |
|
61 | + $tokenStrings = $this->_tokenize( $key + 1, $tokens ); |
|
62 | + if ( $tokenStrings ) { |
|
63 | + foreach ( $tokenStrings as $string ) { |
|
64 | + $res[ ] = '// ' . $string; |
|
65 | 65 | } |
66 | 66 | } |
67 | 67 | } |
68 | - $content = implode(PHP_EOL, $res); |
|
68 | + $content = implode( PHP_EOL, $res ); |
|
69 | 69 | echo 'Tokenizing: ' . $this->path . PHP_EOL; |
70 | - $newPath = dirname($this->path) . DIRECTORY_SEPARATOR . pathinfo($this->path, PATHINFO_FILENAME) . '.tokens.' . pathinfo($this->path, PATHINFO_EXTENSION); |
|
71 | - file_put_contents($newPath, $content); |
|
70 | + $newPath = dirname( $this->path ) . DIRECTORY_SEPARATOR . pathinfo( $this->path, PATHINFO_FILENAME ) . '.tokens.' . pathinfo( $this->path, PATHINFO_EXTENSION ); |
|
71 | + file_put_contents( $newPath, $content ); |
|
72 | 72 | echo 'Token file: ' . $newPath . PHP_EOL; |
73 | 73 | } |
74 | 74 | |
@@ -76,10 +76,10 @@ discard block |
||
76 | 76 | * @param string $path Path |
77 | 77 | * @return array Tokens |
78 | 78 | */ |
79 | - protected function _getTokens($path) { |
|
79 | + protected function _getTokens( $path ) { |
|
80 | 80 | $phpcs = new PHP_CodeSniffer(); |
81 | - $phpcs->process([], $this->root . self::STANDARD, []); |
|
82 | - $file = $phpcs->processFile($path); |
|
81 | + $phpcs->process( [ ], $this->root . self::STANDARD, [ ] ); |
|
82 | + $file = $phpcs->processFile( $path ); |
|
83 | 83 | $file->start(); |
84 | 84 | return $file->getTokens(); |
85 | 85 | } |
@@ -89,43 +89,43 @@ discard block |
||
89 | 89 | * @param array $tokens Tokens array |
90 | 90 | * @return array |
91 | 91 | */ |
92 | - protected function _tokenize($row, $tokens) { |
|
93 | - $pieces = []; |
|
94 | - foreach ($tokens as $key => $token) { |
|
95 | - if ($token['line'] > $row) { |
|
92 | + protected function _tokenize( $row, $tokens ) { |
|
93 | + $pieces = [ ]; |
|
94 | + foreach ( $tokens as $key => $token ) { |
|
95 | + if ( $token[ 'line' ] > $row ) { |
|
96 | 96 | break; |
97 | 97 | } |
98 | - if ($token['line'] < $row) { |
|
98 | + if ( $token[ 'line' ] < $row ) { |
|
99 | 99 | continue; |
100 | 100 | } |
101 | - if ($this->verbose) { |
|
102 | - $type = $token['type']; |
|
103 | - $content = $token['content']; |
|
104 | - $content = '`' . str_replace(["\r\n", "\n", "\r", "\t"], ['\r\n', '\n', '\r', '\t'], $content) . '`'; |
|
105 | - |
|
106 | - unset($token['type']); |
|
107 | - unset($token['content']); |
|
108 | - $token['content'] = $content; |
|
109 | - |
|
110 | - $tokenList = []; |
|
111 | - foreach ($token as $k => $v) { |
|
112 | - if (is_array($v)) { |
|
113 | - if (empty($v)) { |
|
101 | + if ( $this->verbose ) { |
|
102 | + $type = $token[ 'type' ]; |
|
103 | + $content = $token[ 'content' ]; |
|
104 | + $content = '`' . str_replace( [ "\r\n", "\n", "\r", "\t" ], [ '\r\n', '\n', '\r', '\t' ], $content ) . '`'; |
|
105 | + |
|
106 | + unset( $token[ 'type' ] ); |
|
107 | + unset( $token[ 'content' ] ); |
|
108 | + $token[ 'content' ] = $content; |
|
109 | + |
|
110 | + $tokenList = [ ]; |
|
111 | + foreach ( $token as $k => $v ) { |
|
112 | + if ( is_array( $v ) ) { |
|
113 | + if ( empty( $v ) ) { |
|
114 | 114 | continue; |
115 | 115 | } |
116 | - $v = json_encode($v); |
|
116 | + $v = json_encode( $v ); |
|
117 | 117 | } |
118 | - $tokenList[] = $k . '=' . $v; |
|
118 | + $tokenList[ ] = $k . '=' . $v; |
|
119 | 119 | } |
120 | - $pieces[] = $type . ' (' . $key . ') ' . implode(', ', $tokenList); |
|
120 | + $pieces[ ] = $type . ' (' . $key . ') ' . implode( ', ', $tokenList ); |
|
121 | 121 | } else { |
122 | - $pieces[] = $token['type']; |
|
122 | + $pieces[ ] = $token[ 'type' ]; |
|
123 | 123 | } |
124 | 124 | } |
125 | - if ($this->verbose) { |
|
125 | + if ( $this->verbose ) { |
|
126 | 126 | return $pieces; |
127 | 127 | } |
128 | - return [implode(' ', $pieces)]; |
|
128 | + return [ implode( ' ', $pieces ) ]; |
|
129 | 129 | } |
130 | 130 | |
131 | 131 | } |
@@ -17,34 +17,34 @@ |
||
17 | 17 | * @inheritDoc |
18 | 18 | */ |
19 | 19 | public function register() { |
20 | - return [T_SEMICOLON]; |
|
20 | + return [ T_SEMICOLON ]; |
|
21 | 21 | } |
22 | 22 | |
23 | 23 | /** |
24 | 24 | * @inheritDoc |
25 | 25 | */ |
26 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { |
|
26 | + public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) { |
|
27 | 27 | $tokens = $phpcsFile->getTokens(); |
28 | 28 | |
29 | - $previousIndex = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr - 1), null, true); |
|
30 | - if (!$previousIndex || $tokens[$previousIndex]['code'] !== T_SEMICOLON) { |
|
29 | + $previousIndex = $phpcsFile->findPrevious( PHP_CodeSniffer_Tokens::$emptyTokens, ( $stackPtr - 1 ), null, true ); |
|
30 | + if ( ! $previousIndex || $tokens[ $previousIndex ][ 'code' ] !== T_SEMICOLON ) { |
|
31 | 31 | return; |
32 | 32 | } |
33 | 33 | |
34 | - $possibleForIndex = $phpcsFile->findPrevious(T_FOR, ($previousIndex - 1)); |
|
35 | - if ($possibleForIndex && $tokens[$possibleForIndex]['parenthesis_closer'] > $stackPtr) { |
|
34 | + $possibleForIndex = $phpcsFile->findPrevious( T_FOR, ( $previousIndex - 1 ) ); |
|
35 | + if ( $possibleForIndex && $tokens[ $possibleForIndex ][ 'parenthesis_closer' ] > $stackPtr ) { |
|
36 | 36 | return; |
37 | 37 | } |
38 | 38 | |
39 | 39 | $error = 'Double semicolon found'; |
40 | - $fix = $phpcsFile->addFixableError($error, $stackPtr); |
|
41 | - if ($fix) { |
|
40 | + $fix = $phpcsFile->addFixableError( $error, $stackPtr ); |
|
41 | + if ( $fix ) { |
|
42 | 42 | $phpcsFile->fixer->beginChangeset(); |
43 | 43 | |
44 | - $phpcsFile->fixer->replaceToken($stackPtr, ''); |
|
45 | - for ($i = $stackPtr; $i > $previousIndex; --$i) { |
|
46 | - if ($tokens[$i]['code'] === T_WHITESPACE) { |
|
47 | - $phpcsFile->fixer->replaceToken($i, ''); |
|
44 | + $phpcsFile->fixer->replaceToken( $stackPtr, '' ); |
|
45 | + for ( $i = $stackPtr; $i > $previousIndex; --$i ) { |
|
46 | + if ( $tokens[ $i ][ 'code' ] === T_WHITESPACE ) { |
|
47 | + $phpcsFile->fixer->replaceToken( $i, '' ); |
|
48 | 48 | } |
49 | 49 | } |
50 | 50 |
@@ -40,15 +40,15 @@ |
||
40 | 40 | /** |
41 | 41 | * @inheritDoc |
42 | 42 | */ |
43 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { |
|
43 | + public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) { |
|
44 | 44 | $tokens = $phpcsFile->getTokens(); |
45 | - $openTag = $tokens[$stackPtr]; |
|
45 | + $openTag = $tokens[ $stackPtr ]; |
|
46 | 46 | |
47 | - $content = trim($openTag['content']); |
|
48 | - if ($content === '<?') { |
|
47 | + $content = trim( $openTag[ 'content' ] ); |
|
48 | + if ( $content === '<?' ) { |
|
49 | 49 | $error = 'Short PHP opening tag used; expected "<?php" but found "%s"'; |
50 | - $data = [$content]; |
|
51 | - $phpcsFile->addError($error, $stackPtr, 'Found', $data); |
|
50 | + $data = [ $content ]; |
|
51 | + $phpcsFile->addError( $error, $stackPtr, 'Found', $data ); |
|
52 | 52 | } |
53 | 53 | } |
54 | 54 |
@@ -24,40 +24,40 @@ |
||
24 | 24 | * @inheritDoc |
25 | 25 | */ |
26 | 26 | public function register() { |
27 | - return [T_BOOL_CAST, T_INT_CAST, T_BOOLEAN_NOT]; |
|
27 | + return [ T_BOOL_CAST, T_INT_CAST, T_BOOLEAN_NOT ]; |
|
28 | 28 | } |
29 | 29 | |
30 | 30 | /** |
31 | 31 | * @inheritDoc |
32 | 32 | */ |
33 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { |
|
33 | + public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) { |
|
34 | 34 | $tokens = $phpcsFile->getTokens(); |
35 | 35 | |
36 | - if ($tokens[$stackPtr]['content'] === '!') { |
|
37 | - $prevIndex = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true); |
|
38 | - if ($tokens[$prevIndex]['content'] !== '!') { |
|
36 | + if ( $tokens[ $stackPtr ][ 'content' ] === '!' ) { |
|
37 | + $prevIndex = $phpcsFile->findPrevious( T_WHITESPACE, ( $stackPtr - 1 ), null, true ); |
|
38 | + if ( $tokens[ $prevIndex ][ 'content' ] !== '!' ) { |
|
39 | 39 | return; |
40 | 40 | } |
41 | 41 | |
42 | - $fix = $phpcsFile->addFixableError('`!!` cast not allowed, use `(bool)`', $stackPtr); |
|
43 | - if ($fix) { |
|
44 | - $phpcsFile->fixer->replaceToken($prevIndex, ''); |
|
45 | - $phpcsFile->fixer->replaceToken($stackPtr, '(bool)'); |
|
42 | + $fix = $phpcsFile->addFixableError( '`!!` cast not allowed, use `(bool)`', $stackPtr ); |
|
43 | + if ( $fix ) { |
|
44 | + $phpcsFile->fixer->replaceToken( $prevIndex, '' ); |
|
45 | + $phpcsFile->fixer->replaceToken( $stackPtr, '(bool)' ); |
|
46 | 46 | } |
47 | 47 | |
48 | 48 | return; |
49 | 49 | } |
50 | 50 | |
51 | - $content = $tokens[$stackPtr]['content']; |
|
52 | - $key = strtolower($content); |
|
51 | + $content = $tokens[ $stackPtr ][ 'content' ]; |
|
52 | + $key = strtolower( $content ); |
|
53 | 53 | |
54 | - if (!isset(self::$matching[$key])) { |
|
54 | + if ( ! isset( self::$matching[ $key ] ) ) { |
|
55 | 55 | return; |
56 | 56 | } |
57 | 57 | |
58 | - $fix = $phpcsFile->addFixableError($content . ' found, expected ' . self::$matching[$key], $stackPtr); |
|
59 | - if ($fix) { |
|
60 | - $phpcsFile->fixer->replaceToken($stackPtr, self::$matching[$key]); |
|
58 | + $fix = $phpcsFile->addFixableError( $content . ' found, expected ' . self::$matching[ $key ], $stackPtr ); |
|
59 | + if ( $fix ) { |
|
60 | + $phpcsFile->fixer->replaceToken( $stackPtr, self::$matching[ $key ] ); |
|
61 | 61 | } |
62 | 62 | } |
63 | 63 |
@@ -19,27 +19,27 @@ discard block |
||
19 | 19 | * @inheritDoc |
20 | 20 | */ |
21 | 21 | public function register() { |
22 | - return [T_LIST]; |
|
22 | + return [ T_LIST ]; |
|
23 | 23 | } |
24 | 24 | |
25 | 25 | /** |
26 | 26 | * @inheritDoc |
27 | 27 | */ |
28 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { |
|
28 | + public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) { |
|
29 | 29 | $tokens = $phpcsFile->getTokens(); |
30 | 30 | |
31 | - $openIndex = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, $stackPtr + 1, null, true); |
|
32 | - $closeIndex = $tokens[$openIndex]['parenthesis_closer']; |
|
31 | + $openIndex = $phpcsFile->findNext( PHP_CodeSniffer_Tokens::$emptyTokens, $stackPtr + 1, null, true ); |
|
32 | + $closeIndex = $tokens[ $openIndex ][ 'parenthesis_closer' ]; |
|
33 | 33 | |
34 | 34 | $markIndex = null; |
35 | - $prevIndex = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, $closeIndex - 1, null, true); |
|
36 | - while ($tokens[$prevIndex]['code'] === T_COMMA) { |
|
35 | + $prevIndex = $phpcsFile->findPrevious( PHP_CodeSniffer_Tokens::$emptyTokens, $closeIndex - 1, null, true ); |
|
36 | + while ( $tokens[ $prevIndex ][ 'code' ] === T_COMMA ) { |
|
37 | 37 | $markIndex = $prevIndex; |
38 | - $prevIndex = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, $prevIndex - 1, null, true); |
|
38 | + $prevIndex = $phpcsFile->findPrevious( PHP_CodeSniffer_Tokens::$emptyTokens, $prevIndex - 1, null, true ); |
|
39 | 39 | } |
40 | - if ($markIndex !== null) { |
|
41 | - $fix = $phpcsFile->addFixableError('Superflouos commas in list', $markIndex); |
|
42 | - if ($fix) { |
|
40 | + if ( $markIndex !== null ) { |
|
41 | + $fix = $phpcsFile->addFixableError( 'Superflouos commas in list', $markIndex ); |
|
42 | + if ( $fix ) { |
|
43 | 43 | $this->clearRange( |
44 | 44 | $phpcsFile, |
45 | 45 | $markIndex, |
@@ -55,9 +55,9 @@ discard block |
||
55 | 55 | * @param int $endIndex |
56 | 56 | * @return void |
57 | 57 | */ |
58 | - protected function clearRange(PHP_CodeSniffer_File $phpcsFile, $startIndex, $endIndex) { |
|
59 | - for ($i = $startIndex; $i <= $endIndex; $i++) { |
|
60 | - $phpcsFile->fixer->replaceToken($i, ''); |
|
58 | + protected function clearRange( PHP_CodeSniffer_File $phpcsFile, $startIndex, $endIndex ) { |
|
59 | + for ( $i = $startIndex; $i <= $endIndex; $i++ ) { |
|
60 | + $phpcsFile->fixer->replaceToken( $i, '' ); |
|
61 | 61 | } |
62 | 62 | } |
63 | 63 |
@@ -18,43 +18,43 @@ |
||
18 | 18 | * @inheritDoc |
19 | 19 | */ |
20 | 20 | public function register() { |
21 | - return [T_STRING]; |
|
21 | + return [ T_STRING ]; |
|
22 | 22 | } |
23 | 23 | |
24 | 24 | /** |
25 | 25 | * @inheritDoc |
26 | 26 | */ |
27 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { |
|
27 | + public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) { |
|
28 | 28 | $tokens = $phpcsFile->getTokens(); |
29 | 29 | |
30 | - $wrongTokens = [T_FUNCTION, T_OBJECT_OPERATOR, T_NEW, T_DOUBLE_COLON]; |
|
30 | + $wrongTokens = [ T_FUNCTION, T_OBJECT_OPERATOR, T_NEW, T_DOUBLE_COLON ]; |
|
31 | 31 | |
32 | - $tokenContent = $tokens[$stackPtr]['content']; |
|
33 | - if (strtolower($tokenContent) !== 'php_sapi_name') { |
|
32 | + $tokenContent = $tokens[ $stackPtr ][ 'content' ]; |
|
33 | + if ( strtolower( $tokenContent ) !== 'php_sapi_name' ) { |
|
34 | 34 | return; |
35 | 35 | } |
36 | 36 | |
37 | - $previous = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true); |
|
38 | - if (!$previous || in_array($tokens[$previous]['code'], $wrongTokens)) { |
|
37 | + $previous = $phpcsFile->findPrevious( T_WHITESPACE, ( $stackPtr - 1 ), null, true ); |
|
38 | + if ( ! $previous || in_array( $tokens[ $previous ][ 'code' ], $wrongTokens ) ) { |
|
39 | 39 | return; |
40 | 40 | } |
41 | 41 | |
42 | - $openingBrace = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true); |
|
43 | - if (!$openingBrace || $tokens[$openingBrace]['type'] !== 'T_OPEN_PARENTHESIS') { |
|
42 | + $openingBrace = $phpcsFile->findNext( T_WHITESPACE, ( $stackPtr + 1 ), null, true ); |
|
43 | + if ( ! $openingBrace || $tokens[ $openingBrace ][ 'type' ] !== 'T_OPEN_PARENTHESIS' ) { |
|
44 | 44 | return; |
45 | 45 | } |
46 | 46 | |
47 | - $closingBrace = $phpcsFile->findNext(T_WHITESPACE, ($openingBrace + 1), null, true); |
|
48 | - if (!$closingBrace || $tokens[$closingBrace]['type'] !== 'T_CLOSE_PARENTHESIS') { |
|
47 | + $closingBrace = $phpcsFile->findNext( T_WHITESPACE, ( $openingBrace + 1 ), null, true ); |
|
48 | + if ( ! $closingBrace || $tokens[ $closingBrace ][ 'type' ] !== 'T_CLOSE_PARENTHESIS' ) { |
|
49 | 49 | return; |
50 | 50 | } |
51 | 51 | |
52 | 52 | $error = $tokenContent . '() found, should be const ' . self::PHP_SAPI . '.'; |
53 | - $fix = $phpcsFile->addFixableError($error, $stackPtr); |
|
54 | - if ($fix) { |
|
55 | - $phpcsFile->fixer->replaceToken($stackPtr, self::PHP_SAPI); |
|
56 | - for ($i = $openingBrace; $i <= $closingBrace; ++$i) { |
|
57 | - $phpcsFile->fixer->replaceToken($i, ''); |
|
53 | + $fix = $phpcsFile->addFixableError( $error, $stackPtr ); |
|
54 | + if ( $fix ) { |
|
55 | + $phpcsFile->fixer->replaceToken( $stackPtr, self::PHP_SAPI ); |
|
56 | + for ( $i = $openingBrace; $i <= $closingBrace; ++$i ) { |
|
57 | + $phpcsFile->fixer->replaceToken( $i, '' ); |
|
58 | 58 | } |
59 | 59 | } |
60 | 60 | } |
@@ -38,37 +38,37 @@ |
||
38 | 38 | * @inheritDoc |
39 | 39 | */ |
40 | 40 | public function register() { |
41 | - return [T_STRING]; |
|
41 | + return [ T_STRING ]; |
|
42 | 42 | } |
43 | 43 | |
44 | 44 | /** |
45 | 45 | * @inheritDoc |
46 | 46 | */ |
47 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { |
|
47 | + public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) { |
|
48 | 48 | $tokens = $phpcsFile->getTokens(); |
49 | 49 | |
50 | - $wrongTokens = [T_FUNCTION, T_OBJECT_OPERATOR, T_NEW, T_DOUBLE_COLON]; |
|
50 | + $wrongTokens = [ T_FUNCTION, T_OBJECT_OPERATOR, T_NEW, T_DOUBLE_COLON ]; |
|
51 | 51 | |
52 | - $tokenContent = $tokens[$stackPtr]['content']; |
|
53 | - $key = strtolower($tokenContent); |
|
54 | - if (!isset(self::$matching[$key])) { |
|
52 | + $tokenContent = $tokens[ $stackPtr ][ 'content' ]; |
|
53 | + $key = strtolower( $tokenContent ); |
|
54 | + if ( ! isset( self::$matching[ $key ] ) ) { |
|
55 | 55 | return; |
56 | 56 | } |
57 | 57 | |
58 | - $previous = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true); |
|
59 | - if (!$previous || in_array($tokens[$previous]['code'], $wrongTokens)) { |
|
58 | + $previous = $phpcsFile->findPrevious( T_WHITESPACE, ( $stackPtr - 1 ), null, true ); |
|
59 | + if ( ! $previous || in_array( $tokens[ $previous ][ 'code' ], $wrongTokens ) ) { |
|
60 | 60 | return; |
61 | 61 | } |
62 | 62 | |
63 | - $openingBrace = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true); |
|
64 | - if (!$openingBrace || $tokens[$openingBrace]['type'] !== 'T_OPEN_PARENTHESIS') { |
|
63 | + $openingBrace = $phpcsFile->findNext( T_WHITESPACE, ( $stackPtr + 1 ), null, true ); |
|
64 | + if ( ! $openingBrace || $tokens[ $openingBrace ][ 'type' ] !== 'T_OPEN_PARENTHESIS' ) { |
|
65 | 65 | return; |
66 | 66 | } |
67 | 67 | |
68 | - $error = 'Function name ' . $tokenContent . '() found, should be ' . self::$matching[$key] . '().'; |
|
69 | - $fix = $phpcsFile->addFixableError($error, $stackPtr); |
|
70 | - if ($fix) { |
|
71 | - $phpcsFile->fixer->replaceToken($stackPtr, self::$matching[$key]); |
|
68 | + $error = 'Function name ' . $tokenContent . '() found, should be ' . self::$matching[ $key ] . '().'; |
|
69 | + $fix = $phpcsFile->addFixableError( $error, $stackPtr ); |
|
70 | + if ( $fix ) { |
|
71 | + $phpcsFile->fixer->replaceToken( $stackPtr, self::$matching[ $key ] ); |
|
72 | 72 | } |
73 | 73 | } |
74 | 74 |
@@ -28,33 +28,33 @@ |
||
28 | 28 | * @inheritDoc |
29 | 29 | */ |
30 | 30 | public function register() { |
31 | - return [T_CONSTANT_ENCAPSED_STRING]; |
|
31 | + return [ T_CONSTANT_ENCAPSED_STRING ]; |
|
32 | 32 | } |
33 | 33 | |
34 | 34 | /** |
35 | 35 | * @inheritDoc |
36 | 36 | */ |
37 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { |
|
37 | + public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) { |
|
38 | 38 | $tokens = $phpcsFile->getTokens(); |
39 | 39 | |
40 | 40 | // Skip for complex multiline |
41 | - $prevIndex = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, $stackPtr - 1, null, true); |
|
42 | - if ($prevIndex && $tokens[$prevIndex]['code'] === T_CONSTANT_ENCAPSED_STRING) { |
|
41 | + $prevIndex = $phpcsFile->findPrevious( PHP_CodeSniffer_Tokens::$emptyTokens, $stackPtr - 1, null, true ); |
|
42 | + if ( $prevIndex && $tokens[ $prevIndex ][ 'code' ] === T_CONSTANT_ENCAPSED_STRING ) { |
|
43 | 43 | return; |
44 | 44 | } |
45 | 45 | |
46 | - $content = $tokens[$stackPtr]['content']; |
|
47 | - if ($content[0] === '"' |
|
48 | - && strpos($content, "'") === false |
|
49 | - && strpos($content, "\n") === false |
|
46 | + $content = $tokens[ $stackPtr ][ 'content' ]; |
|
47 | + if ( $content[ 0 ] === '"' |
|
48 | + && strpos( $content, "'" ) === false |
|
49 | + && strpos( $content, "\n" ) === false |
|
50 | 50 | // regex: odd number of backslashes, not followed by double quote or dollar |
51 | - && !preg_match('/(?<!\\\\)(?:\\\\{2})*\\\\(?!["$\\\\])/', $content) |
|
51 | + && ! preg_match( '/(?<!\\\\)(?:\\\\{2})*\\\\(?!["$\\\\])/', $content ) |
|
52 | 52 | ) { |
53 | - $fix = $phpcsFile->addFixableError('Use single instead of double quotes for simple strings.', $stackPtr); |
|
54 | - if ($fix) { |
|
55 | - $content = substr($content, 1, -1); |
|
56 | - $content = str_replace(['\\"', '\\$'], ['"', '$'], $content); |
|
57 | - $phpcsFile->fixer->replaceToken($stackPtr, '\'' . $content . '\''); |
|
53 | + $fix = $phpcsFile->addFixableError( 'Use single instead of double quotes for simple strings.', $stackPtr ); |
|
54 | + if ( $fix ) { |
|
55 | + $content = substr( $content, 1, -1 ); |
|
56 | + $content = str_replace( [ '\\"', '\\$' ], [ '"', '$' ], $content ); |
|
57 | + $phpcsFile->fixer->replaceToken( $stackPtr, '\'' . $content . '\'' ); |
|
58 | 58 | } |
59 | 59 | } |
60 | 60 | } |