Skip to content

Commit

Permalink
Locate & parse phpstan config to determine tmpDir
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
shochdoerfer committed Apr 18, 2020
1 parent 55381e2 commit 88e53f8
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 4 deletions.
38 changes: 35 additions & 3 deletions autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,43 @@
use bitExpert\PHPStan\Magento\Autoload\FactoryAutoloader;
use bitExpert\PHPStan\Magento\Autoload\MockAutoloader;
use bitExpert\PHPStan\Magento\Autoload\ProxyAutoloader;
use Nette\Neon\Neon;
use PHPStan\Cache\Cache;

// This autoloader implementation supersedes the former \bitExpert\PHPStan\Magento\Autoload\Autoload implementation
(function () {
$cache = new Cache(new FileCacheStorage(sys_get_temp_dir() . '/phpstan/cache/PHPStan'));
(function (array $argv = []) {
// Sadly we don't have access to the parsed phpstan.neon configuration at this point we need to look up the
// location of the config file and parse it with the Neon parser to be able to extract the tmpDir definition!
$configFile = '';
if (count($argv) > 0) {
foreach($argv as $idx => $value) {
if ((strtolower($value) === '-c') && isset($argv[$idx + 1])) {
$configFile = $argv[$idx + 1];
break;
}
}
}

if (empty($configFile)) {
$currentWorkingDirectory = getcwd();
foreach (['phpstan.neon', 'phpstan.neon.dist'] as $discoverableConfigName) {
$discoverableConfigFile = $currentWorkingDirectory . DIRECTORY_SEPARATOR . $discoverableConfigName;
if (file_exists($discoverableConfigFile) && is_readable(($discoverableConfigFile))) {
$configFile = $discoverableConfigFile;
break;
}
}
}

$tmpDir = sys_get_temp_dir() . '/phpstan';
if (!empty($configFile)) {
$neonConfig = Neon::decode(file_get_contents($configFile));
if(is_array($neonConfig) && isset($neonConfig['parameters']) && isset($neonConfig['parameters']['tmpDir'])) {
$tmpDir = $neonConfig['parameters']['tmpDir'];
}
}

$cache = new Cache(new FileCacheStorage($tmpDir . '/cache/PHPStan'));

$mockAutoloader = new MockAutoloader();
$factoryAutoloader = new FactoryAutoloader($cache);
Expand All @@ -26,4 +58,4 @@
\spl_autoload_register([$mockAutoloader, 'autoload'], true, true);
\spl_autoload_register([$factoryAutoloader, 'autoload'], true, false);
\spl_autoload_register([$proxyAutoloader, 'autoload'], true, false);
})();
})($GLOBALS['argv'] ?? []);
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
],
"require": {
"php": "^7.2.0",
"nette/neon": "^3.1",
"phpstan/phpstan": "^0.12.18"
},
"require-dev": {
Expand Down
64 changes: 63 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 88e53f8

Please sign in to comment.