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 — hack-into-schema ( d95c46...f61909 )
by Pedro
10:57
created

DatabaseSchema::getCreateSchema()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Backpack\CRUD\app\Library\Database;
4
5
use Illuminate\Support\Facades\DB;
6
use Illuminate\Support\LazyCollection;
7
8
final class DatabaseSchema
9
{
10
    private static $schema;
11
12
    /**
13
     * Return the schema for the table.
14
     *
15
     * @param  string  $connection
16
     * @param  string  $table
17
     */
18
    public static function getForTable(string $connection, string $table)
19
    {
20
        self::generateDatabaseSchema($connection, $table);
21
        return self::$schema[$connection][$table] ?? null;
22
    }
23
24
    /**
25
     * Generates and store the database schema.
26
     *
27
     * @param  string  $connection
28
     * @param  string  $table
29
     * @return void
30
     */
31
    private static function generateDatabaseSchema(string $connection, string $table)
32
    {
33
        if (! isset(self::$schema[$connection]) || !isset(self::$schema[$connection][$table])) {
34
            self::$schema[$connection] = self::mapTables($connection);
35
        }
36
    }
37
38
    /**
39
     * Map the tables from raw db values into an usable array.
40
     *
41
     * @param  string  $connection
42
     * @return array
43
     */
44
    private static function mapTables(string $connection)
45
    {
46
        return 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

46
        return 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
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

46
        return 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...
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

46
        return LazyCollection::make(/** @scrutinizer ignore-type */ self::getCreateSchema($connection)->getTables())->mapWithKeys(function ($table, $key) use ($connection) {
Loading history...
47
            $tableName = is_array($table) ? $table['name'] : $table->getName();
48
            
49
            if(self::$schema[$connection][$tableName] ?? false) {
50
                return [$tableName => self::$schema[$connection][$tableName]];
51
            }
52
            
53
            if (is_array($table)) {
54
                $table = new Table(self::mapTableColumns($connection, $tableName));
55
                
56
            }
57
58
            return [$tableName => $table];
59
        })->toArray();
60
    }
61
62
    private static function getIndexColumnNames($connection, $table)
63
    {
64
        $schemaManager = self::getSchemaManager($connection);
65
        $indexes = method_exists($schemaManager, 'listTableIndexes') ? $schemaManager->listTableIndexes($table) : $schemaManager->getIndexes($table);
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

65
        $indexes = method_exists($schemaManager, 'listTableIndexes') ? $schemaManager->listTableIndexes($table) : $schemaManager->/** @scrutinizer ignore-call */ getIndexes($table);

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...
66
67
        $indexes = array_map(function ($index) {
68
            return is_array($index) ? $index['columns'] : $index->getColumns();
69
        }, $indexes);
70
71
        $indexes = \Illuminate\Support\Arr::flatten($indexes);
72
73
        return array_unique($indexes);
74
    }
75
76
    private static function mapTableColumns($connection, $table)
77
    {
78
        $indexedColumns = self::getIndexColumnNames($connection, $table);
79
80
        return LazyCollection::make(self::getSchemaManager($connection)->getColumns($table))->mapWithKeys(function ($column, $key) use ($indexedColumns) {
0 ignored issues
show
Bug introduced by
self::getSchemaManager($...on)->getColumns($table) of type 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

80
        return LazyCollection::make(/** @scrutinizer ignore-type */ self::getSchemaManager($connection)->getColumns($table))->mapWithKeys(function ($column, $key) use ($indexedColumns) {
Loading history...
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

80
        return LazyCollection::make(self::getSchemaManager($connection)->/** @scrutinizer ignore-call */ getColumns($table))->mapWithKeys(function ($column, $key) use ($indexedColumns) {

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...
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

80
        return LazyCollection::make(self::getSchemaManager($connection)->getColumns($table))->mapWithKeys(function ($column, /** @scrutinizer ignore-unused */ $key) use ($indexedColumns) {

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...
81
            $column['index'] = array_key_exists($column['name'], $indexedColumns) ? true : false;
82
83
            return [$column['name'] => $column];
84
        })->toArray();
85
    }
86
87
    private static function getCreateSchema(string $connection)
88
    {
89
        $schemaManager = self::getSchemaManager($connection);
90
        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

90
        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...
91
    }
92
93
    private static function getSchemaManager(string $connection)
94
    {
95
        $connection = DB::connection($connection);
96
97
        return method_exists($connection, 'getDoctrineSchemaManager') ? $connection->getDoctrineSchemaManager() : $connection->getSchemaBuilder();
98
    }
99
100
    public function listTableColumnsNames(string $connection, string $table)
101
    {
102
        $table = self::getForTable($connection, $table);
103
        return array_keys($table->getColumns());
104
    }
105
106
    public function listTableIndexes(string $connection, string $table)
107
    {
108
        return self::getIndexColumnNames($connection, $table);
109
    }
110
}
111