问题
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 ?
回答1:
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.
回答2:
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.
回答3:
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"
来源:https://stackoverflow.com/questions/9823085/why-do-i-get-a-phpdoc-warning-in-phpstorm-over-this-code