以下の書き方は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;
}
}
個人的な経験談では上記のような書き方は見たことがないのですが
これは設定を変えたほうがいいのでしょうか。。。