Skip to content

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

Closed
zonuexe wants to merge 1 commit into
phpstan:1.12.xfrom
zonuexe:feature/array-map-filter-sideeffect
Closed

CallToFunctionStatementWithoutSideEffectsRule checks the purity of array_filter(), array_map(), and array_reduce()#3106
zonuexe wants to merge 1 commit into
phpstan:1.12.xfrom
zonuexe:feature/array-map-filter-sideeffect

Conversation

@zonuexe

@zonuexe zonuexe commented May 29, 2024

Copy link
Copy Markdown
Contributor

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

staabm commented May 29, 2024

Copy link
Copy Markdown
Contributor

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

@staabm staabm May 29, 2024

Copy link
Copy Markdown
Contributor

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
Copy Markdown
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
Copy Markdown
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

3 participants