diff --git a/bin/functionMetadata_original.php b/bin/functionMetadata_original.php index 6e71398632..60f8e8198c 100644 --- a/bin/functionMetadata_original.php +++ b/bin/functionMetadata_original.php @@ -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], diff --git a/bin/generate-function-metadata.php b/bin/generate-function-metadata.php index 404e742aa6..f6f5131a53 100755 --- a/bin/generate-function-metadata.php +++ b/bin/generate-function-metadata.php @@ -85,6 +85,7 @@ public function enterNode(Node $node) 'random_int', 'connection_aborted', 'connection_status', + 'file_get_contents', ], true)) { continue; } diff --git a/resources/functionMetadata.php b/resources/functionMetadata.php index 55f2ac4419..08a416412c 100644 --- a/resources/functionMetadata.php +++ b/resources/functionMetadata.php @@ -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], diff --git a/src/Rules/Functions/CallToFunctionStatementWithoutSideEffectsRule.php b/src/Rules/Functions/CallToFunctionStatementWithoutSideEffectsRule.php index 545bcd1a27..a073f11f6b 100644 --- a/src/Rules/Functions/CallToFunctionStatementWithoutSideEffectsRule.php +++ b/src/Rules/Functions/CallToFunctionStatementWithoutSideEffectsRule.php @@ -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) @@ -68,7 +67,6 @@ public function processNode(Node $node, Scope $scope): array $flipParameterName, $flipParameterPosition, $testName, - $defaultHasSideEffect, ] = self::SIDE_EFFECT_FLIP_PARAMETERS[$functionName]; $sideEffectFlipped = false; @@ -102,7 +100,7 @@ public function processNode(Node $node, Scope $scope): array } } - if ($sideEffectFlipped xor $defaultHasSideEffect) { + if (!$sideEffectFlipped) { return []; } diff --git a/tests/PHPStan/Analyser/data/filesystem-functions.php b/tests/PHPStan/Analyser/data/filesystem-functions.php index 988fbc8dc3..fc7614c63a 100644 --- a/tests/PHPStan/Analyser/data/filesystem-functions.php +++ b/tests/PHPStan/Analyser/data/filesystem-functions.php @@ -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)); } diff --git a/tests/PHPStan/Rules/Functions/CallToFunctionStatementWithoutSideEffectsRuleTest.php b/tests/PHPStan/Rules/Functions/CallToFunctionStatementWithoutSideEffectsRuleTest.php index 163038e8d9..3e6261d247 100644 --- a/tests/PHPStan/Rules/Functions/CallToFunctionStatementWithoutSideEffectsRuleTest.php +++ b/tests/PHPStan/Rules/Functions/CallToFunctionStatementWithoutSideEffectsRuleTest.php @@ -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, @@ -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,