deployer icon indicating copy to clipboard operation
deployer copied to clipboard

[httpie] Delete unnecessary clone

Open joanhey opened this issue 3 years ago • 8 comments
trafficstars

  • [ ] Bug fix #…?

  • [ ] New feature?

  • [ ] BC breaks?

  • [ ] Tests added?

  • [ ] Docs added?

    Please, regenerate docs by running next command:
    $ php bin/docgen
    

joanhey avatar Apr 09 '22 17:04 joanhey

This is a big breaking change. Why?

antonmedv avatar Apr 09 '22 17:04 antonmedv

This change don't break anything. All works the same.

It is NOT necessary to clone this object. We change directly the attributes in the received instance.

joanhey avatar Apr 09 '22 17:04 joanhey

When I have time I'll add tests to this class. And it will be easier to make changes.

joanhey avatar Apr 09 '22 17:04 joanhey

Clone is essential part. One can create a "boilerplate" and copy it via the method. Probably somebody relays on it already.

antonmedv avatar Apr 09 '22 17:04 antonmedv

But this is the contrary. Clone it when you need it. And not httpie.

return Httpie::get($url)->header(....)->send() Here we don't need it.

$response1 = Httpie::get($url)->header(....); Here is the same if it's cloned or not;

$result  = $response1->jsonBody(...)->send();
$result2 = $response1->formBody(...)->send();

Also here we don't need it cloned.

$response2 = $response1->jsonBody(...); Here response2 is a copy of $reponse1, but like you said we changed $reponse1 too.

And the logical is that the developer use the clone if they want it. Not automatically Httpie.

$response2 = clone $response1;
$response2->jsonBody(....);

It's more logical to the developer clone it. $response2 = $response1->jsonBody(...); This look like you changed $response1 too.

joanhey avatar Apr 09 '22 17:04 joanhey

But you are who will decide every PR we send you.

joanhey avatar Apr 09 '22 17:04 joanhey

Also it's possible to add a clone() method. And that will be semantically correct. But unnecessary too.

joanhey avatar Apr 10 '22 09:04 joanhey

A good example is the fetch() function It's working without clone, but we can simplify the code, and use less instances.

Now:

$http = $http->nothrow($nothrow);
foreach ($headers as $key => $value) {
        $http = $http->header($key, $value);
}
if ($body !== null) {
        $http = $http->body($body);
}
return $http->send($info);

After:

$http->nothrow($nothrow);
foreach ($headers as $key => $value) {
        $http->header($key, $value);
}
if ($body !== null) {
        $http->body($body);
}
return $http->send($info);

Only 1 instance used.

And we can clone at any moment, if we need it.

$http2 = clone $http;
// or
$http2 = clone $http->body($body);

joanhey avatar Apr 11 '22 18:04 joanhey