NormalistModels::getPrimaryKey()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2
Metric Value
dl 0
loc 10
ccs 7
cts 7
cp 1
rs 9.4285
cc 2
eloc 7
nc 2
nop 1
crap 2
1
<?php
2
namespace Soluble\Normalist\Driver\Metadata;
3
4
use Soluble\Schema\Source;
5
use Soluble\Schema\Exception;
6
7
class NormalistModels extends Source\AbstractSchemaSource
8
{
9
    /**
10
     * Current class version
11
     */
12
    const VERSION = '1.0';
13
14
    /**
15
     * @var array
16
     */
17
    protected $model_definition;
18
19
    /**
20
     * @param array $model_definition
21
     */
22 17
    public function __construct(array $model_definition)
23
    {
24 17
        $this->model_definition = $model_definition;
25 17
    }
26
27
28
    /**
29
     * Get unique keys on table
30
     *
31
     * @param string $table table name
32
     * @param boolean $include_primary include primary keys in the list
33
     * @throws Exception\InvalidArgumentException
34
     * @throws Exception\ErrorException
35
     * @throws Exception\NoPrimaryKeyException
36
     * @throws Exception\ExceptionInterface
37
     * @throws Exception\TableNotFoundException
38
     * @return array
39
     */
40 1
    public function getUniqueKeys($table, $include_primary = false)
41
    {
42 1
        $this->checkTableArgument($table);
43 1
        return $this->model_definition['tables'][$table]['unique_keys'];
44
    }
45
46
47
    /**
48
     * Return indexes information on a table
49
     *
50
     * @param string $table table name
51
     * @throws Exception\InvalidArgumentException
52
     * @throws Exception\ErrorException
53
     * @throws Exception\ExceptionInterface
54
     * @throws Exception\TableNotFoundException
55
     *
56
     * @return array
57
     */
58
    public function getIndexesInformation($table)
59
    {
60
        $this->checkTableArgument($table);
61
        return $this->model_definition['tables'][$table]['indexes'];
62
    }
63
64
    /**
65
     * Return unique table primary key
66
     *
67
     * @throws Exception\InvalidArgumentException
68
     * @throws Exception\ErrorException
69
     * @throws Exception\NoPrimaryKeyException when no pk
70
     * @throws Exception\MultiplePrimaryKeyException when multiple pk found
71
     * @throws Exception\ExceptionInterface
72
     * @throws Exception\TableNotFoundException
73
     *
74
     * @param string $table
75
     * @return string|int primary key
76
     */
77 4
    public function getPrimaryKey($table)
78
    {
79 4
        $this->checkTableArgument($table);
80 3
        $pks = $this->getPrimaryKeys($table);
81 2
        if (count($pks) > 1) {
82 1
            $keys = implode(',', $pks);
83 1
            throw new Exception\MultiplePrimaryKeyException(__METHOD__ . ". Multiple primary keys found on table '$table':  $keys");
84
        }
85 1
        return $pks[0];
86
    }
87
88
89
    /**
90
     * Return composite primary keys
91
     *
92
     * @throws Exception\InvalidArgumentException
93
     * @throws Exception\ErrorException
94
     * @throws Exception\NoPrimaryKeyException
95
     * @throws Exception\ExceptionInterface
96
     * @throws Exception\TableNotFoundException
97
     *
98
     * @param string $table
99
     * @return array primary keys
100
     */
101 6
    public function getPrimaryKeys($table)
102
    {
103 6
        $this->checkTableArgument($table);
104 5
        $pks = $this->model_definition['tables'][$table]['primary_keys'];
105 5
        if (count($pks) == 0) {
106 2
            throw new Exception\NoPrimaryKeyException(__METHOD__ . ". No primary keys found on table '$table'.");
107
        }
108 3
        return $pks;
109
    }
110
111
112
    /**
113
     * Return column information
114
     *
115
     * @throws Exception\InvalidArgumentException
116
     * @throws Exception\ErrorException
117
     * @throws Exception\ExceptionInterface
118
     * @throws Exception\TableNotFoundException
119
     *
120
     * @param string $table
121
     * @return array associative array [column_name => infos]
122
     */
123 1
    public function getColumnsInformation($table)
124
    {
125 1
        $this->checkTableArgument($table);
126 1
        return $this->model_definition['tables'][$table]['columns'];
127
    }
128
129
130
    /**
131
     * {@inheritdoc}
132
     */
133 1
    public function getForeignKeys($table)
134
    {
135 1
        $this->checkTableArgument($table);
136 1
        return $this->model_definition['tables'][$table]['foreign_keys'];
137
    }
138
139
    /**
140
     * {@inheritdoc}
141
     */
142
    public function getSchemaConfig()
143
    {
144
        return $this->model_definition;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->model_definition; (array) is incompatible with the return type declared by the interface Soluble\Schema\Source\Sc...erface::getSchemaConfig of type ArrayObject.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
145
    }
146
147
    /**
148
     * {@inheritdoc}
149
     */
150
    public function getReferences($table)
151
    {
152
        $this->checkTableArgument($table);
153
        return $this->model_definition['tables'][$table]['references'];
154
    }
155
156
157
    /**
158
     * Return table informations
159
     *
160
     * @throws Exception\InvalidArgumentException
161
     * @throws Exception\ErrorException
162
     * @throws Exception\ExceptionInterface
163
     *
164
     * @return array associative array indexed by table_name
165
     */
166 1
    public function getTablesInformation()
167
    {
168 1
        return $this->model_definition['tables'];
169
    }
170
}
171