WarningsDetector.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of PHP CS Fixer.
  5. *
  6. * (c) Fabien Potencier <fabien@symfony.com>
  7. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  8. *
  9. * This source file is subject to the MIT license that is bundled
  10. * with this source code in the file LICENSE.
  11. */
  12. namespace PhpCsFixer\Console;
  13. use PhpCsFixer\ToolInfo;
  14. use PhpCsFixer\ToolInfoInterface;
  15. /**
  16. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  17. *
  18. * @internal
  19. */
  20. final class WarningsDetector
  21. {
  22. private ToolInfoInterface $toolInfo;
  23. /**
  24. * @var list<string>
  25. */
  26. private array $warnings = [];
  27. public function __construct(ToolInfoInterface $toolInfo)
  28. {
  29. $this->toolInfo = $toolInfo;
  30. }
  31. public function detectOldMajor(): void
  32. {
  33. // @TODO 3.99 to be activated with new MAJOR release 4.0
  34. // $currentMajorVersion = \intval(explode('.', Application::VERSION)[0], 10);
  35. // $nextMajorVersion = $currentMajorVersion + 1;
  36. // $this->warnings[] = "You are running PHP CS Fixer v{$currentMajorVersion}, which is not maintained anymore. Please update to v{$nextMajorVersion}.";
  37. // $this->warnings[] = "You may find an UPGRADE guide at https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/v{$nextMajorVersion}.0.0/UPGRADE-v{$nextMajorVersion}.md .";
  38. }
  39. public function detectOldVendor(): void
  40. {
  41. if ($this->toolInfo->isInstalledByComposer()) {
  42. $details = $this->toolInfo->getComposerInstallationDetails();
  43. if (ToolInfo::COMPOSER_LEGACY_PACKAGE_NAME === $details['name']) {
  44. $this->warnings[] = sprintf(
  45. 'You are running PHP CS Fixer installed with old vendor `%s`. Please update to `%s`.',
  46. ToolInfo::COMPOSER_LEGACY_PACKAGE_NAME,
  47. ToolInfo::COMPOSER_PACKAGE_NAME
  48. );
  49. }
  50. }
  51. }
  52. /**
  53. * @return list<string>
  54. */
  55. public function getWarnings(): array
  56. {
  57. if (0 === \count($this->warnings)) {
  58. return [];
  59. }
  60. return array_values(array_unique(array_merge(
  61. $this->warnings,
  62. ['If you need help while solving warnings, ask at https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/discussions/, we will help you!']
  63. )));
  64. }
  65. }