Skip to content

Do not mutate clean up functions#1285

Merged
maks-rafalko merged 1 commit intoinfection:masterfrom
exussum12:addSomeWhitelistedFunctions
Aug 2, 2020
Merged

Do not mutate clean up functions#1285
maks-rafalko merged 1 commit intoinfection:masterfrom
exussum12:addSomeWhitelistedFunctions

Conversation

@exussum12
Copy link
Copy Markdown
Contributor

@exussum12 exussum12 commented Jul 28, 2020

Functions like Close and Free should not be removed by the function call
removals. Removing them does not change the tested behavior, but does
free the memory before the script finished / garbage collection is
invoked

This PR:

Copy link
Copy Markdown
Member

@maks-rafalko maks-rafalko left a comment

Choose a reason for hiding this comment

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

Could you please run make cs to fix CS Fixer issues locally?

Saw a couple of such complains on Twitter about fclose and friends, so it makes sense for me.

@maks-rafalko maks-rafalko added this to the 0.17.0 milestone Jul 28, 2020
@maks-rafalko maks-rafalko self-requested a review July 28, 2020 14:05
Comment thread src/Mutator/Removal/FunctionCallRemoval.php Outdated
private $whitelisted = [
'close',
'free',
'free_result',
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm not terribly excited about a special list for instance methods. I wish we could have this either configurable, or completed with the means supposed to exists under #1231

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

If it was configurable you would need this everywhere ?
How about checking that all of these in the method versions take no argument ? eg $someClass->free() would be ignored but $someClass->free($someVariable) would be allowed to mutate ?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think doing so against native calls is ok but extending it to arbitrary classes is not: a user will likely be able to test that its $someClass->free() is called or not

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

They return void though ? https://www.php.net/manual/en/mysqli-result.free.php or https://www.php.net/manual/en/event.free could check if void return and 0 arguments and called one of these things ?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

and how do you tell the difference between $mysqliResult->free() and $repository->free()? We do not have type inference at the moment so we cannot distinguish the two and the later may deserve to be covered by infection

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I agree there will be methods missed by doing this, I don't have the stats but most methods I have seen called one of these are methods which have no real observable behaviour.

Could have this as a user config option instead at project level instead of line level ?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't think a user config option for this list is a bad idea.

@exussum12 exussum12 force-pushed the addSomeWhitelistedFunctions branch from 50811d4 to e6b1e06 Compare July 29, 2020 05:23
@exussum12 exussum12 changed the title Add clean up functions to whitelist Do not mutate clean up functions Jul 29, 2020
Comment thread tests/phpunit/Mutator/Removal/FunctionCallRemovalTest.php Outdated
@maks-rafalko
Copy link
Copy Markdown
Member

maks-rafalko commented Jul 31, 2020

Since we have different opinions on object's methods calls, here is what I suggest:

  • leave only native functions call ignore list in this PR and merge it
  • think about method calls in a separate issue. But: I recommend to read this thread: Allow adding mutation exclusions for specific calls #697. By implementing the feature that is being discussed there will cover your needs (method calls)

Functions like Close and Free should not be removed by the function call
removals. Removing them does not change the tested behaviour, but does
free the memory before the script finished / garbage collection is
invoked
@exussum12 exussum12 force-pushed the addSomeWhitelistedFunctions branch from 4795f26 to 3aaf3fe Compare August 2, 2020 05:14
@exussum12
Copy link
Copy Markdown
Contributor Author

Since we have different opinions on object's methods calls, here is what I suggest:

Sounds good to me, I have updated the PR to be just the native functions side of things here.

@maks-rafalko maks-rafalko merged commit 2933982 into infection:master Aug 2, 2020
@maks-rafalko
Copy link
Copy Markdown
Member

Thank you, @exussum12!

@exussum12 exussum12 deleted the addSomeWhitelistedFunctions branch August 2, 2020 16:33
@lcobucci
Copy link
Copy Markdown
Contributor

Does it make sense to add openssl_free_key() to the list too?

@maks-rafalko
Copy link
Copy Markdown
Member

sounds good to me. Feel free to open a PR targeted to 0.17, so we will release it ASAP as 0.17.1

@lcobucci lcobucci mentioned this pull request Aug 19, 2020
2 tasks
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.

5 participants