1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

Make Phar more robust to object namespace changes

This commit is contained in:
Matthew Brown 2018-03-12 00:01:21 -04:00
parent 0828d5e3b3
commit 73fadab0b5
7 changed files with 55 additions and 18 deletions

View File

@ -15,22 +15,16 @@ ulimit -Sn 4096
rm -f bin/psalm.phar rm -f bin/psalm.phar
# Prefixes the code to be bundled # Prefixes the code to be bundled
php -d memory_limit=-1 `which php-scoper` add-prefix --output-dir=build/psalm --force php -d memory_limit=-1 `which php-scoper` add-prefix --prefix='PsalmPhar' --output-dir=build/psalm --force
[ -d build/psalm/src/Psalm/Stubs ] || mkdir build/psalm/src/Psalm/Stubs
cp src/Psalm/Stubs/CoreGenericFunctions.php build/psalm/src/Psalm/Stubs
cp src/Psalm/Stubs/CoreGenericClasses.php build/psalm/src/Psalm/Stubs
# Re-dump the loader to account for the prefixing # Re-dump the loader to account for the prefixing
# and optimize the loader # and optimize the loader
composer dump-autoload --working-dir=build/psalm --classmap-authoritative --no-dev composer dump-autoload --working-dir=build/psalm --classmap-authoritative --no-dev
php -d memory_limit=-1 -d phar.readonly=0 `which box` --debug --dev compile php -d memory_limit=-1 -d phar.readonly=0 `which box` compile
# clean up build # clean up build
rm -Rf build/psalm rm -Rf build/psalm
rm -Rf .box
# reinstall deps (to regenerate autoloader and bring back dev deps) # reinstall deps (to regenerate autoloader and bring back dev deps)
rm -Rf vendor/* rm -Rf vendor/*

View File

@ -4,7 +4,7 @@ use Isolated\Symfony\Component\Finder\Finder;
return [ return [
'finders' => [ 'finders' => [
Finder::create()->files()->exclude(['Psalm/Stubs'])->in('src'), Finder::create()->files()->in('src'),
Finder::create()->files()->in('assets'), Finder::create()->files()->in('assets'),
Finder::create() Finder::create()
->files() ->files()
@ -58,9 +58,13 @@ return [
return $contents; return $contents;
}, },
function ($filePath, $prefix, $contents) { function ($filePath, $prefix, $contents) {
if ($filePath === realpath(__DIR__ . '/src/Psalm/PropertyMap.php')) { if ($filePath === realpath(__DIR__ . '/src/Psalm/PropertyMap.php')
|| $filePath === realpath(__DIR__ . '/src/Psalm/CallMap.php')
|| $filePath === realpath(__DIR__ . '/src/Psalm/Stubs/CoreGenericFunctions.php')
|| $filePath === realpath(__DIR__ . '/src/Psalm/Stubs/CoreGenericClasses.php')
) {
return str_replace( return str_replace(
[$prefix . '\\\\', $prefix . '\\'], ['namespace ' . $prefix . ';', $prefix . '\\\\', $prefix . '\\'],
'', '',
$contents $contents
); );

View File

@ -178,7 +178,8 @@ class ProjectChecker
$statements_provider = new StatementsProvider( $statements_provider = new StatementsProvider(
$file_provider, $file_provider,
$cache_provider $cache_provider,
$file_storage_cache_provider
); );
$this->codebase = new Codebase( $this->codebase = new Codebase(

View File

@ -77,7 +77,9 @@ class ClassLikeStorageCacheProvider
$cache_hash = $this->getCacheHash($file_path, $file_contents); $cache_hash = $this->getCacheHash($file_path, $file_contents);
if ($cache_hash !== $cached_value->hash) { if (@get_class($cached_value) === '__PHP_Incomplete_Class'
|| $cache_hash !== $cached_value->hash
) {
unlink($this->getCacheLocationForClass($fq_classlike_name_lc, $file_path)); unlink($this->getCacheLocationForClass($fq_classlike_name_lc, $file_path));
throw new \UnexpectedValueException('Should not be outdated'); throw new \UnexpectedValueException('Should not be outdated');

View File

@ -75,8 +75,10 @@ class FileStorageCacheProvider
$cache_hash = $this->getCacheHash($file_path, $file_contents); $cache_hash = $this->getCacheHash($file_path, $file_contents);
if ($cache_hash !== $cached_value->hash) { if (@get_class($cached_value) === '__PHP_Incomplete_Class'
unlink($this->getCacheLocationForPath($file_path)); || $cache_hash !== $cached_value->hash
) {
$this->removeCacheForFile($file_path);
return null; return null;
} }
@ -84,6 +86,20 @@ class FileStorageCacheProvider
return $cached_value; return $cached_value;
} }
/**
* @param string $file_path
*
* @return void
*/
public function removeCacheForFile($file_path)
{
$cache_path = $this->getCacheLocationForPath($file_path);
if (file_exists($cache_path)) {
unlink($cache_path);
}
}
/** /**
* @param string $file_path * @param string $file_path
* @param string $file_contents * @param string $file_contents

View File

@ -28,4 +28,8 @@ class NoFileStorageCacheProvider extends \Psalm\Provider\FileStorageCacheProvide
public function getLatestFromCache($file_path, $file_contents) public function getLatestFromCache($file_path, $file_contents)
{ {
} }
public function removeCacheForFile($file_path)
{
}
} }

View File

@ -15,15 +15,30 @@ class StatementsProvider
*/ */
private $cache_provider; private $cache_provider;
/**
* @var int
*/
private $this_modified_time;
/**
* @var FileStorageCacheProvider
*/
private $file_storage_cache_provider;
/** /**
* @var PhpParser\Parser|null * @var PhpParser\Parser|null
*/ */
protected static $parser; protected static $parser;
public function __construct(FileProvider $file_provider, ParserCacheProvider $cache_provider) public function __construct(
{ FileProvider $file_provider,
ParserCacheProvider $cache_provider,
FileStorageCacheProvider $file_storage_cache_provider
) {
$this->file_provider = $file_provider; $this->file_provider = $file_provider;
$this->cache_provider = $cache_provider; $this->cache_provider = $cache_provider;
$this->this_modified_time = filemtime(__FILE__);
$this->file_storage_cache_provider = $file_storage_cache_provider;
} }
/** /**
@ -36,7 +51,7 @@ class StatementsProvider
{ {
$from_cache = false; $from_cache = false;
$version = 'parsercache5'; $version = 'parsercache' . $this->this_modified_time;
$file_contents = $this->file_provider->getContents($file_path); $file_contents = $this->file_provider->getContents($file_path);
$modified_time = $this->file_provider->getModifiedTime($file_path); $modified_time = $this->file_provider->getModifiedTime($file_path);
@ -56,6 +71,7 @@ class StatementsProvider
} }
$stmts = self::parseStatementsInFile($file_contents); $stmts = self::parseStatementsInFile($file_contents);
$this->file_storage_cache_provider->removeCacheForFile($file_path);
} else { } else {
$from_cache = true; $from_cache = true;
} }