|
| 1 | +<?php // phpcs:disable |
| 2 | + |
| 3 | +namespace Inpsyde\Sniffs\CodeQuality; |
| 4 | + |
| 5 | +use Inpsyde\PhpcsHelpers; |
| 6 | +use PHP_CodeSniffer\Files\File; |
| 7 | +use PHP_CodeSniffer\Sniffs\Sniff; |
| 8 | + |
| 9 | +class VariablesNameSniff implements Sniff |
| 10 | +{ |
| 11 | + const GLOBALS = [ |
| 12 | + '$_GET', |
| 13 | + '$_POST', |
| 14 | + '$_REQUEST', |
| 15 | + '$_SERVER', |
| 16 | + '$_COOKIE', |
| 17 | + '$_FILES', |
| 18 | + '$_SESSION', |
| 19 | + '$_ENV', |
| 20 | + '$GLOBALS', |
| 21 | + ]; |
| 22 | + |
| 23 | + const WP_GLOBALS = [ |
| 24 | + '$is_iphone', |
| 25 | + '$is_chrome', |
| 26 | + '$is_safari', |
| 27 | + '$is_NS4', |
| 28 | + '$is_opera', |
| 29 | + '$is_macIE', |
| 30 | + '$is_winIE', |
| 31 | + '$is_gecko', |
| 32 | + '$is_lynx', |
| 33 | + '$is_IE', |
| 34 | + '$is_edge', |
| 35 | + '$is_apache', |
| 36 | + '$is_IIS', |
| 37 | + '$is_iis7', |
| 38 | + '$tinymce_version', |
| 39 | + '$manifest_version', |
| 40 | + '$required_php_version', |
| 41 | + '$required_mysql_version', |
| 42 | + '$super_admins', |
| 43 | + ]; |
| 44 | + |
| 45 | + /** |
| 46 | + * @var bool |
| 47 | + */ |
| 48 | + public $checkType = 'camelCase'; |
| 49 | + |
| 50 | + /** |
| 51 | + * @var string[] |
| 52 | + */ |
| 53 | + public $ignoredNames = []; |
| 54 | + |
| 55 | + /** |
| 56 | + * @var bool |
| 57 | + */ |
| 58 | + public $ignoreLocalVars = false; |
| 59 | + |
| 60 | + /** |
| 61 | + * @var bool |
| 62 | + */ |
| 63 | + public $ignoreProperties = false; |
| 64 | + |
| 65 | + /** |
| 66 | + * @inheritdoc |
| 67 | + */ |
| 68 | + public function register() |
| 69 | + { |
| 70 | + return [ |
| 71 | + T_VARIABLE |
| 72 | + ]; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * @inheritdoc |
| 77 | + */ |
| 78 | + public function process(File $phpcsFile, $stackPtr) |
| 79 | + { |
| 80 | + $ignored = $this->allIgnored(); |
| 81 | + $name = $phpcsFile->getTokens()[$stackPtr]['content']; |
| 82 | + |
| 83 | + if (in_array($name, $ignored, true) || strpos($name, '$wp_') === 0) { |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + $isCamelCase = $this->checkType() === 'camelCase'; |
| 88 | + |
| 89 | + $valid = $isCamelCase ? $this->checkCamelCase($name) : $this->checkSnakeCase($name); |
| 90 | + if ($valid) { |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + $isProperty = PhpcsHelpers::variableIsProperty($phpcsFile, $stackPtr); |
| 95 | + |
| 96 | + if (($isProperty && $this->arePropertiesIgnored()) |
| 97 | + || (!$isProperty && $this->areVariablesIgnored()) |
| 98 | + ) { |
| 99 | + return; |
| 100 | + } |
| 101 | + |
| 102 | + $phpcsFile->addWarning( |
| 103 | + sprintf( |
| 104 | + '"%s" should be used for variable names.', |
| 105 | + $isCamelCase ? '$camelCase' : '$snake_case' |
| 106 | + ), |
| 107 | + $stackPtr, |
| 108 | + $isCamelCase ? 'SnakeCaseVar' : 'CamelCaseVar' |
| 109 | + ); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * @return string |
| 114 | + */ |
| 115 | + private function checkType(): string |
| 116 | + { |
| 117 | + if (!is_string($this->checkType)) { |
| 118 | + return 'camelCase'; |
| 119 | + } |
| 120 | + |
| 121 | + $type = strtolower(trim($this->checkType)); |
| 122 | + if (in_array($type, ['camelcase', 'snake_case'], true)) { |
| 123 | + return $type === 'camelcase' ? 'camelCase' : 'snake_case'; |
| 124 | + } |
| 125 | + |
| 126 | + return 'camelCase'; |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * @return bool |
| 131 | + */ |
| 132 | + private function arePropertiesIgnored(): bool |
| 133 | + { |
| 134 | + return (bool) filter_var($this->ignoreProperties, FILTER_VALIDATE_BOOLEAN); |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * @return bool |
| 139 | + */ |
| 140 | + private function areVariablesIgnored(): bool |
| 141 | + { |
| 142 | + return (bool) filter_var($this->ignoreLocalVars, FILTER_VALIDATE_BOOLEAN); |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * @param string $name |
| 147 | + * @return bool |
| 148 | + */ |
| 149 | + private function checkCamelCase(string $name): bool |
| 150 | + { |
| 151 | + return preg_match('~^\$[a-z]+(?:[a-zA-Z0-9]+)?$~', $name) |
| 152 | + && ! preg_match('~[A-Z]{2,}~', $name); |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * @param string $name |
| 157 | + * @return bool |
| 158 | + */ |
| 159 | + private function checkSnakeCase(string $name): bool |
| 160 | + { |
| 161 | + return (bool)preg_match('~^\$[a-z]+(?:[a-z0-9_]+)?$~', $name); |
| 162 | + } |
| 163 | + |
| 164 | + |
| 165 | + /** |
| 166 | + * @return array |
| 167 | + */ |
| 168 | + private function allIgnored(): array |
| 169 | + { |
| 170 | + if (is_string($this->ignoredNames)) { |
| 171 | + $this->ignoredNames = explode(',', $this->ignoredNames); |
| 172 | + } |
| 173 | + |
| 174 | + if (!is_array($this->ignoredNames)) { |
| 175 | + $this->ignoredNames = []; |
| 176 | + } |
| 177 | + |
| 178 | + $normalized = []; |
| 179 | + foreach ($this->ignoredNames as $name) { |
| 180 | + if (is_string($name)) { |
| 181 | + $normalized[] = '$' . ltrim(trim($name), '$'); |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + $this->ignoredNames = $normalized; |
| 186 | + |
| 187 | + return array_merge($normalized, self::GLOBALS, self::WP_GLOBALS); |
| 188 | + } |
| 189 | +} |
0 commit comments