Passed
Pull Request — master (#278)
by Sergei
02:46
created

CustomerExtraAttributes   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 9
c 1
b 0
f 0
dl 0
loc 28
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getTableName() 0 3 1
A getAttribute() 0 7 2
A getAttributes() 0 3 1
A hasAttribute() 0 3 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord;
6
7
use Yiisoft\ActiveRecord\ActiveRecord;
8
9
/**
10
 * Class CustomerClosureField.
11
 *
12
 * @property int $id
13
 * @property string $name
14
 * @property string $email
15
 * @property string $address
16
 * @property int $status
17
 * @property string $sex
18
 */
19
final class CustomerExtraAttributes extends ActiveRecord
20
{
21
    private array $extraAttributes = [
22
        'sex' => 'm',
23
    ];
24
25
    public function getTableName(): string
26
    {
27
        return 'customer';
28
    }
29
30
    public function getAttribute(string $name): mixed
31
    {
32
        if (array_key_exists($name, $this->extraAttributes)) {
33
            return $this->extraAttributes[$name];
34
        }
35
36
        return parent::getAttribute($name);
37
    }
38
39
    public function getAttributes(array $names = null, array $except = []): array
40
    {
41
        return array_merge(parent::getAttributes($names, $except), $this->extraAttributes);
42
    }
43
44
    public function hasAttribute($name): bool
45
    {
46
        return array_key_exists($name, $this->extraAttributes) || parent::hasAttribute($name);
47
    }
48
}
49