validate-split-packages-phpstan.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #!/usr/bin/php -q
  2. <?php
  3. declare(strict_types=1);
  4. /*
  5. * Validate split packages through PHPStan.
  6. */
  7. $options = [
  8. __DIR__ . '/../vendor/autoload.php',
  9. __DIR__ . '/vendor/autoload.php',
  10. ];
  11. if (!empty($_SERVER['PWD'])) {
  12. array_unshift($options, $_SERVER['PWD'] . '/vendor/autoload.php');
  13. }
  14. foreach ($options as $file) {
  15. if (file_exists($file)) {
  16. define('COMPOSER_INSTALL', $file);
  17. break;
  18. }
  19. }
  20. require COMPOSER_INSTALL;
  21. $path = dirname(__DIR__) . DS . 'src' . DS;
  22. $di = new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::SKIP_DOTS);
  23. $iterator = new RecursiveIteratorIterator($di, RecursiveIteratorIterator::LEAVES_ONLY);
  24. /** @var array<\SplFileInfo> $iterator */
  25. $iterator = new RegexIterator($iterator, '~/src/\w+/composer.json$~');
  26. $packages = [];
  27. $code = 0;
  28. foreach ($iterator as $file) {
  29. $filePath = $file->getPath();
  30. $package = substr($filePath, strrpos($filePath, '/') + 1);
  31. $packages[$filePath . '/'] = $package;
  32. }
  33. ksort($packages);
  34. $issues = [];
  35. foreach ($packages as $path => $package) {
  36. if (!file_exists($path . 'phpstan.neon.dist')) {
  37. continue;
  38. }
  39. $exitCode = null;
  40. $output = [];
  41. exec('cd ' . $path . ' && composer install && vendor/bin/phpstan analyze ./', $output, $exitCode);
  42. if ($exitCode !== 0) {
  43. $code = $exitCode;
  44. $issues[] = 'Issue(s) detected with ' . $package . ' package: ' . PHP_EOL . implode(PHP_EOL, $output);
  45. }
  46. exec('cd ' . $path . ' && rm composer.lock && rm -rf vendor');
  47. }
  48. echo implode(PHP_EOL . PHP_EOL, $issues);
  49. exit($code);