具有扩展工厂模式的PHPDoc返回类型
我想使用一个抽象基类来扩展工厂的通用功能,这是可行的,但我不知道如何准确地指定返回类型并让PHPStorm检测它 这里有一个例子。是否有一种方法可以在PHPDoc中记录AppleFactory::make()返回AppleInterface和OrangeFactory::make()返回OrangeInterface具有扩展工厂模式的PHPDoc返回类型,php,phpstorm,factory-pattern,phpdoc,Php,Phpstorm,Factory Pattern,Phpdoc,我想使用一个抽象基类来扩展工厂的通用功能,这是可行的,但我不知道如何准确地指定返回类型并让PHPStorm检测它 这里有一个例子。是否有一种方法可以在PHPDoc中记录AppleFactory::make()返回AppleInterface和OrangeFactory::make()返回OrangeInterface <?php namespace App\Factories; abstract class AbstractFactory { /** @var array $d
<?php
namespace App\Factories;
abstract class AbstractFactory {
/** @var array $drivers */
protected $drivers;
/**
* instantiate the driver based on the given driver identifier
* @param string $driver Driver identifier.
* @return ???
* @throws UnknownDriverException If driver string is not in list of available drivers.
*/
public function make($driver) {
$class = $this->className($driver);
if (is_null($class))
throw new UnknownDriverException($driver);
return new $class;
}
/**
* get the full class name for the driver
* @param string $driver String mapping of class.
* @return string
*/
public function className($driver) {
return isset($this->drivers[$driver]) ? $this->drivers[$driver] : null;
}
}
class AppleFactory extends AbstractFactory {
protected $drivers = [
// both implement AppleInterface
'fuji' => \App\Apples\Fuji::class,
'gala' => \App\Apples\Gala::class
];
}
class OrangeFactory extends AbstractFactory {
protected $drivers = [
// both implement OrangeInterface
'navel' => \App\Oranges\Navel::class,
'blood' => \App\Oranges\Blood::class
];
}
是否有一种方法可以在PHPDoc中记录AppleFactory::make()
返回AppleInterface和OrangeFactory::make()
返回OrangeInterface
<?php
namespace App\Factories;
abstract class AbstractFactory {
/** @var array $drivers */
protected $drivers;
/**
* instantiate the driver based on the given driver identifier
* @param string $driver Driver identifier.
* @return ???
* @throws UnknownDriverException If driver string is not in list of available drivers.
*/
public function make($driver) {
$class = $this->className($driver);
if (is_null($class))
throw new UnknownDriverException($driver);
return new $class;
}
/**
* get the full class name for the driver
* @param string $driver String mapping of class.
* @return string
*/
public function className($driver) {
return isset($this->drivers[$driver]) ? $this->drivers[$driver] : null;
}
}
class AppleFactory extends AbstractFactory {
protected $drivers = [
// both implement AppleInterface
'fuji' => \App\Apples\Fuji::class,
'gala' => \App\Apples\Gala::class
];
}
class OrangeFactory extends AbstractFactory {
protected $drivers = [
// both implement OrangeInterface
'navel' => \App\Oranges\Navel::class,
'blood' => \App\Oranges\Blood::class
];
}
根据您上面的要求-一个标准的@方法
应该可以完成这项工作-需要在该类的PHPDoc注释中放置(AppleFactory
和OrangeFactory
)。大概是这样的:
@method AppleInterface make($driver)
同时,由于您将参数传递给工厂方法。。返回的实例与此有很强的关系——看看PhpStorm中的高级元数据支持(特定于IDE的功能)。这就是LaravelIDEHelper(例如)用来提供与该框架更好的IDE集成的东西
关于这个的更多信息:@method
也许是标记?好的,实际上就是这样<代码>@类文档中的方法AppleInterface make($driver)
。如果您将其作为答案发布,我将接受。因为您正在将$driver
作为参数传递。。。您还可以查看高级元数据支持:。如果操作正确(并且支持您的案例),您可以根据传递的参数值获得特定类型。