Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
29 | class PriceHydrator extends GeneratedHydrator |
||
30 | { |
||
31 | protected $priceFactory; |
||
32 | |||
33 | public function __construct( |
||
34 | HydratorInterface $hydrator, |
||
35 | PriceFactoryInterface $priceFactory |
||
36 | ) { |
||
37 | parent::__construct($hydrator); |
||
38 | $this->priceFactory = $priceFactory; |
||
39 | } |
||
40 | |||
41 | /** |
||
42 | * {@inheritdoc} |
||
43 | * @param object|Plan $object |
||
44 | */ |
||
45 | public function hydrate(array $row, $object) |
||
46 | { |
||
47 | $row['target'] = $this->hydrator->hydrate($row['target'], Target::class); |
||
48 | $row['type'] = $this->hydrator->hydrate($row['type'], Type::class); |
||
49 | if (isset($row['prepaid']['unit'])) { |
||
50 | $row['unit'] = Unit::create($row['prepaid']['unit']); |
||
51 | } |
||
52 | if (isset($row['unit']) && isset($row['prepaid']['quantity'])) { |
||
53 | $row['prepaid'] = Quantity::create($row['unit'], $row['prepaid']['quantity']); |
||
54 | } |
||
55 | if (isset($row['price']['currency'])) { |
||
56 | $row['currency'] = new Currency(strtoupper($row['price']['currency'])); |
||
57 | } |
||
58 | if (isset($row['currency']) && isset($row['price']['amount'])) { |
||
59 | $row['price'] = new Money($row['price']['amount'], $row['currency']); |
||
60 | } |
||
61 | if (isset($row['data'])) { |
||
62 | $data = Json::decode($row['data']); |
||
|
|||
63 | } |
||
64 | $row['sums'] = empty($data['sums']) ? [] : $data['sums']; |
||
65 | |||
66 | return parent::hydrate($row, $object); |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * {@inheritdoc} |
||
71 | * @param object|Plan $object |
||
72 | */ |
||
73 | View Code Duplication | public function extract($object) |
|
84 | |||
85 | /** |
||
86 | * @param string $className |
||
87 | * @throws \ReflectionException |
||
88 | * @return object |
||
89 | */ |
||
90 | public function createEmptyInstance(string $className, array $data = []) |
||
96 | } |
||
97 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: