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

Fix getenv calls

Fixes #386
This commit is contained in:
Matthew Brown 2018-01-08 00:09:22 -05:00
parent bb41b92789
commit 8e4f8fabe3
3 changed files with 18 additions and 1 deletions

View File

@ -2484,7 +2484,7 @@ return [
'get_defined_constants' => ['array', 'categorize='=>'bool'],
'get_defined_functions' => ['string[][]', 'exclude_disabled='=>'bool'],
'get_defined_vars' => ['array'],
'getenv' => ['string|string[]', 'varname='=>'string', 'local_only='=>'bool'],
'getenv' => ['string|string[]|false', 'varname='=>'string', 'local_only='=>'bool'],
'get_extension_funcs' => ['array', 'extension_name'=>'string'],
'get_headers' => ['array', 'url'=>'string', 'format='=>'int'],
'gethostbyaddr' => ['string', 'ip_address'=>'string'],

View File

@ -365,6 +365,14 @@ class FunctionChecker extends FunctionLikeChecker
throw new \InvalidArgumentException('Function ' . $function_id . ' was not found in callmap');
}
if ($call_map_key === 'getenv') {
if (count($call_args)) {
return new Type\Union([new Type\Atomic\TString, new Type\Atomic\TFalse]);
}
return new Type\Union([new Type\Atomic\TArray([new Type\Atomic\TMixed, new Type\Atomic\TString])]);
}
if ($call_args) {
if (in_array($call_map_key, ['str_replace', 'preg_replace', 'preg_replace_callback'], true)) {
if (isset($call_args[2]->value->inferredType)) {

View File

@ -396,6 +396,15 @@ class FunctionCallTest extends TestCase
a(["a" => "hello"]);',
],
'getenv' => [
'<?php
$a = getenv();
$b = getenv("some_key");',
'assertions' => [
'$a' => 'array<mixed, string>',
'$b' => 'string|false',
],
],
];
}