Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement interface? #42

Open
Pixelfck opened this issue Apr 27, 2016 · 2 comments
Open

Implement interface? #42

Pixelfck opened this issue Apr 27, 2016 · 2 comments
Labels
code quality enhancement New feature or request help wanted Extra attention is needed
Milestone

Comments

@Pixelfck
Copy link

It would be wonderful if you could have your cURL class implement some interface.

Right now, if I want to type-hint the type of a class method argument, I'm forced to type hint the concrete class. If the concrete class would implement an interface, I could type-hint the interface instead.
If I can type-hint an interface, I can more easily supply a mock object for unit testing purposes.

@anlutro
Copy link
Owner

anlutro commented Apr 27, 2016

I don't see a problem with implementing an interface, though there are a lot of "magic" methods which wouldn't be covered by it.

I would argue that you probably shouldn't mock the curl class, instead write a wrapper on top of it that encapsulates your business logic, add integration tests for that class and then mock your business class, but that's a topic for another day.

@Pixelfck
Copy link
Author

Pixelfck commented Apr 27, 2016

That's exactly what I do: I've a wrapper that encapsulate the business logic. To separate the concerns and improve testability, I inject the php-curl into the constructor, instead of instantiating it inside the class.

Right now, the constructor has anlutro\cURL\cURL as a type hint, which pretty much forces me to supply an instance of the concrete class anlutro\cURL\cURL. If anlutro\cURL\cURL would implement an interface, I could instead create a mock object for testing, which implements the same interface, thereby testing my class in isolation.

Current situation

<?php # WrapperClass.php
namespace some/space;

use anlutro\cURL\cURL;

class Wrapper
{
    public function __construct(string $foo, int $bar, cURL $curl)
    {
        // ...
    }
}

Proposed situation

<?php # WrapperClass.php
namespace some/space;

use anlutro\cURL\cURLInterface;

class WrapperClass
{
    public function __construct(string $foo, int $bar, cURLInterface $curl)
    {
        // ...
    }
}
<?php # cURL.php
/**
 * PHP OOP cURL
 * 
 * @author   Andreas Lutro <[email protected]>
 * @license  http://opensource.org/licenses/MIT
 * @package  PHP cURL
 */

namespace anlutro\cURL;

/**
 * ...
 */
class cURL implements cURLInterface
{
    \\ ...
}

The interface only needs to define a minimal set of methods; the following would be enough (for the purpose of loosely coupling functionality, while preserving the option to type-hint, even an empty interface would suffice):

<?php # example.php

namespace anlutro\cURL;

interface cURLInterface
{
    public function newRequest($method, $url, $data = array(), $encoding = Request::ENCODING_QUERY);

    public function sendRequest(Request $request);
}

Cheers

@anlutro anlutro added enhancement New feature or request code quality help wanted Extra attention is needed labels Apr 29, 2016
@anlutro anlutro added this to the Version 1.4 milestone Apr 29, 2016
@anlutro anlutro modified the milestones: Version 2.0, Version 1.4 May 5, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
code quality enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants