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

Anonymous class defined in a static function produces UndefinedVariable for each $this access #1008

Open
6 tasks done
MircoBabin opened this issue May 30, 2023 · 0 comments
Open
6 tasks done

Comments

@MircoBabin
Copy link

MircoBabin commented May 30, 2023

  • PHPMD version: PHPMD 2.13.0snapshot202209100847
  • PHP Version: 8.1.18 64bit
  • Installation type: PHAR
  • Operating System / Distribution & Version: Windows/11

Current Behavior

An anonymous class defined in a static class function is not recognized. Resulting in "UndefinedVariable Avoid using undefined variables such as '$this' which will lead to PHP notices." for each $this access.

Expected Behavior

No output/errors/warnings.

Steps To Reproduce:

  1. Create file phpmd-anonymous.php with contents:
<?php
class MyClass
{
    private static function createListQuery($filter, $options, $recs, $createFromOrderRequest, $filterAccepted, $customerEndtimeFrom, $customerEndtimeTo)
    {
        return new class($filter, $options, $recs, $createFromOrderRequest, $filterAccepted, $customerEndtimeFrom, $customerEndtimeTo) {
            private $filter;
            private $options;
            private $recs;
            private $createFromOrderRequest;
            private $filterAccepted;
            private $customerEndtimeFrom;
            private $customerEndtimeTo;

            public function __construct($filter, $options, $recs, $createFromOrderRequest, $filterAccepted, $customerEndtimeFrom, $customerEndtimeTo)
            {
                $this->filter = $filter;
                $this->options = $options;
                $this->recs = $recs;
                $this->createFromOrderRequest = $createFromOrderRequest;
                $this->filterAccepted = $filterAccepted;
                $this->customerEndtimeFrom = $customerEndtimeFrom;
                $this->customerEndtimeTo = $customerEndtimeTo;
            }
        };
    }
}    
  1. Run php phpmd.phar phpmd-anonymous.php text phpmd_config.xml

phpmd_config.xml contains:

<?xml version="1.0"?>

<ruleset name="Clean Code Rules"
         xmlns="http://pmd.sf.net/ruleset/1.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0 http://pmd.sf.net/ruleset_xml_schema.xsd"
         xsi:noNamespaceSchemaLocation="http://pmd.sf.net/ruleset_xml_schema.xsd">

    <description>
IfStatementAssignment, MissingImport, UndefinedVariable
    </description>

    <rule name="IfStatementAssignment"
          since="2.7.0"
          message="Avoid assigning values to variables in if clauses and the like (line '{0}', column '{1}')."
          class="PHPMD\Rule\CleanCode\IfStatementAssignment"
          externalInfoUrl="http://phpmd.org/rules/cleancode.html#ifstatementassignment">
        <description>
            <![CDATA[
Assignments in if clauses and the like are considered a code smell.
Assignments in PHP return the right operand as their result.
In many cases, this is an expected behavior, but can lead
to many difficult to spot bugs, especially when the right
operand could result in zero, null or an empty string and the like.
            ]]>
        </description>
        <priority>1</priority>
        <properties></properties>
        <example>
            <![CDATA[
class Foo
{
    public function bar($flag)
    {
        if ($foo = 'bar') { // possible typo
            // ...
        }
        if ($baz = 0) { // always false
            // ...
        }
    }
}
            ]]>
        </example>
    </rule>

    <rule name="MissingImport"
          since="2.7.0"
          message="Missing class import via use statement (line '{0}', column '{1}')."
          class="PHPMD\Rule\CleanCode\MissingImport"
          externalInfoUrl="http://phpmd.org/rules/cleancode.html#MissingImport">
        <description>
            <![CDATA[
Importing all external classes in a file through use statements makes them clearly visible.
            ]]>
        </description>
        <priority>1</priority>
        <properties>
            <property name="ignore-global" value="false" description="Ignore classes in the global namespace" />
        </properties>        
        <example>
            <![CDATA[
function make() {
    return new \stdClass();
}
            ]]>
        </example>
    </rule>

    <rule name="UndefinedVariable"
          since="2.8.0"
          message="Avoid using undefined variables such as '{0}' which will lead to PHP notices."
          class="PHPMD\Rule\CleanCode\UndefinedVariable"
          externalInfoUrl="">
        <description>
            Detects when a variable is used that has not been defined before.
        </description>
        <priority>3</priority>
        <example>
            <![CDATA[
class Foo
{
    private function bar()
    {
        // $message is undefined
        echo $message;
    }
}
]]>
        </example>
    </rule>

</ruleset>
  1. Observe the following output
phpmd-anonymous.php:17  UndefinedVariable  Avoid using undefined variables such as '$this' which will lead to PHP notices.
phpmd-anonymous.php:18  UndefinedVariable  Avoid using undefined variables such as '$this' which will lead to PHP notices.
phpmd-anonymous.php:19  UndefinedVariable  Avoid using undefined variables such as '$this' which will lead to PHP notices.
phpmd-anonymous.php:20  UndefinedVariable  Avoid using undefined variables such as '$this' which will lead to PHP notices.
phpmd-anonymous.php:21  UndefinedVariable  Avoid using undefined variables such as '$this' which will lead to PHP notices.
phpmd-anonymous.php:22  UndefinedVariable  Avoid using undefined variables such as '$this' which will lead to PHP notices.
phpmd-anonymous.php:23  UndefinedVariable  Avoid using undefined variables such as '$this' which will lead to PHP notices.

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".
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant