我该如何添加返回此功能?

时间:2018-02-21 15:46:05

标签: php function return refactoring

我正在使用JetBrain的PhpStorm并在我继承的一些代码上运行他们的代码检查器。 PhpStorm检测到的一个问题是“返回点不一致” - 在下面的例子中,它是一个完全缺失的return语句。我想知道是否应该为这个函数添加一个return语句,如果是这样的话,我会怎么做。

 /**
 * Loads the view and outputs it
 *
 * @since  0.1.3
 *
 * @param  boolean $echo Whether to output or return the template
 *
 * @return string        Rendered template
 */
public function load( $echo = false ) {
    $content = '';

    // If we haven't done the template before (or we're forcing it)...
    if ( ! isset( self::$done[ $this->template ] ) || $this->force ) {
        // Then get the content.
        $content = parent::load( false );
    }

    // If we got content...
    if ( $content ) {

        $content = $this->format_css_tag( $content );

        // Ok, this one is done, don't load it again.
        self::$done[ $this->template ] = $this->template;
    }

    if ( ! $echo ) {
        return $content;
    }

    echo $content;
}

2 个答案:

答案 0 :(得分:1)

您可以关注print_r()作为指南,如果您回复内容,只需

return true;

说该功能已成功运行。

答案 1 :(得分:1)

我会这样做:

public function load( $echo = false ) {
  $content = '';

  // If we haven't done the template before (or we're forcing it)...
  if ( ! isset( self::$done[ $this->template ] ) || $this->force ) {
    // Then get the content.
    $content = parent::load( false );
  }

  // If we got content...
  if ( $content ) {

    $content = $this->format_css_tag( $content );

    // Ok, this one is done, don't load it again.
    self::$done[ $this->template ] = $this->template;
  } else {
    $content = false;
  }

  return $content;
}

然后在调用此函数的任何地方执行回显。

相关问题