|
| 1 | +#!/usr/bin/env php |
| 2 | +<?php |
| 3 | + |
| 4 | +/* |
| 5 | + * This file is part of the phpstan-magento package. |
| 6 | + * |
| 7 | + * (c) bitExpert AG |
| 8 | + * |
| 9 | + * For the full copyright and license information, please view the LICENSE |
| 10 | + * file that was distributed with this source code. |
| 11 | + */ |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +/* |
| 15 | + * Runs a real PHPStan analysis against a generated fixture project and fails when the autoloaders |
| 16 | + * report an internal error. |
| 17 | + * |
| 18 | + * The fixture only asks for factories, which is enough: all four generated-class autoloaders |
| 19 | + * share the store-then-require mechanism this guards. |
| 20 | + * |
| 21 | + * The autoloaders generate a class, store it and then require the stored file. Whenever PHPStan |
| 22 | + * changes how it hands cached values back, that round trip can start to return the source code in |
| 23 | + * place of the path, which turns every generated class into |
| 24 | + * "Internal error: Failed opening required '<?php ...'". Unit tests cannot see this: it only shows |
| 25 | + * with a cold cache, more than one analysis process, and PHPStan's turbo extension active. |
| 26 | + * |
| 27 | + * Set REQUIRE_TURBO=1 (the CI job does) to also fail when PHPStan's shared memory arena is not |
| 28 | + * usable, so that the guard cannot pass without exercising the code path it protects. |
| 29 | + */ |
| 30 | + |
| 31 | +$repoRoot = dirname(__DIR__); |
| 32 | +$fixtureRoot = sys_get_temp_dir() . '/phpstan-magento-smoke-' . uniqid(); |
| 33 | +$numberOfFiles = 100; |
| 34 | + |
| 35 | +function removeDirectory(string $directory): void |
| 36 | +{ |
| 37 | + if (!is_dir($directory)) { |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + $entries = new RecursiveIteratorIterator( |
| 42 | + new RecursiveDirectoryIterator($directory, FilesystemIterator::SKIP_DOTS), |
| 43 | + RecursiveIteratorIterator::CHILD_FIRST |
| 44 | + ); |
| 45 | + |
| 46 | + foreach ($entries as $entry) { |
| 47 | + /** @var SplFileInfo $entry */ |
| 48 | + $entry->isDir() ? rmdir($entry->getPathname()) : unlink($entry->getPathname()); |
| 49 | + } |
| 50 | + |
| 51 | + rmdir($directory); |
| 52 | +} |
| 53 | + |
| 54 | +function run(string $repoRoot, string $command, string $config): array |
| 55 | +{ |
| 56 | + $commandLine = sprintf( |
| 57 | + '%s %s %s --configuration=%s 2>&1', |
| 58 | + escapeshellarg(PHP_BINARY), |
| 59 | + escapeshellarg($repoRoot . '/vendor/bin/phpstan'), |
| 60 | + $command, |
| 61 | + escapeshellarg($config) |
| 62 | + ); |
| 63 | + |
| 64 | + $output = []; |
| 65 | + $exitCode = 0; |
| 66 | + exec($commandLine, $output, $exitCode); |
| 67 | + |
| 68 | + return [implode(PHP_EOL, $output), $exitCode]; |
| 69 | +} |
| 70 | + |
| 71 | +/* |
| 72 | + * The arena is what makes the cache round trip observable, and the extension being loaded does not |
| 73 | + * mean the arena works: on macOS the extension loads but never hands out an arena. Probe it the way |
| 74 | + * PHPStan does, with the binary PHPStan itself reports. |
| 75 | + */ |
| 76 | +function arenaIsUsable(string $diagnoseOutput): bool |
| 77 | +{ |
| 78 | + if (preg_match('#^Turbo worker binary: (\S+)#m', $diagnoseOutput, $matches) !== 1) { |
| 79 | + return false; |
| 80 | + } |
| 81 | + |
| 82 | + $probe = sprintf( |
| 83 | + '%s -d %s -r %s 2>&1', |
| 84 | + escapeshellarg(PHP_BINARY), |
| 85 | + escapeshellarg('extension=' . $matches[1]), |
| 86 | + escapeshellarg('echo \PHPStanTurbo\ArenaCache::create("probe") === null ? "no" : "yes";') |
| 87 | + ); |
| 88 | + |
| 89 | + $output = []; |
| 90 | + $exitCode = 0; |
| 91 | + exec($probe, $output, $exitCode); |
| 92 | + |
| 93 | + return $exitCode === 0 && in_array('yes', $output, true); |
| 94 | +} |
| 95 | + |
| 96 | +mkdir($fixtureRoot . '/src', 0777, true); |
| 97 | + |
| 98 | +for ($i = 1; $i <= $numberOfFiles; $i++) { |
| 99 | + file_put_contents( |
| 100 | + sprintf('%s/src/Consumer%d.php', $fixtureRoot, $i), |
| 101 | + <<<PHP |
| 102 | + <?php declare(strict_types=1); |
| 103 | +
|
| 104 | + namespace Smoke; |
| 105 | +
|
| 106 | + class Consumer{$i} |
| 107 | + { |
| 108 | + public function useFactory(\\Smoke\\Thing{$i}Factory \$factory): void |
| 109 | + { |
| 110 | + \$factory->create(); |
| 111 | + } |
| 112 | + } |
| 113 | +
|
| 114 | + PHP |
| 115 | + ); |
| 116 | +} |
| 117 | + |
| 118 | +// A cold tmpDir matters: with a warm cache the autoloaders find the generated file before they |
| 119 | +// ever ask PHPStan to store anything, which hides the very problem this guard looks for. |
| 120 | +file_put_contents($fixtureRoot . '/phpstan.neon', <<<NEON |
| 121 | +includes: |
| 122 | + - {$repoRoot}/extension.neon |
| 123 | +
|
| 124 | +parameters: |
| 125 | + tmpDir: {$fixtureRoot}/tmp |
| 126 | + level: 2 |
| 127 | + paths: |
| 128 | + - {$fixtureRoot}/src |
| 129 | + magento: |
| 130 | + magentoRoot: {$fixtureRoot} |
| 131 | + parallel: |
| 132 | + jobSize: 5 |
| 133 | + minimumNumberOfJobsPerProcess: 1 |
| 134 | + maximumNumberOfProcesses: 4 |
| 135 | +
|
| 136 | +NEON); |
| 137 | + |
| 138 | +[$diagnoseOutput] = run($repoRoot, 'diagnose', $fixtureRoot . '/phpstan.neon'); |
| 139 | +$arenaIsUsable = strpos($diagnoseOutput, 'Turbo extension: enabled (') !== false |
| 140 | + && arenaIsUsable($diagnoseOutput); |
| 141 | + |
| 142 | +echo 'PHPStan shared memory arena usable: ' . ($arenaIsUsable ? 'yes' : 'no') . PHP_EOL; |
| 143 | + |
| 144 | +if (!$arenaIsUsable && getenv('REQUIRE_TURBO') === '1') { |
| 145 | + fwrite(STDERR, PHP_EOL . 'The shared memory arena is not usable, thus this guard would pass' . PHP_EOL); |
| 146 | + fwrite(STDERR, 'without exercising the code path it protects. PHPStan reported:' . PHP_EOL . PHP_EOL); |
| 147 | + fwrite(STDERR, $diagnoseOutput . PHP_EOL); |
| 148 | + removeDirectory($fixtureRoot); |
| 149 | + exit(1); |
| 150 | +} |
| 151 | + |
| 152 | +[$analyseOutput, $exitCode] = run($repoRoot, 'analyse --no-progress', $fixtureRoot . '/phpstan.neon'); |
| 153 | +removeDirectory($fixtureRoot); |
| 154 | + |
| 155 | +if (strpos($analyseOutput, 'Internal error') !== false || $exitCode !== 0) { |
| 156 | + fwrite(STDERR, PHP_EOL . 'The generated-class autoloaders did not survive a real analysis run:' . PHP_EOL . PHP_EOL); |
| 157 | + fwrite(STDERR, $analyseOutput . PHP_EOL); |
| 158 | + exit(1); |
| 159 | +} |
| 160 | + |
| 161 | +echo 'Autoloader smoke test passed.' . PHP_EOL; |
0 commit comments