Psalm stdClass with properties definition annotation error

huangapple go评论67阅读模式
英文:

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

sandbox

答案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:

&lt;?php
/** 
 * @psalm-return object{foo?: string}
 */
function returnObject(): object {
    $item2 = new stdClass();
    $item2-&gt;foo = &quot;asd&quot;;
    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.
 */

huangapple
  • 本文由 发表于 2023年7月28日 04:11:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76783111.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定