Skip to content

Commit ad1920c

Browse files
committed
Fix #3910 - improve handling of fgetcsv
1 parent 7ed5e32 commit ad1920c

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

docs/manipulating_code/fixing.md

+1
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ C::foo("hello");
225225
### MissingPropertyType
226226

227227
Running `vendor/bin/psalter --issues=MissingPropertyType` on
228+
228229
```php
229230
<?php
230231
class A {

src/Psalm/Internal/Analyzer/FunctionAnalyzer.php

+23
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,29 @@ public static function getReturnTypeFromCallMapWithArgs(
211211
// Really this should only work on instances we've created with new Foo(),
212212
// but that requires more work
213213
break;
214+
215+
case 'fgetcsv':
216+
$string_type = Type::getString();
217+
$string_type->addType(new Type\Atomic\TNull);
218+
$string_type->ignore_nullable_issues = true;
219+
220+
$call_map_return_type = new Type\Union([
221+
new Type\Atomic\TNonEmptyList(
222+
$string_type
223+
),
224+
new Type\Atomic\TFalse,
225+
new Type\Atomic\TNull
226+
]);
227+
228+
if ($codebase->config->ignore_internal_nullable_issues) {
229+
$call_map_return_type->ignore_nullable_issues = true;
230+
}
231+
232+
if ($codebase->config->ignore_internal_falsable_issues) {
233+
$call_map_return_type->ignore_falsable_issues = true;
234+
}
235+
236+
return $call_map_return_type;
214237
}
215238
}
216239

tests/FunctionCallTest.php

+8
Original file line numberDiff line numberDiff line change
@@ -1304,6 +1304,14 @@ function safeMatch(string $pattern, string $subject, ?array $matches = null, int
13041304
13051305
safeMatch("/a/", "b");'
13061306
],
1307+
'fgetcsv' => [
1308+
'<?php
1309+
$headers = fgetcsv(fopen("test.txt", "r"));
1310+
if (empty($headers)) {
1311+
throw new Exception("invalid headers");
1312+
}
1313+
print_r(array_map("strval", $headers));'
1314+
],
13071315
];
13081316
}
13091317

0 commit comments

Comments
 (0)