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

IfStatementAssignment False Positives on Function Signature #678

Closed
6 tasks done
drmmr763 opened this issue Oct 8, 2019 · 3 comments
Closed
6 tasks done

IfStatementAssignment False Positives on Function Signature #678

drmmr763 opened this issue Oct 8, 2019 · 3 comments

Comments

@drmmr763
Copy link

drmmr763 commented Oct 8, 2019

  • PHPMD version: 2.7.0
  • PHP Version: PHP 7.2.19
  • Installation type: composer
  • Operating System / Distribution & Version: (Windows 10 / CentOS 7.6 / ...): Ubuntu

Current Behavior

Rule ifstatementassignment is thrown on a function signature that does not contain any if-statement assignments.

Full Function where Rule is Thrown:

public function setOrganizations() : AssociateRecords
    {
        if ($this->request->has('organizations')) {
            // get the user's current organizations so we can properly set updated user / date
            $currentOrganizations = $this->user->organizations->keyBy('id');
            $organizationIDs      = Organization::findMany($this->request->get('organizations'))
                                    ->pluck('id');
            $toSync               = collect();

            // loop over each ID and update or modify the record
            $organizationIDs->each(function ($id) use ($currentOrganizations, $toSync) {
                $createdAt = new \DateTime;
                $createdBy = auth()->id();

                if ($currentOrganizations->has($id)) {
                    /** @var Organization $org */
                    $org       = $currentOrganizations->get($id);
                    $createdAt = $org->pivot->created_at;
                    $createdBy = $org->pivot->created_by;
                }

                $toSync->put($id, [
                    'created_at'  => $createdAt,
                    'created_by'  => $createdBy,
                    'updated_at'  => new \DateTime(),
                    'modified_by' => auth()->id(),
                ]);
            });

            // sync
            $this->user->organizations()->sync($toSync->toArray());
        }

        return $this;
    }

Expected Behavior

The rule should not be thrown in the example code above. I'm not sure exactly what is causing it to trigger. I thought it was the type hinted return type but removing that didn't resolve it.

Steps To Reproduce:

Explain all the steps you did to create this bug so we can reproduce it.

  • I have been using phpmd on my code base for quite a while and not encountered this violation before
  • my code base has not changed significantly and these lines are not newly created. I'm not sure why this just started happening, it popped up in a CI / CD build recently.
  • I updated to the latest version of phpmd 2.7 in hopes that it would be resolved, but that did not help.

Checks before submitting

  • Be sure that there isn't already an issue about this. See: Issues list
  • Be sure that there isn't already a pull request about this. See: Pull requests
  • I have added every step to reproduce the bug.
  • If possible I added relevant code examples.
  • This issue is about 1 bug and nothing more.
  • The issue has a descriptive title. For example: "JSON rendering failed on Windows for filenames with space".

image

@kylekatarnls
Copy link
Member

kylekatarnls commented Oct 8, 2019

Hi, this is fixed and will be released in 2.7.1. See issue #661 and fix #671

Thanks.

@drmmr763
Copy link
Author

drmmr763 commented Oct 8, 2019

Thanks! Sorry for the duplicate. I saw those but thought they were fixed in 2.7.0.

@kylekatarnls
Copy link
Member

Try to require "phpmd/phpmd": "dev-master", if you have your bug with it, I would re-open the issue.

@ravage84 ravage84 added this to the 2.x (unspecific) milestone May 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

3 participants