guzzle / guzzle

Guzzle, an extensible PHP HTTP client

Home Page:https://docs.guzzlephp.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Remove private settings, make library extendable

Deele opened this issue · comments

commented

Description
Most of the classes in guzzle are written so that it is impossible to extend their functions or access their attributes, even when extending, as most of them are private.

Example

Why configuration or URI building needs to be private?

class Client implements ClientInterface, \Psr\Http\Client\ClientInterface
{
    use ClientTrait;

    /**
     * @var array Default request options
     */
    private $config;
[...]

    private function buildUri(UriInterface $uri, array $config): UriInterface
    {
        if (isset($config['base_uri'])) {
            $uri = Psr7\UriResolver::resolve(Psr7\Utils::uriFor($config['base_uri']), $uri);
        }

        if (isset($config['idn_conversion']) && ($config['idn_conversion'] !== false)) {
            $idnOptions = ($config['idn_conversion'] === true) ? \IDNA_DEFAULT : $config['idn_conversion'];
            $uri = Utils::idnUriConvert($uri, $idnOptions);
        }

        return $uri->getScheme() === '' && $uri->getHost() !== '' ? $uri->withScheme('http') : $uri;
    }
[...]

What purpose does privating these functions and variables serve?

Additional context

I encountered this issue while trying to find a way how to access guzzle "request" instance, as I wanted to know what exactly was requested - sent to the remote server, as I encountered issues with URL building and server returning weird errors, with wrong URL. Turns out, only way is to hack myself out using curl debug mode and capturing output buffer - like, why there is no 20th century way to debug connections? Well, at least I was unable to find it, so I had to make my own. I'm sorry, this was frustrating journey...

I know it can be a hard habit to break, but reaching for composition instead of extension is almost always the best way to go. We almost made the guzzle client class final in v7. The only reason we didn't was to continue to allow easy mocking since the client interface (intentionally) has fewer methods so is not always a good drop-in.