|
12 | 12 | namespace PHP_CodeSniffer;
|
13 | 13 |
|
14 | 14 | use PHP_CodeSniffer\Exceptions\RuntimeException;
|
| 15 | +use PHP_CodeSniffer\Sniffs\DeprecatedSniff; |
15 | 16 | use PHP_CodeSniffer\Util;
|
16 | 17 | use stdClass;
|
17 | 18 |
|
@@ -116,6 +117,16 @@ class Ruleset
|
116 | 117 | */
|
117 | 118 | private $config = null;
|
118 | 119 |
|
| 120 | + /** |
| 121 | + * An array of the names of sniffs which have been marked as deprecated. |
| 122 | + * |
| 123 | + * The key is the sniff code and the value |
| 124 | + * is the fully qualified name of the sniff class. |
| 125 | + * |
| 126 | + * @var array<string, string> |
| 127 | + */ |
| 128 | + private $deprecatedSniffs = []; |
| 129 | + |
119 | 130 |
|
120 | 131 | /**
|
121 | 132 | * Initialise the ruleset that the run will use.
|
@@ -290,13 +301,161 @@ public function explain()
|
290 | 301 | }
|
291 | 302 | }//end if
|
292 | 303 |
|
| 304 | + if (isset($this->deprecatedSniffs[$sniff]) === true) { |
| 305 | + $sniff .= ' *'; |
| 306 | + } |
| 307 | + |
293 | 308 | $sniffsInStandard[] = $sniff;
|
294 | 309 | ++$lastCount;
|
295 | 310 | }//end foreach
|
296 | 311 |
|
| 312 | + if (count($this->deprecatedSniffs) > 0) { |
| 313 | + echo PHP_EOL.'* Sniffs marked with an asterix are deprecated.'.PHP_EOL; |
| 314 | + } |
| 315 | + |
297 | 316 | }//end explain()
|
298 | 317 |
|
299 | 318 |
|
| 319 | + /** |
| 320 | + * Checks whether any deprecated sniffs were registered via the ruleset. |
| 321 | + * |
| 322 | + * @return bool |
| 323 | + */ |
| 324 | + public function hasSniffDeprecations() |
| 325 | + { |
| 326 | + return (count($this->deprecatedSniffs) > 0); |
| 327 | + |
| 328 | + }//end hasSniffDeprecations() |
| 329 | + |
| 330 | + |
| 331 | + /** |
| 332 | + * Prints an information block about deprecated sniffs being used. |
| 333 | + * |
| 334 | + * @return void |
| 335 | + * |
| 336 | + * @throws \PHP_CodeSniffer\Exceptions\RuntimeException When the interface implementation is faulty. |
| 337 | + */ |
| 338 | + public function showSniffDeprecations() |
| 339 | + { |
| 340 | + if ($this->hasSniffDeprecations() === false) { |
| 341 | + return; |
| 342 | + } |
| 343 | + |
| 344 | + // Don't show deprecation notices in quiet mode, in explain mode |
| 345 | + // or when the documentation is being shown. |
| 346 | + // Documentation and explain will mark a sniff as deprecated natively |
| 347 | + // and also call the Ruleset multiple times which would lead to duplicate |
| 348 | + // display of the deprecation messages. |
| 349 | + if ($this->config->quiet === true |
| 350 | + || $this->config->explain === true |
| 351 | + || $this->config->generator !== null |
| 352 | + ) { |
| 353 | + return; |
| 354 | + } |
| 355 | + |
| 356 | + $reportWidth = $this->config->reportWidth; |
| 357 | + // Message takes report width minus the leading dash + two spaces, minus a one space gutter at the end. |
| 358 | + $maxMessageWidth = ($reportWidth - 4); |
| 359 | + $maxActualWidth = 0; |
| 360 | + |
| 361 | + ksort($this->deprecatedSniffs, (SORT_NATURAL | SORT_FLAG_CASE)); |
| 362 | + |
| 363 | + $messages = []; |
| 364 | + $messageTemplate = 'This sniff has been deprecated since %s and will be removed in %s. %s'; |
| 365 | + $errorTemplate = 'The %s::%s() method must return a %sstring, received %s'; |
| 366 | + |
| 367 | + foreach ($this->deprecatedSniffs as $sniffCode => $className) { |
| 368 | + if (isset($this->sniffs[$className]) === false) { |
| 369 | + // Should only be possible in test situations, but some extra defensive coding is never a bad thing. |
| 370 | + continue; |
| 371 | + } |
| 372 | + |
| 373 | + // Verify the interface was implemented correctly. |
| 374 | + // Unfortunately can't be safeguarded via type declarations yet. |
| 375 | + $deprecatedSince = $this->sniffs[$className]->getDeprecationVersion(); |
| 376 | + if (is_string($deprecatedSince) === false) { |
| 377 | + throw new RuntimeException( |
| 378 | + sprintf($errorTemplate, $className, 'getDeprecationVersion', 'non-empty ', gettype($deprecatedSince)) |
| 379 | + ); |
| 380 | + } |
| 381 | + |
| 382 | + if ($deprecatedSince === '') { |
| 383 | + throw new RuntimeException( |
| 384 | + sprintf($errorTemplate, $className, 'getDeprecationVersion', 'non-empty ', '""') |
| 385 | + ); |
| 386 | + } |
| 387 | + |
| 388 | + $removedIn = $this->sniffs[$className]->getRemovalVersion(); |
| 389 | + if (is_string($removedIn) === false) { |
| 390 | + throw new RuntimeException( |
| 391 | + sprintf($errorTemplate, $className, 'getRemovalVersion', 'non-empty ', gettype($removedIn)) |
| 392 | + ); |
| 393 | + } |
| 394 | + |
| 395 | + if ($removedIn === '') { |
| 396 | + throw new RuntimeException( |
| 397 | + sprintf($errorTemplate, $className, 'getRemovalVersion', 'non-empty ', '""') |
| 398 | + ); |
| 399 | + } |
| 400 | + |
| 401 | + $customMessage = $this->sniffs[$className]->getDeprecationMessage(); |
| 402 | + if (is_string($customMessage) === false) { |
| 403 | + throw new RuntimeException( |
| 404 | + sprintf($errorTemplate, $className, 'getDeprecationMessage', '', gettype($customMessage)) |
| 405 | + ); |
| 406 | + } |
| 407 | + |
| 408 | + // Truncate the error code if there is not enough report width. |
| 409 | + if (strlen($sniffCode) > $maxMessageWidth) { |
| 410 | + $sniffCode = substr($sniffCode, 0, ($maxMessageWidth - 3)).'...'; |
| 411 | + } |
| 412 | + |
| 413 | + $message = '- '.$sniffCode.PHP_EOL; |
| 414 | + if ($this->config->colors === true) { |
| 415 | + $message = '- '."\033[36m".$sniffCode."\033[0m".PHP_EOL; |
| 416 | + } |
| 417 | + |
| 418 | + $maxActualWidth = max($maxActualWidth, strlen($sniffCode)); |
| 419 | + |
| 420 | + // Normalize new line characters in custom message. |
| 421 | + $customMessage = preg_replace('`\R`', PHP_EOL, $customMessage); |
| 422 | + |
| 423 | + $notice = trim(sprintf($messageTemplate, $deprecatedSince, $removedIn, $customMessage)); |
| 424 | + $maxActualWidth = max($maxActualWidth, min(strlen($notice), $maxMessageWidth)); |
| 425 | + $wrapped = wordwrap($notice, $maxMessageWidth, PHP_EOL); |
| 426 | + $message .= ' '.implode(PHP_EOL.' ', explode(PHP_EOL, $wrapped)); |
| 427 | + |
| 428 | + $messages[] = $message; |
| 429 | + }//end foreach |
| 430 | + |
| 431 | + if (count($messages) === 0) { |
| 432 | + return; |
| 433 | + } |
| 434 | + |
| 435 | + $summaryLine = "WARNING: The $this->name standard uses 1 deprecated sniff"; |
| 436 | + $sniffCount = count($messages); |
| 437 | + if ($sniffCount !== 1) { |
| 438 | + $summaryLine = str_replace('1 deprecated sniff', "$sniffCount deprecated sniffs", $summaryLine); |
| 439 | + } |
| 440 | + |
| 441 | + $maxActualWidth = max($maxActualWidth, min(strlen($summaryLine), $maxMessageWidth)); |
| 442 | + |
| 443 | + $summaryLine = wordwrap($summaryLine, $reportWidth, PHP_EOL); |
| 444 | + if ($this->config->colors === true) { |
| 445 | + echo "\033[33m".$summaryLine."\033[0m".PHP_EOL; |
| 446 | + } else { |
| 447 | + echo $summaryLine.PHP_EOL; |
| 448 | + } |
| 449 | + |
| 450 | + echo str_repeat('-', min(($maxActualWidth + 4), $reportWidth)).PHP_EOL; |
| 451 | + echo implode(PHP_EOL, $messages); |
| 452 | + |
| 453 | + $closer = wordwrap('Deprecated sniffs are still run, but will stop working at some point in the future.', $reportWidth, PHP_EOL); |
| 454 | + echo PHP_EOL.PHP_EOL.$closer.PHP_EOL.PHP_EOL; |
| 455 | + |
| 456 | + }//end showSniffDeprecations() |
| 457 | + |
| 458 | + |
300 | 459 | /**
|
301 | 460 | * Processes a single ruleset and returns a list of the sniffs it represents.
|
302 | 461 | *
|
@@ -1225,6 +1384,10 @@ public function populateTokenListeners()
|
1225 | 1384 | $sniffCode = Util\Common::getSniffCode($sniffClass);
|
1226 | 1385 | $this->sniffCodes[$sniffCode] = $sniffClass;
|
1227 | 1386 |
|
| 1387 | + if ($this->sniffs[$sniffClass] instanceof DeprecatedSniff) { |
| 1388 | + $this->deprecatedSniffs[$sniffCode] = $sniffClass; |
| 1389 | + } |
| 1390 | + |
1228 | 1391 | // Set custom properties.
|
1229 | 1392 | if (isset($this->ruleset[$sniffCode]['properties']) === true) {
|
1230 | 1393 | foreach ($this->ruleset[$sniffCode]['properties'] as $name => $settings) {
|
|
0 commit comments