Code repetition

Asked

Viewed 112 times

0

I’m having trouble with code repetition in my CURL connection class, the class is being declared as follows:

<?php
if (!defined("ROOT_PATH")) define("ROOT_PATH", dirname(__FILE__, 2));

require_once ROOT_PATH."/helpers/Helper.php";

class PhabricatorModel
{
    const TOKEN = "api.token=api-...";

    public function add($parameters)
    {
        $channel = curl_init();
        curl_setopt($channel, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded"));
        curl_setopt($channel, CURLOPT_URL, "https://pb-dc.com/api/maniphest.edit");
        curl_setopt($channel, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($channel, CURLOPT_POSTFIELDS, self::TOKEN.$parameters);
        curl_setopt($channel, CURLOPT_POST, 1);
        $result = curl_exec($channel);
        if (curl_errno($channel)) {
            return false;
        }

        curl_close($channel);

        return $result;
    }

    public function consultProject($project)
    {
        $channel = curl_init();
        curl_setopt($channel, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded"));
        curl_setopt($channel, CURLOPT_URL, "https://pb-dc.com/api/project.query");
        curl_setopt($channel, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($channel, CURLOPT_POSTFIELDS, self::TOKEN."names[]=".$project);
        curl_setopt($channel, CURLOPT_POST, 1);
        $result = Helper::convert_array(json_decode(curl_exec($channel)));
        if (curl_errno($channel)) {
            return false;
        }

        curl_close($channel);

        return $result;
    }

    public function consultStory($story)
    {
        $channel = curl_init();
        curl_setopt($channel, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded"));
        curl_setopt($channel, CURLOPT_URL, "https://pb-dc.com/api/maniphest.query");
        curl_setopt($channel, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($channel, CURLOPT_POSTFIELDS, self::TOKEN."status=status-open&fullText=".$story);
        curl_setopt($channel, CURLOPT_POST, 1);
        $result = Helper::convert_array(json_decode(curl_exec($channel)));
        if (curl_errno($channel)) {
            return false;
        }

        curl_close($channel);

        return $result;
    }
}

If you notice the same item from curl_init in the 3 methods and I wanted to remove this repetition, not only in the curl_init but also in other parts.

  • most cases Curl does not result in anything if you do not use CURLOPT_SSL_VERIFYPEER, false. I have basic knowledge. maybe that’s it

  • @William did not understand very well what you wanted to pass me, could explain me more ?

  • Your three methods are very similar in structure, you could make a unique method and use the switch/case structure to treat parts that are different.

  • @Giovanninunes the idea is that over time this code is evolved, and for reading and understanding a switch/ case would not be cool for this.

  • 1

    Create a new method for startup, like this $channel will turn $this->channel and put there CURLOPT_HTTPHEADER, CURLOPT_RETURNTRANSFER and CURLOPT_POST which are equal in the three cases (but you can still change them within the method whenever you need). I’m going to put an example of this here in the answers.

2 answers

1


Treat the variable $channel as a class attribute and create a specific private method to initialize it with the values that are already repeated inside, something like this:

class PhabricatorModel
{
        const TOKEN = "api.token=api-...";

        // o que é comum fica aqui...
        private function curl_init(){
            $this->channel = curl_init();
            curl_setopt($this->channel, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded"));
            curl_setopt($this->channel, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($this->channel, CURLOPT_POST, 1);
        }

        // o que é específico fica num método próprio...
        public function add($parameters) {
            $this->curl_init();
            curl_setopt($this->channel, CURLOPT_URL, "https://pb-dc.com/api/maniphest.edit");
            curl_setopt($this->channel, CURLOPT_POSTFIELDS, self::TOKEN.$parameters);
            if (curl_errno($this->channel)){
                $result = false;
            } else {
                $result = curl_exec($this->channel);
            }
            curl_close($self->channel);
            return $result;
        }
        // alterei a lógica no final para evitar surpresas.

        // ...

And the same thing would be done in the other methods.

0

Method extraction

Try creating a new method that contains the repeating part and pass as parameter the values that vary in Curl library calls.

     private function configCurl($url, $token)
     {

        $channel = curl_init();
        curl_setopt($channel, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded"));
        curl_setopt($channel, CURLOPT_URL, $url);
        curl_setopt($channel, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($channel, CURLOPT_POSTFIELDS, $token);
        curl_setopt($channel, CURLOPT_POST, 1);

        return $channel;

     }

Then just use the method in place of the repeated code and work with the Curl reference (now returned by the method), just as you are doing.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.