我使用 PHP 严格类型declare(strict_types=1);并通过项目声明类型。此外,我还在 PhpStorm 中启用了 PHPDoc 检查,例如Argument PHPDoc missing、Missing @return tag等。当我有明确的返回类型和参数类型声明时,我希望 PhpStorm 不显示有关 PHPDoc 的警告。
以下是 PhpStorm 方面的有效示例代码。
/**
* @param Request $request
* @return JsonResponse
* @throws DBALException
*/
public function find(Request $request): JsonResponse
{
$user = $this->repository->find($request->get('id'));
if (!$user) {
throw new NotFoundHttpException();
}
return $this->json($user);
}
Run Code Online (Sandbox Code Playgroud)
正如我们所见,我有明确的参数类型Request $request和返回类型: JsonResponse声明,因此无需在 PHPDoc 中复制它们。
我想从 PHPDoc 中排除这一行
/**
* @param Request $request
* @return JsonResponse
*/
Run Code Online (Sandbox Code Playgroud)
没有来自 PhpStorm 的警告,但只有在我声明了类型提示的情况下。尽管如此,当我的代码没有类型提示时,我希望看到警告。
有可能实现吗?如果不是,是否存在某些 …
我基本上是PEAR(和PhpDocumentor)的新手; 我使用PEAR CLI安装了PhpDocumentor,一切似乎都没问题......直到我去使用它,此时我收到以下错误:
Warning: require(PhpDocumentor/phpDocumentor/phpdoc.inc):
failed to open stream: No such file or directory in
/usr/local/bin/phpdoc on line 40
Fatal error: require(): Failed opening required
'PhpDocumentor/phpDocumentor/phpdoc.inc' (include_path='.:/usr/share/pear')
in /usr/local/bin/phpdoc on line 40
Run Code Online (Sandbox Code Playgroud)
我在网上找不到任何关于错误的内容,所以我再次通过命令行卸载/重新安装,没有错误,但是我遇到了同样的问题.我忽略了什么吗?正如我所说,我对PEAR很新:)
谢谢.d
以下哪一项是为phpDocumentor记录此方法的返回类型的正确方法?
方法1:
/**
* @return array Foo array.
*/
public function foo() {
return array(1, 2, 3);
}
Run Code Online (Sandbox Code Playgroud)
方法2:
/**
* @return integer[] Foo array.
*/
public function foo() {
return array(1, 2, 3);
}
Run Code Online (Sandbox Code Playgroud)
此外,这两种方法都有任何IDE含义吗?
编辑:
似乎PhpStorm和Netbeans 7.1+ IDE都支持第二种方法.
说我有这样的功能:
function theFunction() {
$arr = array();
for ($i=0;$i<10;$i++) {
$arr[] = new theObject($i);
}
return $arr;
}
Run Code Online (Sandbox Code Playgroud)
我需要记录函数的返回类型.我当然可以使用array,但这并没有提供可以提供的所有信息,也没有告诉开发人员很多关于函数的真实性质.
如何在PHPDoc中记录"[type]"类型的类型?
例如,考虑以下代码:
/**
* @param array $array
* @param string $key
* @return mixed
* @throws \InvalidArgumentException
*/
private function getArrayEntry(& $array, $key)
{
if (!array_key_exists($key, $array)) {
throw new \InvalidArgumentException(
'Invalid array of values for location. Missing '.$key.'.'
);
}
return $array[$key];
}
/**
* @param array $data
* @return Location
*/
public function createFromArray(array $data)
{
$this->getArrayEntry($data, 'name');
}
Run Code Online (Sandbox Code Playgroud)
第二种方法是否也应该在doc bloc中使用@throws?
与具有'throws'关键字的Java相比,如何使用它?
我在尝试为以下代码示例正确自动完成时遇到了一些问题。我在 Win7 机器上使用 PHPStorm 7。
首先只是一个简单的类。
/**
* Class myObject
*/
class myObject
{
/**
* some method
*/
public function myMethod()
{
// do something
}
}
Run Code Online (Sandbox Code Playgroud)
这是一个集合类,它可以包含先前类的多个实例并实现 ItteratorAggregate 接口。
/**
* Class myCollection
*/
class myCollection implements IteratorAggregate
{
/**
* @var myObject[]
*/
protected $_objects = array();
/**
* @param myObject $object
* @return myCollection
*/
public function add(myObject $object)
{
$this->_objects[] = $object;
return $this;
}
/**
* @return ArrayIterator
*/
public function getIterator()
{
return …Run Code Online (Sandbox Code Playgroud) 我使用PHPStorm并试图找出如何使它停止唧唧喳喳,当__construct()有@return void在其PHPDocs ...
根据PHPDocs,void是有效的,或者@return可能省略.话虽如此,有没有办法解决这个问题还是一个错误?
我开始使用phpdocumentor2进行php项目,并希望扩展它以便它具有自定义功能.
我想在默认生成的内容下输出"最新的函数/方法/类".我的想法是什么,当它的运行能够比较解析文件中的任何元素是否有新的@version或甚至文件更改日期.
有谁知道这是否可行以及如何使用此doc生成器或其他?
也有人知道当前phpdocumentator2是否正确支持@uses,如果没有,还有什么其他的解决方法可以使它工作?我需要它来连接我的项目类.
考虑以下代码:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Car extends Model
{
public static function getTheFirstCar(string $color): ?self
{
/** @var ?self */ // <-- Doesn't apply! Is there any alternative?
return (new self())->newQuery()->firstWhere('color', '=', $color);
}
}
Run Code Online (Sandbox Code Playgroud)
代码工作正常;尽管如此 PhpStorm 抱怨:
返回值应为“
Car|null”,返回
“\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model”
将表达式的结果赋值给一个带注释的变量可以解决警告,但会引入一个“冗余”变量!
/** @var ?self $redundant */
$redundant = (new self())->newQuery()->firstWhere('color', '=', $color);
return $redundant;
Run Code Online (Sandbox Code Playgroud)
那么,在 PhpStorm 中是否有一种方法可以return明确地为语句表达式的值强制执行内联类型注释Car|null,而不引入冗余变量或指定所有预期的返回类型?
我可以在FakerPHP/Faker包中看到这个 PHPDoc 块,但我不知道这是什么@template意思?您可以在该行的包的主分支中找到它
/**
* @template T of Extension\Extension
*
* @param class-string<T> $id
*
* @throws ContainerExceptionInterface
* @throws Extension\ExtensionNotFound
*
* @return T
*/
Run Code Online (Sandbox Code Playgroud)