Search code examples
phpphpdoc

Why does PHPDoc error in PHPStorm over this code?


In the following code the @return is underlined red. I have it expecting an interface to be returned because that is what all of the different Vendor adapters implement.

 /**
 * VendorFactory constructor.
 * @param Model $model
 * @return \Traders\Interfaces\VendorAdapterInterface
 */
public function __construct(Model $model)
{
    return $this->createAdapter($model);
}

This is the code for the createAdapter which does not have the @return underlined in red.

/**
 * @param Model $model
 * @return \Traders\Interfaces\VendorAdapterInterface
 */
public function createAdapter(Model $model)
{
    $type = str_replace('App\Models\\', '', get_class($model)).'s';
    $fqcn = '\Traders\Adapters\\'.$type.'\\'.ucfirst(strtolower($model->name));
    return new $fqcn($model);
}

I have tried doing the /** docblock and letting PHPStorm enter what it believes is the return value and it just keeps giving me

@return mixed

Solution

  • Your problem is the return in the constructor. Constructors do not take return values, they get executed when an instance of that class is instantiated.