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

CallToFunctionStatementWithoutSideEffectsRule checks the purity of array_filter(), array_map(), and array_reduce() #3106

Closed

Conversation

zonuexe
Copy link
Contributor

@zonuexe zonuexe commented May 29, 2024

resolve phpstan/phpstan#11101

The following functions can also cause side effects via callbacks, but since they are used infrequently and the processing is more complex, it is sufficient to follow the existing functionMetadata.

'array_udiff' => ['hasSideEffects' => false],
'array_udiff_assoc' => ['hasSideEffects' => false],
'array_udiff_uassoc' => ['hasSideEffects' => false],
'array_uintersect' => ['hasSideEffects' => false],
'array_uintersect_assoc' => ['hasSideEffects' => false],
'array_uintersect_uassoc' => ['hasSideEffects' => false],
'array_unique' => ['hasSideEffects' => false],
'array_unshift' => ['hasSideEffects' => true],

@staabm
Copy link
Contributor

staabm commented May 29, 2024

Another case which should be tested: phpstan/phpstan#11100

@@ -19,6 +19,11 @@ public function noEffect(string $url, $resourceOrNull)
var_export([], return: true);
print_r([], return: true);
highlight_string($url, return: true);
array_filter([], callback: 'is_string');
Copy link
Contributor

@staabm staabm May 29, 2024

Choose a reason for hiding this comment

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

We should test cases in which the callback is impure, e.g. because of calls to impure functions or access to global variables or similar from within the callable

@ondrejmirtes
Copy link
Member

It's not sufficient to handle this in a rule. It needs to be considered in NodeScopeResolver, so that the statements produce or not produce impurePoints.

…ray_filter(), array_map(), and array_reduce()
@zonuexe zonuexe force-pushed the feature/array-map-filter-sideeffect branch from b19ad33 to c7a5e59 Compare August 27, 2024 15:58
@ondrejmirtes ondrejmirtes changed the base branch from 1.11.x to 1.12.x August 29, 2024 19:31
@ondrejmirtes
Copy link
Member

This is a wrong direction. We need something like @phpstan-pure-unless-callable-is-impure $callable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

array_filter(), array_map() and array_reduce() should be considered pure functions
3 participants