Skip to content

Commit

Permalink
file_get_contents is definitely an impure function
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Apr 3, 2024
1 parent c16e491 commit 2b5b317
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 32 deletions.
1 change: 1 addition & 0 deletions bin/functionMetadata_original.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
'fgetcsv' => ['hasSideEffects' => true],
'fgets' => ['hasSideEffects' => true],
'fgetss' => ['hasSideEffects' => true],
'file_get_contents' => ['hasSideEffects' => true],
'file_put_contents' => ['hasSideEffects' => true],
'flock' => ['hasSideEffects' => true],
'fopen' => ['hasSideEffects' => true],
Expand Down
1 change: 1 addition & 0 deletions bin/generate-function-metadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public function enterNode(Node $node)
'random_int',
'connection_aborted',
'connection_status',
'file_get_contents',
], true)) {
continue;
}
Expand Down
2 changes: 1 addition & 1 deletion resources/functionMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -878,7 +878,7 @@
'fgetss' => ['hasSideEffects' => true],
'file' => ['hasSideEffects' => false],
'file_exists' => ['hasSideEffects' => false],
'file_get_contents' => ['hasSideEffects' => false],
'file_get_contents' => ['hasSideEffects' => true],
'file_put_contents' => ['hasSideEffects' => true],
'fileatime' => ['hasSideEffects' => false],
'filectime' => ['hasSideEffects' => false],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ class CallToFunctionStatementWithoutSideEffectsRule implements Rule
{

private const SIDE_EFFECT_FLIP_PARAMETERS = [
// functionName => [name, pos, testName, defaultHasSideEffect]
'file_get_contents' => ['context', 2, 'isNotNull', false],
'print_r' => ['return', 1, 'isTruthy', true],
'var_export' => ['return', 1, 'isTruthy', true],
// functionName => [name, pos, testName]
'print_r' => ['return', 1, 'isTruthy'],
'var_export' => ['return', 1, 'isTruthy'],
];

public function __construct(private ReflectionProvider $reflectionProvider)
Expand Down Expand Up @@ -68,7 +67,6 @@ public function processNode(Node $node, Scope $scope): array
$flipParameterName,
$flipParameterPosition,
$testName,
$defaultHasSideEffect,
] = self::SIDE_EFFECT_FLIP_PARAMETERS[$functionName];

$sideEffectFlipped = false;
Expand Down Expand Up @@ -102,7 +100,7 @@ public function processNode(Node $node, Scope $scope): array
}
}

if ($sideEffectFlipped xor $defaultHasSideEffect) {
if (!$sideEffectFlipped) {
return [];
}

Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/data/filesystem-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function test3($fh): void
public function test4(string $path): void
{
if (file_get_contents($path) === 'data') {
assertType('\'data\'', file_get_contents($path));
assertType('string|false', file_get_contents($path));
file_put_contents($path, 'other');
assertType('string|false', file_get_contents($path));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,6 @@ public function testRule(): void
'Call to function sprintf() on a separate line has no effect.',
13,
],
[
'Call to function file_get_contents() on a separate line has no effect.',
14,
],
[
'Call to function file_get_contents() on a separate line has no effect.',
22,
],
[
'Call to function var_export() on a separate line has no effect.',
24,
Expand All @@ -47,22 +39,6 @@ public function testRule(): void
}

$this->analyse([__DIR__ . '/data/function-call-statement-no-side-effects-8.0.php'], [
[
'Call to function file_get_contents() on a separate line has no effect.',
15,
],
[
'Call to function file_get_contents() on a separate line has no effect.',
16,
],
[
'Call to function file_get_contents() on a separate line has no effect.',
17,
],
[
'Call to function file_get_contents() on a separate line has no effect.',
18,
],
[
'Call to function var_export() on a separate line has no effect.',
19,
Expand Down

0 comments on commit 2b5b317

Please sign in to comment.