The question is about typehinting of the role interfaces.
The code example has two DataMappers which typehint the interface they need in the constructor,
FooDataMapper type hints the Preparable interface. Because it dependens on it and no other method from the dependency.
In the other mapper, BarDataMapper i replaced the type hint with ??? as it requires both the Preparable and the Queryable interface.
How do i handle this (what should replace ???), typehinting a single of them would not suffice, should i make a new interface (maybe one that extends the previous mentioned) and hint this new interface?
The code example is a boiled down example in PHP and the type hint (???) is in the last Class declared.
<?php
// TWO ROLE INTERFACES, SMALL SEGREGATED PART OF A DBAL
Interface Preparable {
public function prepare( .. );
}
Interface Queryable {
public function query( ... );
}
// ADAPTER IMPLEMENTS SEGREGATION
class DbalAdapter implements Preparable, Queryable {
private $dbal;
public function __construct($dbal)
{
private $this->dbal = $dbal;
}
public function prepare( .. )
{
..
return $this->dbal->prepare( .. );
}
public function query( .. )
{
..
return $this->dbal->query( .. );
}
}
// DATAMAPPER CTOR HINTS FOR METHODS IT NEEDS TO USE
class FooDataMapper implements DataMapper {
private $dbal;
public function __construct(Preparable $dbal)
{
$this->dbal = $dbal;
}
public function create(Foo $Foo)
{
..
$this->dbal->prepare( .. )
}
}
// DATAMAPPER CTOR HINTS FOR METHODS IT NEEDS TO USE
// BUT THIS TIME NONE OF THE ROLE INTERFACES MATCH, BUILD A NEW INTERFACE?
class BarDataMapper implements DataMapper {
private $dbal;
public function __construct(??? $dbal)
{
$this->dbal = $dbal;
}
public function SomeMethodThatNeedsBothPrepareNQuery(Bar $Bar)
{
..
$this->dbal->query( .. )
$this->dbal->prepare( .. )
}
}
The initial solution i come to is making a new interface extending the ones needed.
Beware the naming of classes in the code are only for shown, might as well have been Fooable, BarAble...