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 — v4dot1 ( bece96 )
by Cristian
10:39 queued 08:03
created

HasIdentifiableAttribute::getIdentifiableName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
namespace Backpack\CRUD\app\Models\Traits;
4
5
trait HasIdentifiableAttribute
6
{
7
    /**
8
     * Get the name of the attribute that best defines the entry, from the user perspective.
9
     *
10
     * Rephrased: In most cases a user will NOT identify an Article because its ID is "4", but
11
     * because its name is "10 Ways to Burn Fat". This method returns the column in the database
12
     * that represents that is better to show to the user as an identifier rather than the ID.
13
     * Ex: name, title, label, description etc.
14
     *
15
     * @return string The name of the column that best defines this entry from the user perspective.
16
     */
17
    public static function getIdentifiableName()
18
    {
19
        $model = (new self);
20
        if (method_exists($model, 'identifiableName')) {
21
            return $model->identifiableName();
22
        }
23
24
        return static::guessIdentifiableColumnName();
25
    }
26
27
    /**
28
     * Get the most likely column in the db table that could be used as an identifiable attribute.
29
     *
30
     * @return string The name of the column in the database that is most likely to be a good indentifying attribute.
31
     */
32
    public static function guessIdentifiableColumnName()
33
    {
34
        $instance = new static();
35
        $conn = $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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
36
        $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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
37
        $columns = $conn->getDoctrineSchemaManager()->listTableColumns($table);
38
        $indexes = $conn->getDoctrineSchemaManager()->listTableIndexes($table);
39
        $columnsNames = array_keys($columns);
40
41
        // these column names are sensible defaults for lots of use cases
42
        $sensibleDefaultNames = ['name', 'title', 'description', 'label'];
43
44
        // if any of the sensibleDefaultNames column exists
45
        // that's probably a good choice
46
        foreach ($sensibleDefaultNames as $defaultName) {
47
            if (in_array($defaultName, $columnsNames)) {
48
                return $defaultName;
49
            }
50
        }
51
52
        // get indexed columns in database table
53
        $indexedColumns = [];
54
        foreach ($indexes as $index) {
55
            $indexColumns = $index->getColumns();
56
            foreach ($indexColumns as $ic) {
57
                array_push($indexedColumns, $ic);
58
            }
59
        }
60
61
        // if none of the sensible defaults exists
62
        // we get the first column from database
63
        // that is NOT indexed (usually primary, foreign keys)
64
        foreach ($columns as $columnName => $columnProperties) {
65
            if (! in_array($columnName, $indexedColumns)) {
66
67
                //check for convention "field<_id>" in case developer didn't add foreign key constraints.
68
                if (strpos($columnName, '_id') !== false) {
69
                    continue;
70
                }
71
72
                return $columnName;
73
            }
74
        }
75
76
        // in case everything fails we just return the first column in database
77
        return \Arr::first($columnsNames);
78
    }
79
}
80