Skip to content

Commit 88e53f8

Browse files
committed
Locate & parse phpstan config to determine tmpDir
To make sure that the cached files are generated in the same cache directory as the regular phpstan files, find the phpstan configuration and parse it via Nette\Neon parser. Downside: The configuration gets parsed twice but I did not notice any bigger speed difference.
1 parent 55381e2 commit 88e53f8

File tree

3 files changed

+99
-4
lines changed

3 files changed

+99
-4
lines changed

autoload.php

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,43 @@
1313
use bitExpert\PHPStan\Magento\Autoload\FactoryAutoloader;
1414
use bitExpert\PHPStan\Magento\Autoload\MockAutoloader;
1515
use bitExpert\PHPStan\Magento\Autoload\ProxyAutoloader;
16+
use Nette\Neon\Neon;
1617
use PHPStan\Cache\Cache;
1718

1819
// This autoloader implementation supersedes the former \bitExpert\PHPStan\Magento\Autoload\Autoload implementation
19-
(function () {
20-
$cache = new Cache(new FileCacheStorage(sys_get_temp_dir() . '/phpstan/cache/PHPStan'));
20+
(function (array $argv = []) {
21+
// Sadly we don't have access to the parsed phpstan.neon configuration at this point we need to look up the
22+
// location of the config file and parse it with the Neon parser to be able to extract the tmpDir definition!
23+
$configFile = '';
24+
if (count($argv) > 0) {
25+
foreach($argv as $idx => $value) {
26+
if ((strtolower($value) === '-c') && isset($argv[$idx + 1])) {
27+
$configFile = $argv[$idx + 1];
28+
break;
29+
}
30+
}
31+
}
32+
33+
if (empty($configFile)) {
34+
$currentWorkingDirectory = getcwd();
35+
foreach (['phpstan.neon', 'phpstan.neon.dist'] as $discoverableConfigName) {
36+
$discoverableConfigFile = $currentWorkingDirectory . DIRECTORY_SEPARATOR . $discoverableConfigName;
37+
if (file_exists($discoverableConfigFile) && is_readable(($discoverableConfigFile))) {
38+
$configFile = $discoverableConfigFile;
39+
break;
40+
}
41+
}
42+
}
43+
44+
$tmpDir = sys_get_temp_dir() . '/phpstan';
45+
if (!empty($configFile)) {
46+
$neonConfig = Neon::decode(file_get_contents($configFile));
47+
if(is_array($neonConfig) && isset($neonConfig['parameters']) && isset($neonConfig['parameters']['tmpDir'])) {
48+
$tmpDir = $neonConfig['parameters']['tmpDir'];
49+
}
50+
}
51+
52+
$cache = new Cache(new FileCacheStorage($tmpDir . '/cache/PHPStan'));
2153

2254
$mockAutoloader = new MockAutoloader();
2355
$factoryAutoloader = new FactoryAutoloader($cache);
@@ -26,4 +58,4 @@
2658
\spl_autoload_register([$mockAutoloader, 'autoload'], true, true);
2759
\spl_autoload_register([$factoryAutoloader, 'autoload'], true, false);
2860
\spl_autoload_register([$proxyAutoloader, 'autoload'], true, false);
29-
})();
61+
})($GLOBALS['argv'] ?? []);

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
],
1818
"require": {
1919
"php": "^7.2.0",
20+
"nette/neon": "^3.1",
2021
"phpstan/phpstan": "^0.12.18"
2122
},
2223
"require-dev": {

composer.lock

Lines changed: 63 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)