php - Refactoring 3 functions -


i refactoring simple twitter application wrote when noticed appeared 3 (3) major code smells in form of following 3 functions:

private function get_friend_list($username, $twitter)     {         $getfield = '?screen_name=' . $username;         $url = 'https://api.twitter.com/1.1/users/show.json';                    $requestmethod = 'get';              return $twitter->setgetfield($getfield)                 ->buildoauth($url, $requestmethod)                 ->performrequest();      }      private function get_user_timeline($username, $twitter)     {                $getfield = '?screen_name=' . $username;         $url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';                    $requestmethod = 'get';                          return $twitter->setgetfield($getfield)                        ->buildoauth($url, $requestmethod)                        ->performrequest();     }      private function get_favorites_list($username, $twitter)     {                $getfield = '?screen_name=' . $username;         $url = 'https://api.twitter.com/1.1/favorites/list.json';                $requestmethod = 'get';              return $twitter->setgetfield($getfield)                        ->buildoauth($url, $requestmethod)                        ->performrequest();           } 

the 3 functions each return different information twitter user. however, break dry rule (don't repeat yourself) because each function identical except url fetch specific info twitter api.

questions:

  1. from refactoring standpoint, there wrong code?
  2. should combine 3 functions 1 (1) function?

i refactor this:

private function get_favorites_list($username, $twitter, $path)     {                  $path = sanitize($path);         $getfield = '?screen_name=' . $username;         $url = 'https://api.twitter.com/1.1/' . $path;                $requestmethod = 'get';              return $twitter->setgetfield($getfield)                        ->buildoauth($url, $requestmethod)                        ->performrequest();           } 

i recommend sanitizing $path not allow vulnerabilities creep in.


Comments

Popular posts from this blog

.htaccess - First slash is removed after domain when entering a webpage in the browser -

Automatically create pages in phpfox -

c# - Farseer ContactListener is not working -