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

Removes the method call may remove too much code #1721

Open
flohw opened this issue Sep 2, 2022 · 0 comments
Open

Removes the method call may remove too much code #1721

flohw opened this issue Sep 2, 2022 · 0 comments
Labels

Comments

@flohw
Copy link

flohw commented Sep 2, 2022

Question Answer
Infection version 0.26.14
Test Framework version PHPUnit 9.5.21
PHP version 8.1.1
Platform Docker (archlinux)
Github Repo private

Infection may remove too much code when applying the remove method call rule. I have this code template inside a symfony controller:

public function __invoke(int $prospect, int $audit, int $id)
{
    $this->checkAccess($prospect = $this->getByIdOrThrowNotFound($prospect, Member::class));
    $this->checkOwner($prospect, $audit = $this->getByIdOrThrowNotFound($audit, Audit::class));
    $this->checkAudit($audit, $cart = $this->getByIdOrThrowNotFound($id, Cart::class));

    return $cart;
}

Which is mutated to

public function __invoke(int $prospect, int $audit, int $id)
{
    $this->checkAccess($prospect = $this->getByIdOrThrowNotFound($prospect, Member::class));
    $this->checkOwner($prospect, $audit = $this->getByIdOrThrowNotFound($audit, Audit::class));

    return $cart;
}

So the test fails but not for the good reason: it fails because $cart does not exists, not because of the method call removal.
I think Infection shoud replace by

public function __invoke(int $prospect, int $audit, int $id)
{
    $this->checkAccess($prospect = $this->getByIdOrThrowNotFound($prospect, Member::class));
    $this->checkOwner($prospect, $audit = $this->getByIdOrThrowNotFound($audit, Audit::class));
    $cart = $this->getByIdOrThrowNotFound($id, Cart::class);

    return $cart;
}

I know it could be a bit more complicated than that...

Sorry, I wasn't able to reproduce on infection-php.dev 😕

Log of the api response
URL: http://localhost/api/backend/prospects/2/audits/3/carts/3 (500)

x-debug-exception: Undefined%20variable%20%24cart
x-debug-exception-file: path/to/controller.php
content-type: application/json
vary: Accept
cache-control: no-cache, private
date: Fri, 02 Sep 2022 09:41:45 GMT
x-robots-tag: noindex

{
   "code": 500,
   "message": "Undefined variable $cart"
}

Let me know if I can provide other details I don't have in mind yet.

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

No branches or pull requests

2 participants