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

Test Failed
Pull Request — main (#5584)
by Pedro
10:52
created

guessIdentifiableColumnName()   B

Complexity

Conditions 7
Paths 15

Size

Total Lines 42
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 18
nc 15
nop 0
dl 0
loc 42
rs 8.8333
c 0
b 0
f 0
1
<?php
2
3
namespace Backpack\CRUD\app\Models\Traits;
4
5
use Illuminate\Support\Arr;
6
7
trait HasIdentifiableAttribute
8
{
9
    /**
10
     * Get the name of the attribute that best defines the entry, from the user perspective.
11
     *
12
     * Rephrased: In most cases a user will NOT identify an Article because its ID is "4", but
13
     * because its name is "10 Ways to Burn Fat". This method returns the column in the database
14
     * that represents what is better to show to the user as an identifier rather than the ID.
15
     * Ex: name, title, label, description etc.
16
     *
17
     * @return string The name of the column that best defines this entry from the user perspective.
18
     */
19
    public function identifiableAttribute()
20
    {
21
        if (property_exists($this, 'identifiableAttribute')) {
22
            return $this->identifiableAttribute;
23
        }
24
25
        return static::guessIdentifiableColumnName();
26
    }
27
28
    /**
29
     * Get the most likely column in the db table that could be used as an identifiable attribute.
30
     *
31
     * @return string The name of the column in the database that is most likely to be a good identifying attribute.
32
     *
33
     * @throws \Exception
34
     */
35
    private static function guessIdentifiableColumnName()
36
    {
37
        $instance = new static();
38
        $connection = $instance->getConnectionWithExtraTypeMappings();
0 ignored issues
show
Bug introduced by
It seems like getConnectionWithExtraTypeMappings() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

38
        /** @scrutinizer ignore-call */ 
39
        $connection = $instance->getConnectionWithExtraTypeMappings();
Loading history...
39
        $table = $instance->getTableWithPrefix();
0 ignored issues
show
Bug introduced by
It seems like getTableWithPrefix() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

39
        /** @scrutinizer ignore-call */ 
40
        $table = $instance->getTableWithPrefix();
Loading history...
40
        $columnNames = app('DatabaseSchema')->listTableColumnsNames($connection->getName(), $table);
41
        $indexes = app('DatabaseSchema')->listTableIndexes($connection->getName(), $table);
42
43
        // these column names are sensible defaults for lots of use cases
44
        $sensibleDefaultNames = ['name', 'title', 'description', 'label'];
45
46
        // if any of the sensibleDefaultNames column exists
47
        // that's probably a good choice
48
        foreach ($sensibleDefaultNames as $defaultName) {
49
            if (in_array($defaultName, $columnNames)) {
50
                return $defaultName;
51
            }
52
        }
53
54
        // if none of the sensible defaults exists
55
        // we get the first column from database
56
        // that is NOT indexed (usually primary, foreign keys)
57
        foreach ($columnNames as $columnName) {
58
            if (! in_array($columnName, $indexes)) {
59
                //check for convention "field<_id>" in case developer didn't add foreign key constraints.
60
                if (strpos($columnName, '_id') !== false) {
61
                    continue;
62
                }
63
64
                return $columnName;
65
            }
66
        }
67
68
        // in case everything fails we just return the first column in database
69
        $firstColumnInTable = Arr::first($columnNames);
70
        if (! empty($firstColumnInTable)) {
71
            return $firstColumnInTable;
72
        }
73
74
        // if there are no columns in the table, we need to throw an exception as there is nothing we can use to
75
        // correlate with the entry. Developer need to tell Backpack what attribute to use as identifier.
76
        throw new \Exception("There are no columns in the table «{$table}». Please add a column to the table or define a 'public function identifiableAttribute()' in the model.");
77
    }
78
}
79