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