Skip to content

Commit

Permalink
Fix #1191 - allow Psalm to check against different versions of PHP
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed Feb 7, 2019
1 parent f8f5ddf commit 17541b4
Show file tree
Hide file tree
Showing 31 changed files with 590 additions and 225 deletions.
10 changes: 10 additions & 0 deletions src/Psalm/Codebase.php
Expand Up @@ -159,6 +159,16 @@ class Codebase
*/
public $diff_methods = false;

/**
* @var int
*/
public $php_major_version = PHP_MAJOR_VERSION;

/**
* @var int
*/
public $php_minor_version = PHP_MINOR_VERSION;

/**
* @param bool $collect_references
* @param bool $debug_output
Expand Down
2 changes: 1 addition & 1 deletion src/Psalm/Internal/Analyzer/CommentAnalyzer.php
Expand Up @@ -98,7 +98,7 @@ public static function getTypeFromComment(
}

try {
$defined_type = Type::parseTokens($var_type_tokens, false, $template_type_map ?: []);
$defined_type = Type::parseTokens($var_type_tokens, null, $template_type_map ?: []);
} catch (TypeParseTreeException $e) {
if (is_int($came_from_line_number)) {
throw new DocblockParseException(
Expand Down
Expand Up @@ -702,13 +702,13 @@ private static function addOrUpdateReturnType(
$function
);
$manipulator->setReturnType(
!$docblock_only && $project_analyzer->php_major_version >= 7
!$docblock_only && $project_analyzer->getCodebase()->php_major_version >= 7
? $inferred_return_type->toPhpString(
$source->getNamespace(),
$source->getAliasedClassesFlipped(),
$source->getFQCLN(),
$project_analyzer->php_major_version,
$project_analyzer->php_minor_version
$project_analyzer->getCodebase()->php_major_version,
$project_analyzer->getCodebase()->php_minor_version
) : null,
$inferred_return_type->toNamespacedString(
$source->getNamespace(),
Expand Down
6 changes: 3 additions & 3 deletions src/Psalm/Internal/Analyzer/FunctionLikeAnalyzer.php
Expand Up @@ -875,13 +875,13 @@ private function addOrUpdateParamType(
);
$manipulator->setParamType(
$param_name,
!$docblock_only && $project_analyzer->php_major_version >= 7
!$docblock_only && $project_analyzer->getCodebase()->php_major_version >= 7
? $inferred_return_type->toPhpString(
$this->source->getNamespace(),
$this->source->getAliasedClassesFlipped(),
$this->source->getFQCLN(),
$project_analyzer->php_major_version,
$project_analyzer->php_minor_version
$project_analyzer->getCodebase()->php_major_version,
$project_analyzer->getCodebase()->php_minor_version
) : null,
$inferred_return_type->toNamespacedString(
$this->source->getNamespace(),
Expand Down
29 changes: 15 additions & 14 deletions src/Psalm/Internal/Analyzer/ProjectAnalyzer.php
Expand Up @@ -108,16 +108,6 @@ class ProjectAnalyzer
*/
private $issues_to_fix = [];

/**
* @var int
*/
public $php_major_version = PHP_MAJOR_VERSION;

/**
* @var int
*/
public $php_minor_version = PHP_MINOR_VERSION;

/**
* @var bool
*/
Expand Down Expand Up @@ -743,20 +733,31 @@ public function fileExists($file_path)
* @return void
*/
public function alterCodeAfterCompletion(
$php_major_version,
$php_minor_version,
$dry_run = false,
$safe_types = false
) {
$this->codebase->alter_code = true;
$this->codebase->infer_types_from_usage = true;
$this->show_issues = false;
$this->php_major_version = $php_major_version;
$this->php_minor_version = $php_minor_version;
$this->dry_run = $dry_run;
$this->only_replace_php_types_with_non_docblock_types = $safe_types;
}

/**
* @return void
*/
public function setPhpVersion(string $version)
{
if (!preg_match('/^(5\.[456]|7\.[01234])(\..*)?$/', $version)) {
throw new \UnexpectedValueException('Expecting a version number in the format x.y');
}

list($php_major_version, $php_minor_version) = explode('.', $version);

$this->codebase->php_major_version = (int) $php_major_version;
$this->codebase->php_minor_version = (int) $php_minor_version;
}

/**
* @param array<string, bool> $issues
*
Expand Down
2 changes: 1 addition & 1 deletion src/Psalm/Internal/CallMap.php
Expand Up @@ -4171,7 +4171,7 @@
'hash_hmac' => ['string', 'algo'=>'string', 'data'=>'string', 'key'=>'string', 'raw_output='=>'bool'],
'hash_hmac_algos' => ['array<int,string>'],
'hash_hmac_file' => ['string', 'algo'=>'string', 'filename'=>'string', 'key'=>'string', 'raw_output='=>'bool'],
'hash_init' => ['HashContext|resource', 'algo'=>'string', 'options='=>'int', 'key='=>'string'],
'hash_init' => ['HashContext', 'algo'=>'string', 'options='=>'int', 'key='=>'string'],
'hash_pbkdf2' => ['string', 'algo'=>'string', 'password'=>'string', 'salt'=>'string', 'iterations'=>'int', 'length='=>'int', 'raw_output='=>'bool'],
'hash_update' => ['bool', 'context'=>'HashContext', 'data'=>'string'],
'hash_update_file' => ['bool', 'context'=>'HashContext', 'filename'=>'string', 'scontext='=>'?HashContext'],
Expand Down
51 changes: 51 additions & 0 deletions src/Psalm/Internal/CallMap_71_delta.php
@@ -0,0 +1,51 @@
<?php // phpcs:ignoreFile

/**
* This contains the information needed to convert the function signatures for php 7.1 to php 7.0 (and vice versa)
*
* This has two sections.
* The 'new' section contains function/method names from FunctionSignatureMap (And alternates, if applicable) that do not exist in php7.0 or have different signatures in php 7.1.
* If they were just updated, the function/method will be present in the 'added' signatures.
* The 'old' signatures contains the signatures that are different in php 7.0.
* Functions are expected to be removed only in major releases of php. (e.g. php 7.0 removed various functions that were deprecated in 5.6)
*
* @see FunctionSignatureMap.php
*
* @phan-file-suppress PhanPluginMixedKeyNoKey (read by Phan when analyzing this file)
*/
return [
'new' => [
'Closure::fromCallable' => ['Closure', 'callable'=>'callable'],
'SQLite3::createFunction' => ['bool', 'name'=>'string', 'callback'=>'callable', 'argument_count='=>'int', 'flags='=>'int'],
'curl_multi_errno' => ['int', 'mh'=>'resource'],
'curl_share_errno' => ['int', 'sh'=>'resource'],
'curl_share_strerror' => ['string', 'code'=>'int'],
'get_headers' => ['array|false', 'url'=>'string', 'format='=>'int', 'context='=>'resource'],
'getenv\'1' => ['array<string,string>'],
'getopt' => ['array<string,string>|array<string,false>|array<string,array<int,string|false>>', 'options'=>'string', 'longopts='=>'array', '&w_optind='=>'int'],
'hash_hkdf' => ['string', 'algo'=>'string', 'ikm'=>'string', 'length='=>'int', 'info='=>'string', 'salt='=>'string'],
'is_iterable' => ['bool', 'var'=>'mixed'],
'openssl_get_curve_names' => ['array<int,string>'],
'pcntl_async_signals' => ['bool', 'on='=>'bool'],
'pcntl_signal_get_handler' => ['int|string', 'signo'=>'int'],
'pg_fetch_all' => ['array', 'result'=>'resource', 'result_type='=>'int'],
'pg_last_error' => ['string', 'connection='=>'resource', 'operation='=>'int'],
'pg_select' => ['mixed', 'db'=>'resource', 'table'=>'string', 'ids'=>'array', 'options='=>'int', 'result_type='=>'int'],
'sapi_windows_cp_conv' => ['string', 'in_codepage'=>'int|string', 'out_codepage'=>'int|string', 'subject'=>'string'],
'sapi_windows_cp_get' => ['int'],
'sapi_windows_cp_is_utf8' => ['bool'],
'sapi_windows_cp_set' => ['bool', 'code_page'=>'int'],
'session_create_id' => ['string', 'prefix='=>'string'],
'session_gc' => ['int'],
'unpack' => ['array', 'format'=>'string', 'data'=>'string', 'offset='=>'int'],
],
'old' => [
'SQLite3::createFunction' => ['bool', 'name'=>'string', 'callback'=>'callable', 'argument_count='=>'int'],
'get_headers' => ['array|false', 'url'=>'string', 'format='=>'int'],
'getopt' => ['array<string,string>|array<string,false>|array<string,array<int,string|false>>', 'options'=>'string', 'longopts='=>'array'],
'pg_fetch_all' => ['array', 'result'=>'resource'],
'pg_last_error' => ['string', 'connection='=>'resource'],
'pg_select' => ['mixed', 'db'=>'resource', 'table'=>'string', 'ids'=>'array', 'options='=>'int'],
'unpack' => ['array', 'format'=>'string', 'data'=>'string'],
],
];

0 comments on commit 17541b4

Please sign in to comment.