13

I can't understand why PhpStorm gives me the following warning PHPDoc comment does not match function or method signature over this method:

/**
 * Create a new instance of the class
 * @param string $classname Class to instantiate
 * @return object the instance
 * @throw FactoryException If the class is not instantiable
 */
private function newInstance($classname) {
    $reflectionClass = new \ReflectionClass($classname);
    if (! $reflectionClass->isInstantiable()) {
        throw new FactoryException("The class $classname is not instantiable.");
    }
    return new $classname;
}

The warning isn't very specific, I've tried several things like changing the return type to "Object", "mixed" or even "int" (to try) but it didn't change. What is the problem here ?

Matthieu Napoli
  • 48,448
  • 45
  • 173
  • 261

3 Answers3

11

It should be @throws not @throw.

If you just type /** over the line of a function or class var declaration it'll auto insert a base PHPDoc for you. That's how I noticed the difference.

enter image description here

MetalFrog
  • 9,943
  • 1
  • 22
  • 24
  • 1
    I tried `@throws` but the warning is still there (I did the same as you did with typing `/**` but even the auto-generated docblock had the warning). Actually I have a beta version of PhpStorm 4.0, maybe it's just a bug? – Matthieu Napoli Mar 22 '12 at 14:18
  • Funky, yeah I'm using 4.0 as well. – MetalFrog Mar 22 '12 at 14:19
  • 1
    @Matthieu 1) It definitely should be **throws** 2) There are few issues with current v4 EAP build (116.101) in this regard (I'm having the same). From what I see (based on your comments) this function is in class that extends another class or implements interface and this function overrides one in parent class/interface. If so -- please compare PHPDoc blocks for them -- even if 1 character is different (e.g. "Create a new instance ..." vs "Create new instance ...") PhpStorm will report such error. – LazyOne Mar 22 '12 at 15:12
  • 2
    New EAP build should be available later today or tomorrow .. but I do not expect this to be fixed there (at least based on related tickets statuses). – LazyOne Mar 22 '12 at 15:13
  • 1
    OK I'll go with a bug then, even though one this method there is no overriding, that is the cas for another method of this class (implementing an interface). – Matthieu Napoli Mar 22 '12 at 19:17
1

Please refer to this link https://blog.jetbrains.com/webide/2011/05/phpdoc-inspections/ It is mentioned there that "The inspection reports a problem if a number of parameters described in PHPDoc comment and/or their types do not match a corresponding function or method declaration"

1

If this method happens to implement/override from a parent class where a docblock exists for it, see if your tags match across both. Normally, tags in the parent will be inherited by the child, such that if the parent's method docblock already has those same tags (param, return, throws), then it is not necessary to list them in the child's docblock, unless they specifically need to say something different than the parent's does.

ashnazg
  • 6,558
  • 2
  • 32
  • 39