I have had this argument for a while, because I have noticed some people prefer “readable” code over properly structured one. So in the example I am showing, basically I have this Mapper
class in which different Mappable
instances are injected and the Mapper uses the instances to know how to map some data. The question here is, why is it a bad practice to do so. I suspect that the problems are: the method is static, the factory is in the wrong class, making the Mapper hard coupled to the Map
classes.
interface Mappable {
public function getMetaData();
public function setMetaData(array $metaData);
}
class BaseMap implements Mappable {
protected $metaData = [];
public function getMetaData() {
return $this->metaData;
}
public function setMetaData(array $metaData) {
$this->metaData = $metaData;
}
/**
* Possible Code Smell
*/
public static function getNewMapper() {
return new Mapper(new static);
}
}
class MapA extends BaseMap {
protected $metaData = [
'backField1' => 'frontField1',
'backField2' => 'frontField2'
];
}
class MapB extends BaseMap {
protected $metaData = [
'backFieldA' => 'frontFieldA',
'backFieldB' => 'frontFieldB'
];
}
class Mapper {
private $map;
public function __construct(Mappable $map) {
$this->setMap($map);
}
public function setMap(Mappable $map) {
$this->map = $map;
}
public function parseData($dataRow) {
$metaData = $this->map->getMetaData();
$parsedDataRow = [];
foreach($dataRow as $columnName => $columnValue) {
if(isset($metaData[$columnName]))
$parsedDataRow[$metaData[$columnName]] = $columnValue;
}
return $parsedDataRow;
}
}
// Probably correct
$mapper = new Mapper(new MapA());
print_r($mapper->parseData(['backField1' => 'foo1', 'backField2' => 'bar1']));
$mapper->setMap(new MapB());
print_r($mapper->parseData(['backFieldA' => 'fooA', 'backFieldB' => 'barB']));
// Possible Code Smell
print_r(MapA::getNewMapper()->parseData(['backField1' => 'foo1', 'backField2' => 'bar1']));
print_r(MapB::getNewMapper()->parseData(['backFieldA' => 'fooA', 'backFieldB' => 'barB']));
UML class diagram:
0
The other problem i see with your code is your inheritance is making contracts with classes it should not need to and is really breaking all oop practices. This approach allows for less code and cleaner code but also follows the convention of grouped functionality and multiple inheritance.
interface Mappable {
public function getMetaData();
public function setMetaData(array $metaData);
}
abstract class BaseMap implements Mappable {
protected $metaData = [];
public function getMetaData() {
return $this->metaData;
}
public function setMetaData(array $metaData) {
$this->metaData = $metaData;
}
}
abstract class Mapper extends BaseMap{
private $map;
public function setMap($map) {
$this->map = $map;
}
public function parseData($dataRow) {
$metaData = $this->getMetaData();
$parsedDataRow = [];
foreach($dataRow as $columnName => $columnValue) {
if(isset($metaData[$columnName]))
$parsedDataRow[$metaData[$columnName]] = $columnValue;
}
return $parsedDataRow;
}
}
class MapA extends Mapper {
protected $metaData = [
'backField1' => 'frontField1',
'backField2' => 'frontField2'
];
}
class MapB extends Mapper {
protected $metaData = [
'backFieldA' => 'frontFieldA',
'backFieldB' => 'frontFieldB'
];
}
/* i think the benifits speak for them selfs */
// less code
// less objects
// clean and readable
// easy to use
$mapA = new MapA();
$mapA->parseData(['backField1' => 'foo1', 'backField2' => 'bar1']);
$mapB = new MapB();
$mapB->parseData(['backField1' => 'foo1', 'backField2' => 'bar1']);
I am not a PHP expert, but I can read the code.
When I see this code
// Probably correct
$mapper = new Mapper(new MapA());
print_r($mapper->parseData(['backField1' => 'foo1', 'backField2' => 'bar1']));
Then I think to myself, why do I need to instantiate a Mapper
with MapA
.
I would want to have 1 class that both holds the data and knows how to map.
I would want class Mapper
to extend BaseMap
and MapA
to extend Mapper
Then, I can simply
$mapper = new MapA();
print_r($mapper->parseData(['backField1' => 'foo1', 'backField2' => 'bar1']));
Granted, MapA
might not be the best name, you could have used the name of the real mapping method.
Edit:
The counter argument for that is that this leaves the current design flexible, one could use different Mapper
classes. My counter counter argument; that feature is not intuitive because the only factory method you have only returns Mapper
.
That means that your approach is not caller friendly and not intuitive. If you want to keep the design flexible, then you should remove the static factory method. You could replace it with a comment that points out that this can be used by different mappers.
Finally, you should design with YAGNI in mind, refactoring for new requirements is okay.
5