Re: phpdoc: function return type

"J.O. Aho" <[email protected]>
Newsgroups comp.lang.php
Message-ID <[email protected]>
On 14/07/2022 18.23, Tommy wrote:
> /**
>   * @return int|exit
>   */
> function err($message, $exitCode) {
>      $int = fwrite(STDERR, $message);
> 
>      if (null !== $exitCode) {
         if(!is_null($exitCode)) {

>          exit($exitCode);
>      }
> 
>      return $int;
> }
> 
> In your opinion, int|exit is good as a type for @return tag?

No, as exit do not return anything and also it will break the execution 
here. you should write it in the description that the function will stop 
the execution if the $exitCode ain't null. Maybe you should test that 
the exitCode ain't 0, as it's normally an exit code for something that 
went well.

> Or it would be better int|null?

No, the function will return int  or false

> Or it would be better simply int?

As you are returning the return value from fwrite(), then it's int or 
false.
https://www.php.net/manual/en/function.fwrite.php


-- 

  //Aho
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.