validate-deprecation-aliases.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #!/usr/bin/php -q
  2. <?php
  3. declare(strict_types=1);
  4. /*
  5. * Validate that each deprecation of a class is followed by class_alias() and class_exists()
  6. * in each relevant file complementing each other.
  7. */
  8. $options = [
  9. __DIR__ . '/../vendor/autoload.php',
  10. __DIR__ . '/vendor/autoload.php',
  11. ];
  12. if (!empty($_SERVER['PWD'])) {
  13. array_unshift($options, $_SERVER['PWD'] . '/vendor/autoload.php');
  14. }
  15. foreach ($options as $file) {
  16. if (file_exists($file)) {
  17. define('COMPOSER_INSTALL', $file);
  18. break;
  19. }
  20. }
  21. require COMPOSER_INSTALL;
  22. $path = dirname(__DIR__) . DS . 'src' . DS;
  23. $di = new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::SKIP_DOTS);
  24. /** @var array<\SplFileInfo> $iterator */
  25. $iterator = new RecursiveIteratorIterator($di);
  26. $code = 0;
  27. foreach ($iterator as $file) {
  28. if (pathinfo((string)$file, PATHINFO_EXTENSION) !== 'php') {
  29. continue;
  30. }
  31. if (pathinfo((string)$file, PATHINFO_FILENAME) === 'functions') {
  32. continue;
  33. }
  34. if (strpos($file->getRealPath(), '/TestSuite/')) {
  35. continue;
  36. }
  37. $content = file_get_contents((string)$file);
  38. if (!strpos($content, 'class_alias(')) {
  39. continue;
  40. }
  41. preg_match('#class_alias\(\s*\'([^\']+)\',#', $content, $matches);
  42. if (!$matches) {
  43. var_dump($content);
  44. var_dump($file->getPath());
  45. exit(1);
  46. }
  47. echo $matches[1] . PHP_EOL;
  48. $filePath = str_replace('\\', '/', $matches[1]);
  49. $filePath = str_replace('Cake/', $path, $filePath);
  50. $filePath .= '.php';
  51. if (!file_exists($filePath)) {
  52. throw new RuntimeException('Cannot find path for `' . $matches[1] . '`');
  53. }
  54. $newFileContent = file_get_contents($filePath);
  55. if (strpos($newFileContent, 'class_exists(') === false && !str_contains($newFileContent, 'class_alias(')) {
  56. $oldPath = str_replace($path, '', $file->getRealPath());
  57. $newPath = str_replace($path, '', $filePath);
  58. echo "\033[31m" . ' * Missing `class_exists()` or `class_alias()` on new file for `' . $oldPath . '` => `' . $newPath . '`' . "\033[0m" . PHP_EOL;
  59. $code = 1;
  60. } else {
  61. echo ' * OK' . PHP_EOL;
  62. }
  63. }
  64. exit($code);