Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

False positives in the effect of file_get_contents() #8440

Closed
zonuexe opened this issue Nov 30, 2022 · 1 comment · Fixed by phpstan/phpstan-src#2037
Closed

False positives in the effect of file_get_contents() #8440

zonuexe opened this issue Nov 30, 2022 · 1 comment · Fixed by phpstan/phpstan-src#2037
Labels

Comments

@zonuexe
Copy link
Contributor

zonuexe commented Nov 30, 2022

Bug report

Code snippet that reproduces the problem

The code below is common code for issuing HTTP requests in projects that do not have an HTTP client installed (also known as legacy code).

// In the general case, this line has no effect as detected by PHPStan.
file_get_contents(__DIR__ . '/config');

// This line send a POST request with HTTP, not for GET, as it seems.
file_get_contents(self::URL, false, stream_context_create([
	'http' => [
		'method' => 'POST',
		'header' => 'Content-Type: application/json',
		'content' => json_encode($data, JSON_THROW_ON_ERROR),
	],
]));

https://phpstan.org/r/5ce5f059-a935-4871-9fb0-a2f14e337b35

Expected output

My rule of thumb is that a call passed context argument may have intended side effects.
Continue to warn of side effects when called without context.

Calling file_get_content() without specifying a context will issue a GET method.
Since GET is an idempotent method, the use case of sending a GET request over HTTP to cause side effects is an exception.

Did PHPStan help you today? Did it make you happy in any way?

I like PSR-18 so much that I forgot about file_get_contents() as an HTTP client substitute until I started volunteering to improve my friend's code. PHPStan is also very useful for inspecting such code. ❤️

@github-actions
Copy link

github-actions bot commented Feb 9, 2023

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 9, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants