Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/Reflection/SignatureMap/NativeFunctionReflectionProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
use PHPStan\Reflection\Native\NativeFunctionReflection;
use PHPStan\Reflection\Native\NativeParameterReflection;
use PHPStan\TrinaryLogic;
use PHPStan\Type\ArrayType;
use PHPStan\Type\BooleanType;
use PHPStan\Type\FloatType;
use PHPStan\Type\Generic\TemplateTypeMap;
use PHPStan\Type\IntegerType;
use PHPStan\Type\NullType;
use PHPStan\Type\StringAlwaysAcceptingObjectWithToStringType;
use PHPStan\Type\StringType;
use PHPStan\Type\UnionType;

class NativeFunctionReflectionProvider
Expand Down Expand Up @@ -63,6 +65,26 @@ public function findFunctionReflection(string $functionName): ?NativeFunctionRef
new BooleanType(),
]);
}

if (
$parameterSignature->getName() === 'fields'
&& $lowerCasedFunctionName === 'fputcsv'
) {
$type = new ArrayType(
new UnionType([
new StringType(),
new IntegerType(),
]),
new UnionType([
new StringAlwaysAcceptingObjectWithToStringType(),
new IntegerType(),
new FloatType(),
new NullType(),
new BooleanType(),
])
);
}

return new NativeParameterReflection(
$parameterSignature->getName(),
$parameterSignature->isOptional(),
Expand Down
22 changes: 22 additions & 0 deletions tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,28 @@ public function testUnpackOperator(): void
]);
}

public function testFputCsv(): void
{
$this->analyse([__DIR__ . '/data/fputcsv-fields-parameter.php'], [
[
'Parameter #2 $fields of function fputcsv expects array<int|string, bool|float|int|string|null>, array<int, Fputcsv\Person> given.',
35,
],
]);
}


public function testPutCsvWithStringable(): void
{
if (PHP_VERSION_ID < 80000) {
$this->markTestSkipped('Test skipped on lower version than 8.0 (needs Stringable interface, added in PHP8)');
}

$this->analyse([__DIR__ . '/data/fputcsv-fields-parameter-php8.php'], [
// No issues expected
]);
}

public function testFunctionWithNumericParameterThatCreatedByAddition(): void
{
$this->analyse([__DIR__ . '/data/function-with-int-parameter-that-created-by-addition.php'], [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php declare(strict_types = 1);

namespace FputcsvPhp8;

class StringablePerson implements \Stringable {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't put these test files in global namespace. Some namespace like "namespace FputcsvPhp8" would work.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added

public function __toString(): string {
return "stringable name";
}
}

class CsvWriterPhp8
{
/** @param resource $handle */
public function write($handle): void
{
// This is valid. StringablePerson should be treated as a string
fputcsv($handle, [new StringablePerson()]);
}
}
40 changes: 40 additions & 0 deletions tests/PHPStan/Rules/Functions/data/fputcsv-fields-parameter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php declare(strict_types=1);

namespace Fputcsv;

class Person
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't put these test files in global namespace. Some namespace like "namespace FputcsvTest" would work.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added

{
}

class PersonWithToString
{
public function __toString(): string
{
return "to string name";
}
}

class CsvWriter
{
/** @param resource $handle */
public function writeCsv($handle): void
{
// These are all valid scalers
fputcsv($handle, [
"string",
1,
3.5,
true,
false,
null, // Yes this is accepted by fputcsv,
]);

// Arrays can have string for keys (which are ignored)
fputcsv($handle, ["foo" => "bar",]);

fputcsv($handle, [new Person,]); // Problem on this line

// This is valid. PersonWithToString should be cast to string by fputcsv
fputcsv($handle, [new PersonWithToString()]);
}
}