Passed
Push — dependabot/composer/satooshi/p... ( 44e8fc...9a8d56 )
by David
02:55
created

src/Schema/ForeignKey.php (3 issues)

1
<?php
2
3
4
namespace TheCodingMachine\TDBM\Schema;
5
6
7
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
8
9
class ForeignKey
10
{
11
    public const FOREIGN_TABLE = 'foreignTable';
12
    public const LOCAL_COLUMNS = 'localColumns';
13
    public const FOREIGN_COLUMNS = 'foreignColumns';
14
15
    /**
16
     * @var array<string, string|array<string>>
17
     */
18
    private $foreignKey;
19
20
    /**
21
     * @param array<string, string|array<string>> $foreignKey
22
     */
23
    public function __construct(array $foreignKey)
24
    {
25
        $this->foreignKey = $foreignKey;
26
    }
27
28
    public static function createFromFk(ForeignKeyConstraint $fk): self
29
    {
30
        return new self([
31
            self::FOREIGN_TABLE => $fk->getForeignTableName(),
32
            self::LOCAL_COLUMNS => $fk->getUnquotedLocalColumns(),
33
            self::FOREIGN_COLUMNS => $fk->getUnquotedForeignColumns(),
34
        ]);
35
    }
36
37
    /**
38
     * @return array<string>
39
     */
40
    public function getUnquotedLocalColumns(): array
41
    {
42
        return $this->foreignKey[self::LOCAL_COLUMNS];
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->foreignKey[self::LOCAL_COLUMNS] could return the type string which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
43
    }
44
45
    /**
46
     * @return array<string>
47
     */
48
    public function getUnquotedForeignColumns(): array
49
    {
50
        return $this->foreignKey[self::FOREIGN_COLUMNS];
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->foreignKey[self::FOREIGN_COLUMNS] could return the type string which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
51
    }
52
53
    public function getForeignTableName(): string
54
    {
55
        return $this->foreignKey[self::FOREIGN_TABLE];
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->foreignKey[self::FOREIGN_TABLE] could return the type string[] which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
56
    }
57
58
    public function getCacheKey(): string
59
    {
60
        return 'from__'.implode(',', $this->getUnquotedLocalColumns()) . '__to__table__' . $this->getForeignTableName() . '__columns__' . implode(',', $this->getUnquotedForeignColumns());
61
    }
62
}
63