Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Passed
Push — add-span-to-buttons ( b5e4e4...27bfb1 )
by Pedro
28:14
created

TableSchema::columnExists()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Backpack\CRUD\app\Library\Database;
4
5
class TableSchema
6
{
7
    /** @var Doctrine\DBAL\Schema\Table|Backpack\CRUD\app\Library\Database\Table */
0 ignored issues
show
Bug introduced by
The type Backpack\CRUD\app\Librar...trine\DBAL\Schema\Table was not found. Did you mean Doctrine\DBAL\Schema\Table? If so, make sure to prefix the type with \.
Loading history...
Bug introduced by
The type Backpack\CRUD\app\Librar...\Library\Database\Table was not found. Did you mean Backpack\CRUD\app\Library\Database\Table? If so, make sure to prefix the type with \.
Loading history...
8
    public $schema;
9
10
    public function __construct(string $connection, string $table)
11
    {
12
        $this->schema = app('DatabaseSchema')->getForTable($table, $connection);
13
    }
14
15
    /**
16
     * Return an array of column names in database.
17
     *
18
     * @return array
19
     */
20
    public function getColumnsNames()
21
    {
22
        return array_values(
23
            array_map(function ($item) {
24
                return $item->getName();
25
            }, $this->getColumns())
26
        );
27
    }
28
29
    /**
30
     * Return the column type in database.
31
     *
32
     * @param  string  $columnName
33
     * @return string
34
     */
35
    public function getColumnType(string $columnName)
36
    {
37
        if (! $this->schemaExists() || ! $this->schema->hasColumn($columnName)) {
38
            return 'varchar';
39
        }
40
41
        $column = $this->schema->getColumn($columnName);
42
43
        return $column->getType()->getName();
44
    }
45
46
    /**
47
     * Check if the column exists in the database.
48
     *
49
     * @param  string  $columnName
50
     * @return bool
51
     */
52
    public function hasColumn($columnName)
53
    {
54
        if (! $this->schemaExists()) {
55
            return false;
56
        }
57
58
        return $this->schema->hasColumn($columnName);
59
    }
60
61
    /**
62
     * Check if the column is nullable in database.
63
     *
64
     * @param  string  $columnName
65
     * @return bool
66
     */
67
    public function columnIsNullable($columnName)
68
    {
69
        if (! $this->hasColumn($columnName)) {
70
            return true;
71
        }
72
73
        $column = $this->schema->getColumn($columnName);
74
75
        return $column->getNotnull() ? false : true;
76
    }
77
78
    /**
79
     * Check if the column has default value set on database.
80
     *
81
     * @param  string  $columnName
82
     * @return bool
83
     */
84
    public function columnHasDefault($columnName)
85
    {
86
        if (! $this->hasColumn($columnName)) {
87
            return false;
88
        }
89
90
        $column = $this->schema->getColumn($columnName);
91
92
        return $column->getDefault() !== null ? true : false;
93
    }
94
95
    /**
96
     * Get the default value for a column on database.
97
     *
98
     * @param  string  $columnName
99
     * @return bool
100
     */
101
    public function getColumnDefault($columnName)
102
    {
103
        if (! $this->hasColumn($columnName)) {
104
            return false;
105
        }
106
107
        $column = $this->schema->getColumn($columnName);
108
109
        return $column->getDefault();
110
    }
111
112
    /**
113
     * Get the table schema columns.
114
     *
115
     * @return array
116
     */
117
    public function getColumns()
118
    {
119
        if (! $this->schemaExists()) {
120
            return [];
121
        }
122
123
        return $this->schema->getColumns();
124
    }
125
126
    /**
127
     * Make sure the schema for the connection is initialized.
128
     *
129
     * @return bool
130
     */
131
    private function schemaExists()
132
    {
133
        if (! empty($this->schema)) {
134
            return true;
135
        }
136
137
        return false;
138
    }
139
}
140