PluginProbe
MLSImport: IDX Plugin & MLS Plugin for Real Estate Listings / 5.8.4
MLSImport: IDX Plugin & MLS Plugin for Real Estate Listings v5.8.4
7.2.2 7.2.1 7.2 7.1.2 7.1.1 7.1 7.0.4 7.0.6 7.0.7 6.3.8 6.3.7 6.3.6 6.3.5 6.3.4 6.3.3 6.3.1 trunk 5.7.3 5.7.5 5.8.1 5.8.2 5.8.3 5.8.4 5.8.6 6.0.4 All 37 releases
mlsimport / vendor / phpcsstandards / phpcsextra / Universal / Sniffs / Arrays / DisallowShortArraySyntaxSniff.php

DisallowShortArraySyntaxSniff.php in MLSImport: IDX Plugin & MLS Plugin for Real Estate Listings 5.8.4, at vendor/phpcsstandards/phpcsextra/Universal/Sniffs/Arrays/DisallowShortArraySyntaxSniff.php

90 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * PHPCSExtra, a collection of sniffs and standards for use with PHP_CodeSniffer.
4 *
5 * @package PHPCSExtra
6 * @copyright 2020 PHPCSExtra Contributors
7 * @license https://opensource.org/licenses/LGPL-3.0 LGPL3
8 * @link https://github.com/PHPCSStandards/PHPCSExtra
9 */
10
11 namespace PHPCSExtra\Universal\Sniffs\Arrays;
12
13 use PHP_CodeSniffer\Files\File;
14 use PHP_CodeSniffer\Sniffs\Sniff;
15 use PHPCSUtils\Tokens\Collections;
16 use PHPCSUtils\Utils\Arrays;
17
18 /**
19 * Disallow the use of the short array syntax.
20 *
21 * Improved version of the upstream `Generic.Arrays.DisallowShortArraySyntax` sniff which does
22 * not account for short lists and because of this can cause parse errors when auto-fixing.
23 *
24 * Other related sniffs:
25 * - `Generic.Arrays.DisallowLongArraySyntax` Forbids the use of the long array syntax.
26 *
27 * @since 1.0.0 This sniff is loosely based on and inspired by the upstream
28 * `Generic.Arrays.DisallowShortArraySyntax` sniff.
29 */
30 final class DisallowShortArraySyntaxSniff implements Sniff
31 {
32
33 /**
34 * The phrase to use for the metric recorded by this sniff.
35 *
36 * @var string
37 */
38 const METRIC_NAME = 'Short array syntax used';
39
40 /**
41 * Registers the tokens that this sniff wants to listen for.
42 *
43 * @since 1.0.0
44 *
45 * @return array<int|string>
46 */
47 public function register()
48 {
49 return Collections::arrayOpenTokensBC();
50 }
51
52 /**
53 * Processes this test, when one of its tokens is encountered.
54 *
55 * @since 1.0.0
56 *
57 * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
58 * @param int $stackPtr The position of the current token
59 * in the stack passed in $tokens.
60 *
61 * @return void
62 */
63 public function process(File $phpcsFile, $stackPtr)
64 {
65 $tokens = $phpcsFile->getTokens();
66
67 if ($tokens[$stackPtr]['code'] === \T_ARRAY) {
68 $phpcsFile->recordMetric($stackPtr, self::METRIC_NAME, 'no');
69 return;
70 }
71
72 if (Arrays::isShortArray($phpcsFile, $stackPtr) === false) {
73 // Square brackets, but not a short array. Probably short list or real square brackets.
74 return;
75 }
76
77 $phpcsFile->recordMetric($stackPtr, self::METRIC_NAME, 'yes');
78
79 $error = 'Short array syntax is not allowed';
80 $fix = $phpcsFile->addFixableError($error, $stackPtr, 'Found');
81
82 if ($fix === true) {
83 $phpcsFile->fixer->beginChangeset();
84 $phpcsFile->fixer->replaceToken($tokens[$stackPtr]['bracket_opener'], 'array(');
85 $phpcsFile->fixer->replaceToken($tokens[$stackPtr]['bracket_closer'], ')');
86 $phpcsFile->fixer->endChangeset();
87 }
88 }
89 }
90