Giter VIP home page Giter VIP logo

Comments (4)

sirbrillig avatar sirbrillig commented on May 14, 2024

Are you sure this is a bug? I think that actually it's a correct warning. See the section titled "When calling static methods, the function call is stronger than the static property operator" in this PHP doc. I actually had to fix this behavior in #27.

In your example, return new self::$defaultClass(); will first evaluate $defaultClass and then look for a static property on self which matches that name. However, $defaultClass is not defined.

Here's a simplified example which you can run to see the failure:

<?php
class Foobar {
	private static $name = 'doSomething';
	public static function doSomething() {
		return 'hi';
	}
	public static function proxy() {
		return self::$name();
	}
}
echo Foobar::proxy();

Or view it here: https://repl.it/repls/DefiantCrowdedTabs

If I'm misunderstanding or I'm wrong (always possible!) please feel free to re-open the issue.

from phpcs-variable-analysis.

 avatar commented on May 14, 2024

Hi @sirbrillig and thanks for response, I was trying to change the code after your comment and I discovered this...

Why does my previous code throw a warning and this does not?

<?php

class MyClass extends MyForms
{
    private static $defaultClass = 'OtherClass';

    final public static function getForm($class = false)
    {
        $defaultClass = self::$defaultClass;

        if ($class !== false) {
            return new $class();
        }

        return new self::$defaultClass();
    }
}

from phpcs-variable-analysis.

sirbrillig avatar sirbrillig commented on May 14, 2024

That example is not an issue, I think, because in that case, $defaultClass is defined by the time it's used. PHP is weird this way.

In the second example, you're creating a local variable called $defaultClass, which you're assigning with the value of self::$defaultClass, which evaluates to the static property and thus has the value of 'OtherClass'. Then you're returning self::$defaultClass(). Because the function call is stronger than the static reference, essentially that means that those parenthesis evaluate first, before the self:: part. So self::$defaultClass() first evaluates $defaultClass(). In the original code, that was causing an error (and also, the sniff was warning you about it) since $defaultClass didn't exist, but in the second example, it does exist, so it will be evaluated into OtherClass(). That function does not exist (it's a class, not a function), so you'll probably get a PHP error, but it's a different error and one that this sniff cannot detect because it only looks to make sure variables exist, not functions.

I understand what you're trying to do: dynamically access the static property and then use that as a class name. This is tricky to do in PHP, though. I'm not sure if it will work, but maybe try this:

<?php

class MyClass extends MyForms
{
    private static $defaultClass = 'OtherClass';

    final public static function getForm($class = false)
    {
        if ($class !== false) {
            return new $class();
        }

        return new (self::$defaultClass)();
    }
}

from phpcs-variable-analysis.

 avatar commented on May 14, 2024

Thank u so much @sirbrillig, we will review it!

from phpcs-variable-analysis.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.