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 |
||
18 | class CertificatePrice extends Price |
||
19 | { |
||
20 | use ModelTrait; |
||
21 | |||
22 | const TYPE_CERT_PURCHASE = 'certificate,certificate_purchase'; |
||
23 | const TYPE_CERT_RENEWAL = 'certificate,certificate_renewal'; |
||
24 | |||
25 | /** |
||
26 | * @var DecimalMoneyFormatter |
||
27 | */ |
||
28 | private $moneyFormatter; |
||
29 | |||
30 | /** |
||
31 | * @var DecimalMoneyParser |
||
32 | */ |
||
33 | private $moneyParser; |
||
34 | |||
35 | public function __construct(array $config = []) |
||
36 | { |
||
37 | parent::__construct($config); |
||
38 | $this->moneyFormatter = new DecimalMoneyFormatter(new ISOCurrencies()); |
||
39 | $this->moneyParser = new DecimalMoneyParser(new ISOCurrencies()); |
||
40 | } |
||
41 | |||
42 | public static function tableName() |
||
43 | { |
||
44 | return 'price'; |
||
45 | } |
||
46 | |||
47 | public function behaviors() |
||
48 | { |
||
49 | return [ |
||
50 | 'typecastAfterFind' => [ |
||
51 | 'class' => AttributeTypecastBehavior::class, |
||
52 | 'attributeTypes' => [ |
||
53 | 'sums' => function ($sums) { |
||
54 | foreach ($sums as $key => $value) { |
||
55 | $sums[$key] = new Money($value, new Currency(strtoupper($this->currency))); |
||
56 | } |
||
57 | return $sums; |
||
58 | }], |
||
59 | 'typecastAfterFind' => true, |
||
60 | 'typecastAfterValidate' => false, |
||
61 | 'typecastBeforeSave' => false, |
||
62 | ], |
||
63 | 'typecastBeforeSave' => [ |
||
64 | 'class' => AttributeTypecastBehavior::class, |
||
65 | 'attributeTypes' => [ |
||
66 | 'sums' => function ($sums) { |
||
67 | foreach ($sums as $key => $value) { |
||
68 | $sums[$key] = $this->moneyParser |
||
69 | ->parse($value, strtoupper($this->currency)) |
||
70 | ->getAmount(); |
||
71 | } |
||
72 | return $sums; |
||
73 | }], |
||
74 | 'typecastAfterFind' => false, |
||
75 | 'typecastAfterValidate' => false, |
||
76 | 'typecastBeforeSave' => true, |
||
77 | ], |
||
78 | ]; |
||
79 | } |
||
80 | |||
81 | |||
82 | public function rules() |
||
83 | { |
||
84 | $rules = parent::rules(); |
||
85 | $rules['create-required'] = [ |
||
86 | ['object_id'], |
||
87 | 'required', |
||
88 | 'on' => ['create', 'update'], |
||
89 | 'when' => function ($model) { |
||
90 | /** @var self $model */ |
||
91 | return $model->isTypeCorrect(); |
||
92 | }, |
||
93 | ]; |
||
94 | $rules[] = [['certificateType'], 'safe']; |
||
95 | $rules[] = [['sums'], 'validatePrices', 'on' => ['create', 'update']]; |
||
96 | $rules[] = [['sums'], 'number', 'min' => 0, 'when' => function () { |
||
97 | return false; |
||
98 | }]; |
||
99 | $rules[] = [['sums'], 'each', 'rule' => ['number', 'min' => 0]]; |
||
100 | |||
101 | return $rules; |
||
102 | } |
||
103 | |||
104 | public function isTypeCorrect() |
||
105 | { |
||
106 | return isset($this->getTypes()[$this->type]); |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * @return array |
||
111 | */ |
||
112 | View Code Duplication | public static function getPeriods() |
|
119 | |||
120 | /** |
||
121 | * @return array |
||
122 | */ |
||
123 | View Code Duplication | public function getAvailablePeriods() |
|
134 | |||
135 | /** |
||
136 | * @param int $period |
||
137 | * @return null|string |
||
138 | */ |
||
139 | View Code Duplication | public function getPriceForPeriod(int $period) |
|
147 | |||
148 | /** |
||
149 | * @param int $period |
||
150 | * @return bool |
||
151 | */ |
||
152 | public function hasPriceForPeriod(int $period) |
||
156 | |||
157 | public function validatePrices(): bool |
||
171 | |||
172 | /** |
||
173 | * @return array |
||
174 | */ |
||
175 | public static function getTypes() |
||
182 | } |
||
183 |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: