|
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\Common;
|
16 | 17 | use PHP_CodeSniffer\Util\Standards;
|
17 | 18 | use stdClass;
|
@@ -124,6 +125,16 @@ class Ruleset
|
124 | 125 | */
|
125 | 126 | public $tokenListeners = [];
|
126 | 127 |
|
| 128 | + /** |
| 129 | + * An array of the names of sniffs which have been marked as deprecated. |
| 130 | + * |
| 131 | + * The key is the sniff code and the value |
| 132 | + * is the fully qualified name of the sniff class. |
| 133 | + * |
| 134 | + * @var array<string, string> |
| 135 | + */ |
| 136 | + private $deprecatedSniffs = []; |
| 137 | + |
127 | 138 |
|
128 | 139 | /**
|
129 | 140 | * Initialise the ruleset that the run will use.
|
@@ -317,6 +328,146 @@ public function explain()
|
317 | 328 | }//end explain()
|
318 | 329 |
|
319 | 330 |
|
| 331 | + /** |
| 332 | + * Checks whether any deprecated sniffs were registered via the ruleset. |
| 333 | + * |
| 334 | + * @return bool |
| 335 | + */ |
| 336 | + public function hasSniffDeprecations() |
| 337 | + { |
| 338 | + return (count($this->deprecatedSniffs) > 0); |
| 339 | + |
| 340 | + }//end hasSniffDeprecations() |
| 341 | + |
| 342 | + |
| 343 | + /** |
| 344 | + * Prints an information block about deprecated sniffs being used. |
| 345 | + * |
| 346 | + * @return void |
| 347 | + * |
| 348 | + * @throws \PHP_CodeSniffer\Exceptions\RuntimeException When the interface implementation is faulty. |
| 349 | + */ |
| 350 | + public function showSniffDeprecations() |
| 351 | + { |
| 352 | + if ($this->hasSniffDeprecations() === false) { |
| 353 | + return; |
| 354 | + } |
| 355 | + |
| 356 | + // Don't show deprecation notices in quiet mode, in explain mode |
| 357 | + // or when the documentation is being shown. |
| 358 | + // Documentation and explain will mark a sniff as deprecated natively |
| 359 | + // and also call the Ruleset multiple times which would lead to duplicate |
| 360 | + // display of the deprecation messages. |
| 361 | + if ($this->config->quiet === true |
| 362 | + || $this->config->explain === true |
| 363 | + || $this->config->generator !== null |
| 364 | + ) { |
| 365 | + return; |
| 366 | + } |
| 367 | + |
| 368 | + $reportWidth = $this->config->reportWidth; |
| 369 | + // Message takes report width minus the leading dash + two spaces, minus a one space gutter at the end. |
| 370 | + $maxMessageWidth = ($reportWidth - 4); |
| 371 | + $maxActualWidth = 0; |
| 372 | + |
| 373 | + ksort($this->deprecatedSniffs, (SORT_NATURAL | SORT_FLAG_CASE)); |
| 374 | + |
| 375 | + $messages = []; |
| 376 | + $messageTemplate = 'This sniff has been deprecated since %s and will be removed in %s. %s'; |
| 377 | + $errorTemplate = 'The %s::%s() method must return a %sstring, received %s'; |
| 378 | + |
| 379 | + foreach ($this->deprecatedSniffs as $sniffCode => $className) { |
| 380 | + if (isset($this->sniffs[$className]) === false) { |
| 381 | + // Should only be possible in test situations, but some extra defensive coding is never a bad thing. |
| 382 | + continue; |
| 383 | + } |
| 384 | + |
| 385 | + // Verify the interface was implemented correctly. |
| 386 | + // Unfortunately can't be safeguarded via type declarations yet. |
| 387 | + $deprecatedSince = $this->sniffs[$className]->getDeprecationVersion(); |
| 388 | + if (is_string($deprecatedSince) === false) { |
| 389 | + throw new RuntimeException( |
| 390 | + sprintf($errorTemplate, $className, 'getDeprecationVersion', 'non-empty ', gettype($deprecatedSince)) |
| 391 | + ); |
| 392 | + } |
| 393 | + |
| 394 | + if ($deprecatedSince === '') { |
| 395 | + throw new RuntimeException( |
| 396 | + sprintf($errorTemplate, $className, 'getDeprecationVersion', 'non-empty ', '""') |
| 397 | + ); |
| 398 | + } |
| 399 | + |
| 400 | + $removedIn = $this->sniffs[$className]->getRemovalVersion(); |
| 401 | + if (is_string($removedIn) === false) { |
| 402 | + throw new RuntimeException( |
| 403 | + sprintf($errorTemplate, $className, 'getRemovalVersion', 'non-empty ', gettype($removedIn)) |
| 404 | + ); |
| 405 | + } |
| 406 | + |
| 407 | + if ($removedIn === '') { |
| 408 | + throw new RuntimeException( |
| 409 | + sprintf($errorTemplate, $className, 'getRemovalVersion', 'non-empty ', '""') |
| 410 | + ); |
| 411 | + } |
| 412 | + |
| 413 | + $customMessage = $this->sniffs[$className]->getDeprecationMessage(); |
| 414 | + if (is_string($customMessage) === false) { |
| 415 | + throw new RuntimeException( |
| 416 | + sprintf($errorTemplate, $className, 'getDeprecationMessage', '', gettype($customMessage)) |
| 417 | + ); |
| 418 | + } |
| 419 | + |
| 420 | + // Truncate the error code if there is not enough report width. |
| 421 | + if (strlen($sniffCode) > $maxMessageWidth) { |
| 422 | + $sniffCode = substr($sniffCode, 0, ($maxMessageWidth - 3)).'...'; |
| 423 | + } |
| 424 | + |
| 425 | + $message = '- '.$sniffCode.PHP_EOL; |
| 426 | + if ($this->config->colors === true) { |
| 427 | + $message = '- '."\033[36m".$sniffCode."\033[0m".PHP_EOL; |
| 428 | + } |
| 429 | + |
| 430 | + $maxActualWidth = max($maxActualWidth, strlen($sniffCode)); |
| 431 | + |
| 432 | + // Normalize new line characters in custom message. |
| 433 | + $customMessage = preg_replace('`\R`', PHP_EOL, $customMessage); |
| 434 | + |
| 435 | + $notice = trim(sprintf($messageTemplate, $deprecatedSince, $removedIn, $customMessage)); |
| 436 | + $maxActualWidth = max($maxActualWidth, min(strlen($notice), $maxMessageWidth)); |
| 437 | + $wrapped = wordwrap($notice, $maxMessageWidth, PHP_EOL); |
| 438 | + $message .= ' '.implode(PHP_EOL.' ', explode(PHP_EOL, $wrapped)); |
| 439 | + |
| 440 | + $messages[] = $message; |
| 441 | + }//end foreach |
| 442 | + |
| 443 | + if (count($messages) === 0) { |
| 444 | + return; |
| 445 | + } |
| 446 | + |
| 447 | + $summaryLine = "WARNING: The $this->name standard uses 1 deprecated sniff"; |
| 448 | + $sniffCount = count($messages); |
| 449 | + if ($sniffCount !== 1) { |
| 450 | + $summaryLine = str_replace('1 deprecated sniff', "$sniffCount deprecated sniffs", $summaryLine); |
| 451 | + } |
| 452 | + |
| 453 | + $maxActualWidth = max($maxActualWidth, min(strlen($summaryLine), $maxMessageWidth)); |
| 454 | + |
| 455 | + $summaryLine = wordwrap($summaryLine, $reportWidth, PHP_EOL); |
| 456 | + if ($this->config->colors === true) { |
| 457 | + echo "\033[33m".$summaryLine."\033[0m".PHP_EOL; |
| 458 | + } else { |
| 459 | + echo $summaryLine.PHP_EOL; |
| 460 | + } |
| 461 | + |
| 462 | + echo str_repeat('-', min(($maxActualWidth + 4), $reportWidth)).PHP_EOL; |
| 463 | + echo implode(PHP_EOL, $messages); |
| 464 | + |
| 465 | + $closer = wordwrap('Deprecated sniffs are still run, but will stop working at some point in the future.', $reportWidth, PHP_EOL); |
| 466 | + echo PHP_EOL.PHP_EOL.$closer.PHP_EOL.PHP_EOL; |
| 467 | + |
| 468 | + }//end showSniffDeprecations() |
| 469 | + |
| 470 | + |
320 | 471 | /**
|
321 | 472 | * Processes a single ruleset and returns a list of the sniffs it represents.
|
322 | 473 | *
|
@@ -1184,6 +1335,10 @@ public function populateTokenListeners()
|
1184 | 1335 | $sniffCode = Common::getSniffCode($sniffClass);
|
1185 | 1336 | $this->sniffCodes[$sniffCode] = $sniffClass;
|
1186 | 1337 |
|
| 1338 | + if ($this->sniffs[$sniffClass] instanceof DeprecatedSniff) { |
| 1339 | + $this->deprecatedSniffs[$sniffCode] = $sniffClass; |
| 1340 | + } |
| 1341 | + |
1187 | 1342 | // Set custom properties.
|
1188 | 1343 | if (isset($this->ruleset[$sniffCode]['properties']) === true) {
|
1189 | 1344 | foreach ($this->ruleset[$sniffCode]['properties'] as $name => $settings) {
|
|
0 commit comments