validate-split-packages-phpstan.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. $mainJsonContent = file_get_contents(dirname(__FILE__, 2) . DS . 'composer.json');
  35. $mainJson = json_decode($mainJsonContent, true);
  36. $composerCommand = 'composer require --dev phpstan/phpstan:' . $mainJson['require-dev']['phpstan/phpstan'];
  37. $issues = [];
  38. foreach ($packages as $path => $package) {
  39. if (!file_exists($path . 'phpstan.neon.dist')) {
  40. continue;
  41. }
  42. $exitCode = null;
  43. exec(
  44. 'cd ' . $path . ' && ' . $composerCommand . ' && vendor/bin/phpstan analyze ./',
  45. $output,
  46. $exitCode
  47. );
  48. if ($exitCode !== 0) {
  49. $code = $exitCode;
  50. $issues[] = $package . ': ' . PHP_EOL . implode(PHP_EOL, $output);
  51. }
  52. exec('cd ' . $path . ' && rm composer.lock && rm -rf vendor');
  53. }
  54. echo implode(PHP_EOL . PHP_EOL, $issues);
  55. exit($code);