Skip to content

Instantly share code, notes, and snippets.

@ericlbarnes
Created January 22, 2014 16:22
Show Gist options
  • Save ericlbarnes/8561773 to your computer and use it in GitHub Desktop.
Save ericlbarnes/8561773 to your computer and use it in GitHub Desktop.
Which one is better?
interface PostRepositoryInterface {
public function create($title, $content, $slug, array $meta, array $tags, $active, $user_id, Carbon $publish_date);
}
interface PostRepositoryInterface {
public function create(array $data);
}
@rtablada
Copy link

How about

interface PostRepositoryInterface {
  public function create($title, $content, $slug, array $meta, array $tags, $active, $user_id, Carbon $publish_date);

  public function createFromArray(array $data);
}

Implementation:

class DBPostRepository implements PostRepositoryInterface
{
    public function create($title, $content, $slug, array $meta, array $tags, $active, $user_id, Carbon $publish_date)
    {
        // do stuff
    }

    public function createFromArray(array $data)
    {
        $defaults = ['title' => 'Somthing', ...

        $attributes = array_merge($data, $defaults);

        return $this->create($attributes['title'], $attributes['slug'], ...
    }
}

This has the added benefit of throwing type errors when the final call to create is made. And allows for default params.

@brayniverse
Copy link

@rtablada would you need the createFromArray in the interface? Shouldn't the interface only care about the creation method?

@ericlbarnes
Copy link
Author

@brayniverse If you are coding to the interface then it would need this. Otherwise you can't guarantee createFromArray would exist.

@brayniverse
Copy link

@ericbarnes Makes sense.

@JonoB
Copy link

JonoB commented Jan 22, 2014

interface PostRepositoryInterface {
  public function create(array $data);
}

Any function taking more than 3-4 params is usually a code smell to me. At least until php gets named params.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment