|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace BestIt\Sniffs\Functions; |
| 6 | + |
| 7 | +use BestIt\Sniffs\AbstractSniff; |
| 8 | +use BestIt\Sniffs\SuppressingTrait; |
| 9 | +use SlevomatCodingStandard\Helpers\FunctionHelper; |
| 10 | +use SlevomatCodingStandard\Helpers\PropertyHelper; |
| 11 | +use SlevomatCodingStandard\Helpers\TokenHelper; |
| 12 | +use function current; |
| 13 | +use function is_int; |
| 14 | +use function preg_match; |
| 15 | +use function substr; |
| 16 | +use function trim; |
| 17 | +use function ucfirst; |
| 18 | +use const T_AS; |
| 19 | +use const T_CONST; |
| 20 | +use const T_FUNCTION; |
| 21 | +use const T_PRIVATE; |
| 22 | +use const T_PROTECTED; |
| 23 | +use const T_PUBLIC; |
| 24 | +use const T_STRING; |
| 25 | +use const T_VAR; |
| 26 | +use const T_VARIABLE; |
| 27 | + |
| 28 | +/** |
| 29 | + * We want to encourage that you use the new typed properties instead of redundant simple getter and setters. |
| 30 | + * |
| 31 | + * @author blange <[email protected]> |
| 32 | + * @package BestIt\Sniffs\Functions |
| 33 | + */ |
| 34 | +class NoSimplePropertyMethodSniff extends AbstractSniff |
| 35 | +{ |
| 36 | + use SuppressingTrait; |
| 37 | + |
| 38 | + /** |
| 39 | + * The error code for the getter. |
| 40 | + * |
| 41 | + * @var string |
| 42 | + */ |
| 43 | + public const CODE_SHOULD_USE_PROPERTY = 'ShouldUseTypedPropertyDirectly'; |
| 44 | + |
| 45 | + /** |
| 46 | + * Error message when a simple getter exists. |
| 47 | + * |
| 48 | + * @var string |
| 49 | + */ |
| 50 | + private const ERROR_GETTER_TOO_SIMPLE = 'We suggest that you use the typed property "%s" directly.'; |
| 51 | + |
| 52 | + /** |
| 53 | + * Error message when a simple getter exists. |
| 54 | + * |
| 55 | + * @var string |
| 56 | + */ |
| 57 | + private const ERROR_SETTER_TOO_SIMPLE = 'We suggest that you use the typed property "%s" directly.'; |
| 58 | + |
| 59 | + /** |
| 60 | + * Method getter for the setter methods. |
| 61 | + * |
| 62 | + * @var string |
| 63 | + */ |
| 64 | + private const METHOD_PREFIX_GETTER = 'get'; |
| 65 | + |
| 66 | + /** |
| 67 | + * Method getter for the setter methods. |
| 68 | + * |
| 69 | + * @var string |
| 70 | + */ |
| 71 | + private const METHOD_PREFIX_SETTER = 'set'; |
| 72 | + |
| 73 | + /** |
| 74 | + * Finds the position of the method token which is named like the given string. |
| 75 | + * |
| 76 | + * @param string $string |
| 77 | + * |
| 78 | + * @return int|null |
| 79 | + */ |
| 80 | + private function findMethod(string $string): ?int |
| 81 | + { |
| 82 | + $file = $this->getFile(); |
| 83 | + $searchPos = $this->getStackPos(); |
| 84 | + |
| 85 | + do { |
| 86 | + $searchPos = $file->findNext(T_FUNCTION, ++$searchPos); |
| 87 | + |
| 88 | + $nextPos = $searchPos + 1; |
| 89 | + $possibleNextName = $nextPos + 2; |
| 90 | + } while ($searchPos && !$file->findNext(T_STRING, $nextPos, $possibleNextName, false, $string)); |
| 91 | + |
| 92 | + return is_int($searchPos) ? $searchPos : null; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Returns the stack position of the relevant property if it is a typed one. |
| 97 | + * |
| 98 | + * @return int|null |
| 99 | + */ |
| 100 | + private function getExactPropertyPointer(): ?int |
| 101 | + { |
| 102 | + $asPointer = TokenHelper::findPreviousEffective($this->getFile(), $this->getStackPos() - 1); |
| 103 | + |
| 104 | + if ($this->tokens[$asPointer]['code'] === T_AS) { |
| 105 | + return null; |
| 106 | + } |
| 107 | + |
| 108 | + $propertyPointer = TokenHelper::findNext( |
| 109 | + $this->getFile(), |
| 110 | + [T_FUNCTION, T_CONST, T_VARIABLE], |
| 111 | + $this->getStackPos() + 1, |
| 112 | + ); |
| 113 | + |
| 114 | + if ($this->tokens[$propertyPointer]['code'] !== T_VARIABLE) { |
| 115 | + return null; |
| 116 | + } |
| 117 | + |
| 118 | + $propertyTypeHint = PropertyHelper::findTypeHint($this->getFile(), $propertyPointer); |
| 119 | + |
| 120 | + if (!$propertyTypeHint) { |
| 121 | + return null; |
| 122 | + } |
| 123 | + |
| 124 | + return $propertyPointer; |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Returns true if this sniff or a rule of this sniff is suppressed with the slevomat suppress annotation. |
| 129 | + * |
| 130 | + * @param null|string $rule The optional rule. |
| 131 | + * @param int|null $stackPos Do you want ot overload the position for the which position the sniff is suppressed. |
| 132 | + * |
| 133 | + * @return bool Returns true if the sniff is suppressed. |
| 134 | + */ |
| 135 | + protected function isSniffSuppressed(?string $rule = null, ?int $stackPos = null): bool |
| 136 | + { |
| 137 | + return $this->getSuppressHelper()->isSniffSuppressed( |
| 138 | + $this->getFile(), |
| 139 | + $stackPos ?? $this->getStackPos(), |
| 140 | + $this->getSniffName($rule), |
| 141 | + ); |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Returns true if the getter is too simple and does not provide more functionality. |
| 146 | + * |
| 147 | + * @param string $propertyName |
| 148 | + * @param int $methodPosition |
| 149 | + * |
| 150 | + * @return bool |
| 151 | + */ |
| 152 | + private function isTooSimpleGetter(string $propertyName, int $methodPosition): bool |
| 153 | + { |
| 154 | + return $this->matchesMethodContentToRegex( |
| 155 | + '/return \$this->' . $propertyName . '\s*;$/m', |
| 156 | + $methodPosition, |
| 157 | + ); |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * Returns true if the setter is too simple and does not provide more functionality. |
| 162 | + * |
| 163 | + * @param string $propertyName |
| 164 | + * @param int $methodPosition |
| 165 | + * |
| 166 | + * @return bool |
| 167 | + */ |
| 168 | + private function isTooSimpleSetter(string $propertyName, int $methodPosition): bool |
| 169 | + { |
| 170 | + $isTooSimpleSetter = false; |
| 171 | + $file = $this->getFile(); |
| 172 | + |
| 173 | + $parameters = FunctionHelper::getParametersNames($file, $methodPosition); |
| 174 | + |
| 175 | + if (count($parameters) === 1) { |
| 176 | + $parameterName = current($parameters); |
| 177 | + |
| 178 | + $isTooSimpleSetter = $this->matchesMethodContentToRegex( |
| 179 | + '/\$this->' . $propertyName . '\s*=\s*\\' . $parameterName . '\s*;' . |
| 180 | + // finish with the assignment or wait for the fluent return. |
| 181 | + '($|\s*return\s*\$this\s*;$)/m', |
| 182 | + $methodPosition, |
| 183 | + ); |
| 184 | + } |
| 185 | + |
| 186 | + return $isTooSimpleSetter; |
| 187 | + } |
| 188 | + |
| 189 | + /** |
| 190 | + * Returns true if the methods content matches the given regex. |
| 191 | + * |
| 192 | + * @param string $contentRegex |
| 193 | + * |
| 194 | + * @return bool |
| 195 | + */ |
| 196 | + private function matchesMethodContentToRegex(string $contentRegex, int $methodPosition): bool |
| 197 | + { |
| 198 | + $matches = false; |
| 199 | + $methodToken = $this->tokens[$methodPosition]; |
| 200 | + |
| 201 | + if ($methodToken && @$methodToken['scope_opener'] && @$methodToken['scope_closer']) { |
| 202 | + $fileContent = trim($this->getFile()->getTokensAsString( |
| 203 | + $methodToken['scope_opener'] + 1, |
| 204 | + $methodToken['scope_closer'] - $methodToken['scope_opener'] - 1, |
| 205 | + )); |
| 206 | + |
| 207 | + $matches = (bool) preg_match($contentRegex, $fileContent); |
| 208 | + } |
| 209 | + |
| 210 | + return $matches; |
| 211 | + } |
| 212 | + |
| 213 | + /** |
| 214 | + * Checks if the sniff on a typed property is not suppressed to check for a simple getter and setter. |
| 215 | + * |
| 216 | + * @return void |
| 217 | + */ |
| 218 | + protected function processToken(): void |
| 219 | + { |
| 220 | + $propertyPointer = $this->getExactPropertyPointer(); |
| 221 | + |
| 222 | + if (!($propertyPointer && !$this->isSniffSuppressed(static::CODE_SHOULD_USE_PROPERTY))) { |
| 223 | + return; |
| 224 | + } |
| 225 | + |
| 226 | + $propertyName = substr($this->tokens[$propertyPointer]['content'], 1); |
| 227 | + $setterPosition = $this->findMethod(self::METHOD_PREFIX_SETTER . ucfirst($propertyName)); |
| 228 | + $getterPosition = $this->findMethod(self::METHOD_PREFIX_GETTER . ucfirst($propertyName)); |
| 229 | + |
| 230 | + if ( |
| 231 | + $getterPosition && $setterPosition && $this->isTooSimpleSetter($propertyName, $setterPosition) && |
| 232 | + $this->isTooSimpleGetter($propertyName, $getterPosition) |
| 233 | + ) { |
| 234 | + $this->getFile()->addWarning( |
| 235 | + self::ERROR_GETTER_TOO_SIMPLE, |
| 236 | + $getterPosition, |
| 237 | + static::CODE_SHOULD_USE_PROPERTY, |
| 238 | + $propertyName, |
| 239 | + ); |
| 240 | + |
| 241 | + $this->getFile()->addWarning( |
| 242 | + self::ERROR_SETTER_TOO_SIMPLE, |
| 243 | + $setterPosition, |
| 244 | + static::CODE_SHOULD_USE_PROPERTY, |
| 245 | + $propertyName, |
| 246 | + ); |
| 247 | + } |
| 248 | + } |
| 249 | + |
| 250 | + /** |
| 251 | + * Registers on the prefixes of a possible property, because is not so easy anymore since type hinting. |
| 252 | + * |
| 253 | + * @return array|mixed[] |
| 254 | + */ |
| 255 | + public function register() |
| 256 | + { |
| 257 | + return [ |
| 258 | + T_VAR, |
| 259 | + T_PUBLIC, |
| 260 | + T_PROTECTED, |
| 261 | + T_PRIVATE, |
| 262 | + ]; |
| 263 | + } |
| 264 | +} |
0 commit comments