Psalm stdClass with properties definition annotation error

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

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?

  1. /**
  2. * @psalm-return \stdClass{foo?: string}
  3. */
  4. function returnObject(): \stdClass {
  5. $item2 = new \stdClass();
  6. $item2->foo = "asd";
  7. return $item2;
  8. }
  9. returnObject();
  10. ERROR: InvalidDocblock - 19:1 - Unexpected brace character in docblock for returnObject

sandbox

答案1

得分: 1

您可以使用object来完成这个操作,但您不能对任何命名类进行此操作,包括stdClass

  1. <?php
  2. /**
  3. * @psalm-return object{foo?: string}
  4. */
  5. function returnObject(): object {
  6. $item2 = new stdClass();
  7. $item2->foo = "asd";
  8. return $item2;
  9. }
  10. $_v = returnObject();
  11. /** @psalm-trace $_v */;
  1. Psalm 输出 (使用提交 73ebe22):
  2. 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:

  1. &lt;?php
  2. /**
  3. * @psalm-return object{foo?: string}
  4. */
  5. function returnObject(): object {
  6. $item2 = new stdClass();
  7. $item2-&gt;foo = &quot;asd&quot;;
  8. return $item2;
  9. }
  10. $_v = returnObject();
  11. /** @psalm-trace $_v */;
  1. Psalm output (using commit 73ebe22):
  2. INFO: Trace - 12:24 - $_v: object{foo?:string}

答案2

得分: 0

The syntax you are using is not supported:

  1. /**
  2. * @psalm-return \stdClass{foo?: string}
  3. */

You should only indicate the type, followed by an optional description:

  1. /**
  2. * @psalm-return \stdClass 返回一个具有设置了foo属性的对象。
  3. */
英文:

The syntax you are using is not supported:

  1. /**
  2. * @psalm-return \stdClass{foo?: string}
  3. */

You should only indicate the type, followed by an optional description:

  1. /**
  2. * @psalm-return \stdClass Returns an object with the foo property set.
  3. */

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:

确定