英文:
Psalm stdClass with properties definition anotation error
问题
/**
- @psalm-return \stdClass{foo?: string}
*/
function returnObject(): \stdClass {
$item2 = new \stdClass();
$item2->foo = "asd";
return $item2;
}
returnObject();
ERROR: InvalidDocblock - 19:1 - Unexpected brace character in docblock for returnObject
英文:
What is the correct returning object definition in psalm?
/**
* @psalm-return \stdClass{foo?: string}
*/
function returnObject(): \stdClass {
$item2 = new \stdClass();
$item2->foo = "asd";
return $item2;
}
returnObject();
ERROR: InvalidDocblock - 19:1 - Unexpected brace character in docblock for returnObject
答案1
得分: 1
您可以使用object
来完成这个操作,但您不能对任何命名类进行此操作,包括stdClass
:
<?php
/**
* @psalm-return object{foo?: string}
*/
function returnObject(): object {
$item2 = new stdClass();
$item2->foo = "asd";
return $item2;
}
$_v = returnObject();
/** @psalm-trace $_v */;
Psalm 输出 (使用提交 73ebe22):
INFO: Trace - 12:24 - $_v: object{foo?:string}
英文:
You can do this with object
, but you can't do this with any named class, including stdClass
:
<?php
/**
* @psalm-return object{foo?: string}
*/
function returnObject(): object {
$item2 = new stdClass();
$item2->foo = "asd";
return $item2;
}
$_v = returnObject();
/** @psalm-trace $_v */;
Psalm output (using commit 73ebe22):
INFO: Trace - 12:24 - $_v: object{foo?:string}
答案2
得分: 0
The syntax you are using is not supported:
/**
* @psalm-return \stdClass{foo?: string}
*/
You should only indicate the type, followed by an optional description:
/**
* @psalm-return \stdClass 返回一个具有设置了foo属性的对象。
*/
英文:
The syntax you are using is not supported:
/**
* @psalm-return \stdClass{foo?: string}
*/
You should only indicate the type, followed by an optional description:
/**
* @psalm-return \stdClass Returns an object with the foo property set.
*/
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论