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

DatabaseSchema   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 146
Duplicated Lines 0 %

Importance

Changes 4
Bugs 3 Features 0
Metric Value
eloc 65
c 4
b 3
f 0
dl 0
loc 146
rs 10
wmc 26

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getManager() 0 5 2
A generateDatabaseSchema() 0 4 2
A listTableColumnsNames() 0 5 2
A getTables() 0 17 4
A listTableIndexes() 0 3 1
A getForTable() 0 7 2
A mapTable() 0 31 5
A getCreateSchema() 0 5 2
A dbalTypes() 0 13 1
A getSchemaManager() 0 13 3
A getIndexColumnNames() 0 11 2
1
<?php
2
3
namespace Backpack\CRUD\app\Library\Database;
4
5
use Illuminate\Support\Arr;
6
use Illuminate\Support\Facades\DB;
7
use Illuminate\Support\LazyCollection;
8
9
final class DatabaseSchema
10
{
11
    private static $schema;
12
13
    /**
14
     * Return the schema for the table.
15
     */
16
    public static function getForTable(string $table, string $connection)
17
    {
18
        $connection = $connection ?: config('database.default');
19
20
        self::generateDatabaseSchema($connection, $table);
21
22
        return self::$schema[$connection][$table] ?? null;
23
    }
24
25
    public static function getTables(string $connection = null): array
26
    {
27
        $connection = $connection ?: config('database.default');
28
29
        self::$schema[$connection] = LazyCollection::make(self::getCreateSchema($connection)->getTables())->mapWithKeys(function ($table, $key) use ($connection) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

29
        self::$schema[$connection] = LazyCollection::make(self::getCreateSchema($connection)->getTables())->mapWithKeys(function ($table, /** @scrutinizer ignore-unused */ $key) use ($connection) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Bug introduced by
self::getCreateSchema($connection)->getTables() of type Doctrine\DBAL\Schema\Table[]|array is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $items of Illuminate\Support\LazyCollection::make(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

29
        self::$schema[$connection] = LazyCollection::make(/** @scrutinizer ignore-type */ self::getCreateSchema($connection)->getTables())->mapWithKeys(function ($table, $key) use ($connection) {
Loading history...
Bug introduced by
The method getTables() does not exist on Doctrine\DBAL\Schema\AbstractSchemaManager. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

29
        self::$schema[$connection] = LazyCollection::make(self::getCreateSchema($connection)->/** @scrutinizer ignore-call */ getTables())->mapWithKeys(function ($table, $key) use ($connection) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
30
            $tableName = is_array($table) ? $table['name'] : $table->getName();
31
32
            if ($existingTable = self::$schema[$connection][$tableName] ?? false) {
33
                return [$tableName => $existingTable];
34
            }
35
36
            $table = self::mapTable($connection, $tableName);
37
38
            return [$tableName => $table];
39
        })->toArray();
40
41
        return self::$schema[$connection];
42
    }
43
44
    public function listTableColumnsNames(string $connection, string $table)
45
    {
46
        $table = self::getForTable($table, $connection);
47
48
        return $table ? array_keys($table->getColumns()) : [];
49
    }
50
51
    public function listTableIndexes(string $connection, string $table)
52
    {
53
        return self::getIndexColumnNames($connection, $table);
54
    }
55
56
    public function getManager(string $connection = null)
57
    {
58
        $connection = $connection ?: config('database.default');
59
60
        return self::getSchemaManager($connection);
61
    }
62
63
    /**
64
     * Generates and store the database schema.
65
     */
66
    private static function generateDatabaseSchema(string $connection, string $table)
67
    {
68
        if (! isset(self::$schema[$connection][$table])) {
69
            self::$schema[$connection][$table] = self::mapTable($connection, $table);
70
        }
71
    }
72
73
    private static function mapTable(string $connection, string $tableName)
74
    {
75
        try {
76
            $table = method_exists(self::getCreateSchema($connection), 'getTable') ?
77
                        self::getCreateSchema($connection)->getTable($tableName) :
0 ignored issues
show
Bug introduced by
The method getTable() does not exist on Doctrine\DBAL\Schema\AbstractSchemaManager. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

77
                        self::getCreateSchema($connection)->/** @scrutinizer ignore-call */ getTable($tableName) :

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
78
                        self::getCreateSchema($connection)->getColumns($tableName);
0 ignored issues
show
Bug introduced by
The method getColumns() does not exist on Doctrine\DBAL\Schema\AbstractSchemaManager. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

78
                        self::getCreateSchema($connection)->/** @scrutinizer ignore-call */ getColumns($tableName);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method getColumns() does not exist on Doctrine\DBAL\Schema\Schema. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

78
                        self::getCreateSchema($connection)->/** @scrutinizer ignore-call */ getColumns($tableName);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
79
        } catch (\Exception $e) {
80
            return new Table($tableName, []);
81
        }
82
83
        if (! is_array($table)) {
84
            return $table;
85
        }
86
87
        if (empty($table)) {
88
            return new Table($tableName, []);
89
        }
90
91
        $schemaManager = self::getSchemaManager($connection);
92
        $indexes = $schemaManager->getIndexes($tableName);
0 ignored issues
show
Bug introduced by
The method getIndexes() does not exist on Doctrine\DBAL\Schema\AbstractSchemaManager. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

92
        /** @scrutinizer ignore-call */ 
93
        $indexes = $schemaManager->getIndexes($tableName);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
93
94
        $indexes = array_map(function ($index) {
95
            return $index['columns'];
96
        }, $indexes);
97
98
        $table = new Table($tableName, $table);
99
100
        $indexes = Arr::flatten($indexes);
101
        $table->setIndexes(array_unique($indexes));
102
103
        return $table;
104
    }
105
106
    private static function getIndexColumnNames(string $connection, string $table)
107
    {
108
        self::generateDatabaseSchema($connection, $table);
109
110
        $indexes = self::$schema[$connection][$table]->getIndexes();
111
112
        $indexes = \Illuminate\Support\Arr::flatten(array_map(function ($index) {
113
            return is_string($index) ? $index : $index->getColumns();
114
        }, $indexes));
115
116
        return array_unique($indexes);
117
    }
118
119
    private static function getCreateSchema(string $connection)
120
    {
121
        $schemaManager = self::getSchemaManager($connection);
122
123
        return method_exists($schemaManager, 'createSchema') ? $schemaManager->createSchema() : $schemaManager;
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Schema\Abs...Manager::createSchema() has been deprecated: Use {@link introspectSchema()} instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

123
        return method_exists($schemaManager, 'createSchema') ? /** @scrutinizer ignore-deprecated */ $schemaManager->createSchema() : $schemaManager;

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
124
    }
125
126
    private static function dbalTypes()
127
    {
128
        return [
129
            'enum' => \Doctrine\DBAL\Types\Types::STRING,
130
            'jsonb' => \Doctrine\DBAL\Types\Types::JSON,
131
            'geometry' => \Doctrine\DBAL\Types\Types::STRING,
132
            'point' => \Doctrine\DBAL\Types\Types::STRING,
133
            'lineString' => \Doctrine\DBAL\Types\Types::STRING,
134
            'polygon' => \Doctrine\DBAL\Types\Types::STRING,
135
            'multiPoint' => \Doctrine\DBAL\Types\Types::STRING,
136
            'multiLineString' => \Doctrine\DBAL\Types\Types::STRING,
137
            'multiPolygon' => \Doctrine\DBAL\Types\Types::STRING,
138
            'geometryCollection' => \Doctrine\DBAL\Types\Types::STRING,
139
        ];
140
    }
141
142
    private static function getSchemaManager(string $connection)
143
    {
144
        $connection = DB::connection($connection);
145
146
        if (method_exists($connection, 'getDoctrineSchemaManager')) {
147
            foreach (self::dbalTypes() as $key => $value) {
148
                $connection->getDoctrineSchemaManager()->getDatabasePlatform()->registerDoctrineTypeMapping($key, $value);
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Schema\Abs...::getDatabasePlatform() has been deprecated: Use {@link Connection::getDatabasePlatform()} instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

148
                /** @scrutinizer ignore-deprecated */ $connection->getDoctrineSchemaManager()->getDatabasePlatform()->registerDoctrineTypeMapping($key, $value);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
149
            }
150
151
            return $connection->getDoctrineSchemaManager();
152
        }
153
154
        return $connection->getSchemaBuilder();
155
    }
156
}
157