php - 快速重构

发布于 2024-10-26 19:23:23 字数 1474 浏览 0 评论 0原文

我有两种方法,几乎​​完全相同,我希望有人帮助我重构它们:

public static function searchFromVideoRequest($word, $returnPropelObjects = false)
{
    $c = new Criteria();
    $c->addJoin(YoutubeVideoPeer::ID,ItemPeer::YOUTUBE_VIDEO_ID);
    $c->addSelectColumn(self::TITLE);
    $c->addSelectColumn(self::ID);
    $c->add(ItemPeer::TITLE, '%'.$word.'%', Criteria::LIKE);
    $c->addAnd(self::YOUTUBE_VIDEO_ID, null, Criteria::ISNOTNULL);
$c->addAscendingOrderByColumn(self::TITLE);
   if ($returnPropelObjects)
          return self::doSelect($c);

        $stmt = self::doSelectStmt($c);
        $results = array();
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
          $results[] = $row;
        }
        return $results;
}

public static function searchFromFlickrRequest($word, $returnPropelObjects = false)
{
    $c = new Criteria();
    $c->addJoin(FlickrPhotoPeer::ID,ItemPeer::FLICKR_PHOTO_ID);
    $c->addSelectColumn(self::TITLE);
    $c->addSelectColumn(self::ID);
    $c->add(ItemPeer::TITLE, '%'.$word.'%', Criteria::LIKE);
    $c->addAnd(self::FLICKR_PHOTO_ID, null, Criteria::ISNOTNULL);
    $c->addAscendingOrderByColumn(self::TITLE);
   if ($returnPropelObjects)
          return self::doSelect($c);

        $stmt = self::doSelectStmt($c);
        $results = array();
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
          $results[] = $row;
        }
        return $results;
}

谢谢

I have 2 methods, that are pretty much exactly the same and I'd likie someone to help me refactor them:

public static function searchFromVideoRequest($word, $returnPropelObjects = false)
{
    $c = new Criteria();
    $c->addJoin(YoutubeVideoPeer::ID,ItemPeer::YOUTUBE_VIDEO_ID);
    $c->addSelectColumn(self::TITLE);
    $c->addSelectColumn(self::ID);
    $c->add(ItemPeer::TITLE, '%'.$word.'%', Criteria::LIKE);
    $c->addAnd(self::YOUTUBE_VIDEO_ID, null, Criteria::ISNOTNULL);
$c->addAscendingOrderByColumn(self::TITLE);
   if ($returnPropelObjects)
          return self::doSelect($c);

        $stmt = self::doSelectStmt($c);
        $results = array();
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
          $results[] = $row;
        }
        return $results;
}

public static function searchFromFlickrRequest($word, $returnPropelObjects = false)
{
    $c = new Criteria();
    $c->addJoin(FlickrPhotoPeer::ID,ItemPeer::FLICKR_PHOTO_ID);
    $c->addSelectColumn(self::TITLE);
    $c->addSelectColumn(self::ID);
    $c->add(ItemPeer::TITLE, '%'.$word.'%', Criteria::LIKE);
    $c->addAnd(self::FLICKR_PHOTO_ID, null, Criteria::ISNOTNULL);
    $c->addAscendingOrderByColumn(self::TITLE);
   if ($returnPropelObjects)
          return self::doSelect($c);

        $stmt = self::doSelectStmt($c);
        $results = array();
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
          $results[] = $row;
        }
        return $results;
}

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若言繁花未落 2024-11-02 19:23:23

要重构这些方法,您可以将它们分成几个包含公共代码并将它们设为私有的方法,这样就没有人可以在类之外使用它们:

public static function searchFromVideoRequest($word, $returnPropelObjects = false)
{
    $c = self::buildSearchCriteria($word);
    $c->addJoin(YoutubeVideoPeer::ID,ItemPeer::YOUTUBE_VIDEO_ID);
    $c->addAnd(self::YOUTUBE_VIDEO_ID, null, Criteria::ISNOTNULL);
    return self::getSearchResult($c, $returnPropelObjects);
}

public static function searchFromFlickrRequest($word, $returnPropelObjects = false)
{
    $c = self::buildSearchCriteria($word);
    $c->addJoin(FlickrPhotoPeer::ID,ItemPeer::FLICKR_PHOTO_ID);
    $c->addAnd(self::FLICKR_PHOTO_ID, null, Criteria::ISNOTNULL);
    return self::getSearchResult($c, $returnPropelObjects);
}

private static function buildSearchCriteria($word)
{
    $c = new Criteria();
    $c->addSelectColumn(self::TITLE);
    $c->addSelectColumn(self::ID);
    $c->add(ItemPeer::TITLE, '%'.$word.'%', Criteria::LIKE);
    $c->addAscendingOrderByColumn(self::TITLE);
    return $c;
}

private static function getSearchResult($c, $returnPropelObjects)
{
    if ($returnPropelObjects)
      return self::doSelect($c);

    $stmt = self::doSelectStmt($c);
    $results = array();
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
      $results[] = $row;
    }
    return $results;
}

PS:我认为这个问题是可以的。

To refactor such methods you can split up them into a few methods that will contain common code and make them private, so no one can use them outside the class:

public static function searchFromVideoRequest($word, $returnPropelObjects = false)
{
    $c = self::buildSearchCriteria($word);
    $c->addJoin(YoutubeVideoPeer::ID,ItemPeer::YOUTUBE_VIDEO_ID);
    $c->addAnd(self::YOUTUBE_VIDEO_ID, null, Criteria::ISNOTNULL);
    return self::getSearchResult($c, $returnPropelObjects);
}

public static function searchFromFlickrRequest($word, $returnPropelObjects = false)
{
    $c = self::buildSearchCriteria($word);
    $c->addJoin(FlickrPhotoPeer::ID,ItemPeer::FLICKR_PHOTO_ID);
    $c->addAnd(self::FLICKR_PHOTO_ID, null, Criteria::ISNOTNULL);
    return self::getSearchResult($c, $returnPropelObjects);
}

private static function buildSearchCriteria($word)
{
    $c = new Criteria();
    $c->addSelectColumn(self::TITLE);
    $c->addSelectColumn(self::ID);
    $c->add(ItemPeer::TITLE, '%'.$word.'%', Criteria::LIKE);
    $c->addAscendingOrderByColumn(self::TITLE);
    return $c;
}

private static function getSearchResult($c, $returnPropelObjects)
{
    if ($returnPropelObjects)
      return self::doSelect($c);

    $stmt = self::doSelectStmt($c);
    $results = array();
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
      $results[] = $row;
    }
    return $results;
}

PS: And I think the question is OK.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文