I have several models using the same code and I would like to handle it better.
class Expense extends \yii\db\ActiveRecord
{
const STATUS_DELETED = 0;
const STATUS_NEW = 1;
const STATUS_PENDING = 2;
const STATUS_APPROVED = 3;
const STATUS_DECLINED = 4;
public static function StatusInText()
{
return array(
self::STATUS_DELETED => 'DELETED',
self::STATUS_NEW => 'NEW',
self::STATUS_PENDING => 'PENDING',
self::STATUS_APPROVED => 'APPROVED',
self::STATUS_DECLINED => 'DECLINED',
);
}
public function StatusToText()
{
$status = self::StatusInText();
if (array_key_exists($this->status, $status)) {
return $status[$this->status];
} else {
return '';
}
}
class Car extends \yii\db\ActiveRecord
{
const STATUS_DELETED = 0;
const STATUS_NEW = 1;
const STATUS_PENDING = 2;
const STATUS_APPROVED = 3;
const STATUS_DECLINED = 4;
public static function StatusInText()
{
return array(
self::STATUS_DELETED => 'DELETED',
self::STATUS_NEW => 'NEW',
self::STATUS_PENDING => 'PENDING',
self::STATUS_APPROVED => 'APPROVED',
self::STATUS_DECLINED => 'DECLINED',
);
}
public function StatusToText()
{
$status = self::StatusInText();
if (array_key_exists($this->status, $status)) {
return $status[$this->status];
} else {
return '';
}
}
As you see, both the model use same constant and same public function. It’s duplication. How can I improve the code ?