Completed
Push — master ( 3deaa5...4c5cb4 )
by Changwan
06:31
created

RepositorySettings::getColumns()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Wandu\Database\Repository;
3
4
use Doctrine\Common\Annotations\Reader;
5
use ReflectionClass;
6
use Wandu\Database\Annotations\Column;
7
use Wandu\Database\Annotations\Table;
8
9
class RepositorySettings
10
{
11 12
    public static function fromAnnotation($model, Reader $reader)
12
    {
13
        $settings = [
14 12
            'model' => $model,
15 12
        ];
16 12
        $classRefl = new ReflectionClass($model);
17 12
        $propertiesRefl = $classRefl->getProperties();
18
19 12
        class_exists(Table::class);
20 12
        class_exists(Column::class);
21
22
        /* @var \Wandu\Database\Annotations\Table $table */
23 12
        if ($table = $reader->getClassAnnotation($classRefl, Table::class)) {
24 12
            $settings['identifier'] = $table->identifier;
25 12
            $settings['increments'] = $table->increments;
26 12
        }
27
        
28 12
        $columns = [];
29 12
        $casts = [];
30 12
        foreach ($propertiesRefl as $propertyRefl) {
31
            /* @var \Wandu\Database\Annotations\Column $column */
32 12
            if ($column = $reader->getPropertyAnnotation($propertyRefl, Column::class)) {
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $column is correct as $reader->getPropertyAnno...otations\Column::class) (which targets Doctrine\Common\Annotati...getPropertyAnnotation()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
33 12
                $columns[$column->name] = $propertyRefl->name;
34 12
                $casts[$column->name] = $column->cast;
35 12
            }
36 12
        }
37 12
        if (count($columns)) {
38 12
            $settings['columns'] = $columns;
39 12
        }
40 12
        if (count($casts)) {
41 12
            $settings['casts'] = $casts;
42 12
        }
43 12
        return new RepositorySettings($table->name, $settings);
44
    }
45
    
46
    /** @var string */
47
    protected $table;
48
    
49
    /** @var string */
50
    protected $model;
51
    
52
    /** @var array */
53
    protected $columns = [];
54
    
55
    /** @var array */
56
    protected $casts = [];
57
    
58
    /** @var string */
59
    protected $identifier = 'id';
60
    
61
    /** @var bool */
62
    protected $increments = true;
63
    
64
    /**
65
     * @example
66
     * [
67
     *     'model' => RepositoryTestActor::class,
68
     *     'table' => 'actor',
69
     *     'columns' => [
70
     *         'id' => 'act_id',
71
     *         'firstName' => 'first_name',
72
     *         'lastName' => 'last_name',
73
     *         'lastUpdate' => 'last_update',
74
     *     ],
75
     *     'identifier' => 'id',
76
     *     'increments' => true,
77
     * ]
78
     * @param string $table
79
     * @param array $settings
80
     */
81 12
    public function __construct($table, array $settings = [])
82
    {
83 12
        $this->table = $table;
84 12
        foreach ($settings as $name => $setting) {
85 12
            $this->{$name} = $setting;
86 12
        }
87 12
    }
88
89
    /**
90
     * @return string
91
     */
92 7
    public function getTable()
93
    {
94 7
        return $this->table;
95
    }
96
97
    /**
98
     * @return string
99
     */
100 10
    public function getModel()
101
    {
102 10
        return $this->model;
103
    }
104
105
    /**
106
     * @return array
107
     */
108 11
    public function getColumns()
109
    {
110 11
        return $this->columns;
111
    }
112
113
    /**
114
     * @return string
115
     */
116 2
    public function getIdentifier()
117
    {
118 2
        return $this->identifier;
119
    }
120
121
    /**
122
     * @return boolean
123
     */
124 1
    public function isIncrements()
125
    {
126 1
        return $this->increments;
127
    }
128
129
    /**
130
     * @return array
131
     */
132 10
    public function getCasts()
133
    {
134 10
        return $this->casts;
135
    }
136
}
137