Table   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 161
Duplicated Lines 0 %

Test Coverage

Coverage 95.65%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 41
c 1
b 0
f 1
dl 0
loc 161
ccs 44
cts 46
cp 0.9565
rs 10
wmc 20

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getAlias() 0 3 1
A optionalColumn() 0 3 1
A requiredColumn() 0 3 1
A __get() 0 5 2
A getSchema() 0 3 1
A __toString() 0 5 2
A getNode() 0 11 2
A getName() 0 3 1
B listColumns() 0 33 7
A autoColumn() 0 3 1
A __construct() 0 7 1
1
<?php
2
3
namespace mindplay\sql\model\schema;
4
5
use mindplay\sql\model\Driver;
6
use mindplay\sql\model\TypeProvider;
7
use ReflectionClass;
8
use ReflectionMethod;
9
10
/**
11
 * This is an abstract base-class for user-defined Table-types belonging to a Schema.
12
 */
13
abstract class Table
14
{
15
    private Schema $schema;
16
    private Driver $driver;
17
    private TypeProvider $types;
18
    private string $name;
19
    private string|null $alias;
20
21 1
    public function __construct(Schema $schema, Driver $driver, TypeProvider $types, string $name, string|null $alias)
22
    {
23 1
        $this->schema = $schema;
24 1
        $this->driver = $driver;
25 1
        $this->types = $types;
26 1
        $this->name = $name;
27 1
        $this->alias = $alias;
28
    }
29
30
    /**
31
     * @return Schema owner Schema instance
32
     */
33 1
    public function getSchema(): Schema
34
    {
35 1
        return $this->schema;
36
    }
37
38 1
    public function getName(): string
39
    {
40 1
        return $this->name;
41
    }
42
43 1
    public function getAlias(): string|null
44
    {
45 1
        return $this->alias;
46
    }
47
48
    /**
49
     * @return string table expression (e.g. "{table} AS {alias}" for use in the FROM clause of an SQL statement)
50
     */
51 1
    public function getNode(): string
52
    {
53 1
        $alias = $this->getAlias();
54
55 1
        $quoted_table_name = $this->driver->quoteTableName($this->schema->getName(), $this->getName());
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->schema->getName() targeting mindplay\sql\model\schema\Schema::getName() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

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

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

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

Loading history...
56
57 1
        if ($alias) {
58 1
            return $quoted_table_name . ' AS ' . $this->driver->quoteName($alias);
59
        }
60
61 1
        return $quoted_table_name;
62
    }
63
64
    /**
65
     * @param string|null $prefix optional Column Alias prefix
66
     *
67
     * @return Column[] list of all available Columns
68
     */
69 1
    public function listColumns(string|null $prefix = null): array
70
    {
71
        // create a whitelist of parent types, excluding the Table class itself:
72
73 1
        $type = get_class($this);
74
75 1
        $whitelist = [];
76
77 1
        while ($type && $type !== self::class) {
78 1
            $whitelist[$type] = true;
79
80 1
            $type = get_parent_class($type);
81
        }
82
83
        // reflect all available public methods:
84
85 1
        $reflection = new ReflectionClass($this);
86
87 1
        $methods = $reflection->getMethods(ReflectionMethod::IS_PUBLIC);
88
89 1
        $columns = [];
90
91 1
        foreach ($methods as $method) {
92 1
            if (isset($whitelist[$method->class]) && !$method->isStatic()) {
93 1
                $alias = $prefix
94 1
                    ? "{$prefix}_{$method->name}"
95 1
                    : null;
96
97 1
                $columns[] =  $method->invoke($this, $alias);
98
            }
99
        }
100
101 1
        return $columns;
102
    }
103
104
    /**
105
     * Creates a required Column.
106
     *
107
     * A value *must* be specified when building an `INSERT` query - if you don't specify a value
108
     * for this Column, the INSERT query-builder will throw an exception.
109
     */
110 1
    protected function requiredColumn(string $name, string $type, string|null $alias = null): Column
111
    {
112 1
        return new Column($this->driver, $this, $name, $this->types->getType($type), $alias, true, null, false);
113
    }
114
115
    /**
116
     * Creates an optional Column.
117
     *
118
     * A value is optional (and may have a `$default`) when building an `INSERT` query - if you don't
119
     * specify a value for this Column, the INSERT query-builder will automatically assign the `$default`.
120
     *
121
     * @param $name    Column name
122
     * @param $type    Type class-name
123
     * @param $alias   Optional alias
0 ignored issues
show
Bug introduced by
The type mindplay\sql\model\schema\Optional was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
124
     * @param $default Optional default PHP value (Type-conversion will be applied.)
125
     *
126
     * @return Column
127
     */
128
    protected function optionalColumn(string $name, string $type, string|null $alias = null, mixed $default = null): Column
129
    {
130
        return new Column($this->driver, $this, $name, $this->types->getType($type), $alias, false, $default, false);
131
    }
132
133
    /**
134
     * Creates an auto-defined Column.
135
     *
136
     * A value should *not* by specified when building an `INSERT` query.
137
     *
138
     * Use this for Columns that the database itself will populate, e.g. auto-incrementing keys or
139
     * columns that are otherwise initialized by the database itself.
140
     *
141
     * @param $name  Column name
142
     * @param $type  Type class-name
143
     * @param $alias Optional alias
144
     */
145 1
    protected function autoColumn(string $name, string $type, string|null $alias = null): Column
146
    {
147 1
        return new Column($this->driver, $this, $name, $this->types->getType($type), $alias, false, null, true);
148
    }
149
150
    /**
151
     * @ignore
152
     *
153
     * @return string
154
     */
155 1
    public function __toString(): string
156
    {
157 1
        return $this->alias
158 1
            ? $this->driver->quoteName($this->alias)
159 1
            : $this->driver->quoteTableName($this->schema->getName(), $this->name);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->schema->getName() targeting mindplay\sql\model\schema\Schema::getName() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

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

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

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

Loading history...
160
    }
161
162
    /**
163
     * @ignore
164
     *
165
     * @param string $name
166
     *
167
     * @return Column
168
     */
169 1
    public function __get($name)
170
    {
171
        // TODO caching
172
173 1
        return $this->$name($this->alias ? "{$this->alias}_{$name}" : null);
174
    }
175
}
176