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 |
||
27 | class CertificateResource extends Resource |
||
28 | { |
||
29 | use ModelTrait; |
||
30 | |||
31 | public $certificateType; |
||
32 | |||
33 | public static function tableName() |
||
37 | |||
38 | const TYPE_CERT_PURCHASE = 'certificate_purchase'; |
||
39 | const TYPE_CERT_RENEWAL = 'certificate_renewal'; |
||
40 | |||
41 | public function rules() |
||
42 | { |
||
43 | $rules = parent::rules(); |
||
44 | $rules['create-required'] = [ |
||
45 | ['object_id'], |
||
46 | 'required', |
||
47 | 'on' => ['create', 'update'], |
||
48 | 'when' => function ($model) { |
||
49 | /** @var self $model */ |
||
50 | return $model->isTypeCorrect(); |
||
51 | }, |
||
52 | ]; |
||
53 | $rules[] = [['certificateType'], 'safe']; |
||
54 | $rules[] = [['data'], 'validatePrices', 'on' => ['create', 'update']]; |
||
55 | |||
56 | return $rules; |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * @return array |
||
61 | */ |
||
62 | public static function getPeriods() |
||
69 | |||
70 | /** |
||
71 | * @return array |
||
72 | */ |
||
73 | public function getAvailablePeriods() |
||
84 | |||
85 | public function getPriceForPeriod($period) |
||
95 | |||
96 | /** |
||
97 | * @param int $period |
||
98 | * @return string|null |
||
99 | */ |
||
100 | public function getMoneyForPeriod(int $period) |
||
107 | |||
108 | public function validatePrices() |
||
124 | |||
125 | /** |
||
126 | * @param int $period |
||
127 | * @return bool |
||
128 | */ |
||
129 | public function hasPriceForPeriod(int $period) |
||
133 | |||
134 | /** |
||
135 | * @return array |
||
136 | */ |
||
137 | View Code Duplication | public function getTypes() |
|
144 | } |
||
145 |
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.