LoginSignup
0
0

More than 5 years have passed since last update.

Missing return statement less|phpstormでのwarning

Posted at

以下の書き方はwarningをとりあえず解消したというメモ。
今後書き方を変えるか、設定を変えるかプロジェクトなどを基準に考えます。

warning

Missing return statement less... (Ctrl+F1) 

The inspection is intended to point at the following types of inconsistencies:
The function/method contains return statements with arguments as well as without them
The function/method may happen to return a value or end it's execution without a return statement at all
The function/method is declared void but contains return statements with arguments
Technically these are not errors but practically usually indicate programmer’s mistakes.

ソース

php
function get_user_name() {
    $current_user = wp_get_current_user();

    if ( isset( $current_user->user_login ) ) {
        $user_name = $current_user->user_login;

        return $user_name;
    } 
}

原因

if文のelseの動作でreturnがnullを返すと明示的に指定しないといけないというエラーらしい。
「文法的には間違いではないのでこのエラーチェックが不要だったら設定してね」という
気の利いたコメントも出力されておりました。

修正ソース

以下のように修正したらwarning消えました。

php
function get_user_name() {
    $current_user = wp_get_current_user();

    if ( isset( $current_user->user_login ) ) {
        $user_name = $current_user->user_login;

        return $user_name;
    } else {
        return null;
    }
}

個人的な経験談では上記のような書き方は見たことがないのですが
これは設定を変えたほうがいいのでしょうか。。。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0