1 | <?php |
||
2 | |||
3 | |||
4 | namespace TheCodingMachine\TDBM\Schema; |
||
5 | |||
6 | use Doctrine\DBAL\Schema\ForeignKeyConstraint; |
||
7 | |||
8 | class ForeignKey |
||
9 | { |
||
10 | public const FOREIGN_TABLE = 'foreignTable'; |
||
11 | public const LOCAL_COLUMNS = 'localColumns'; |
||
12 | public const FOREIGN_COLUMNS = 'foreignColumns'; |
||
13 | |||
14 | /** @var string */ |
||
15 | private $foreignTable; |
||
16 | /** @var string[] */ |
||
17 | private $localColumns; |
||
18 | /** @var string[] */ |
||
19 | private $foreignColumns; |
||
20 | |||
21 | |||
22 | /** |
||
23 | * @param array<string, string|array<string>> $foreignKey |
||
24 | */ |
||
25 | public function __construct(array $foreignKey) |
||
26 | { |
||
27 | $this->foreignTable = $foreignKey[self::FOREIGN_TABLE]; |
||
0 ignored issues
–
show
|
|||
28 | $this->localColumns = $foreignKey[self::LOCAL_COLUMNS]; |
||
0 ignored issues
–
show
It seems like
$foreignKey[self::LOCAL_COLUMNS] can also be of type string . However, the property $localColumns is declared as type string[] . Maybe add an additional type check?
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly. For example, imagine you have a variable Either this assignment is in error or a type check should be added for that assignment. class Id
{
public $id;
public function __construct($id)
{
$this->id = $id;
}
}
class Account
{
/** @var Id $id */
public $id;
}
$account_id = false;
if (starsAreRight()) {
$account_id = new Id(42);
}
$account = new Account();
if ($account instanceof Id)
{
$account->id = $account_id;
}
Loading history...
|
|||
29 | $this->foreignColumns = $foreignKey[self::FOREIGN_COLUMNS]; |
||
0 ignored issues
–
show
It seems like
$foreignKey[self::FOREIGN_COLUMNS] can also be of type string . However, the property $foreignColumns is declared as type string[] . Maybe add an additional type check?
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly. For example, imagine you have a variable Either this assignment is in error or a type check should be added for that assignment. class Id
{
public $id;
public function __construct($id)
{
$this->id = $id;
}
}
class Account
{
/** @var Id $id */
public $id;
}
$account_id = false;
if (starsAreRight()) {
$account_id = new Id(42);
}
$account = new Account();
if ($account instanceof Id)
{
$account->id = $account_id;
}
Loading history...
|
|||
30 | } |
||
31 | |||
32 | public static function createFromFk(ForeignKeyConstraint $fk): self |
||
33 | { |
||
34 | return new self([ |
||
35 | self::FOREIGN_TABLE => $fk->getForeignTableName(), |
||
36 | self::LOCAL_COLUMNS => $fk->getUnquotedLocalColumns(), |
||
37 | self::FOREIGN_COLUMNS => $fk->getUnquotedForeignColumns(), |
||
38 | ]); |
||
39 | } |
||
40 | |||
41 | /** |
||
42 | * @return array<string> |
||
43 | */ |
||
44 | public function getUnquotedLocalColumns(): array |
||
45 | { |
||
46 | return $this->localColumns; |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * @return array<string> |
||
51 | */ |
||
52 | public function getUnquotedForeignColumns(): array |
||
53 | { |
||
54 | return $this->foreignColumns; |
||
55 | } |
||
56 | |||
57 | public function getForeignTableName(): string |
||
58 | { |
||
59 | return $this->foreignTable; |
||
60 | } |
||
61 | |||
62 | private $cacheKey; |
||
63 | public function getCacheKey(): string |
||
64 | { |
||
65 | if ($this->cacheKey === null) { |
||
66 | $this->cacheKey = 'from__' . implode(',', $this->getUnquotedLocalColumns()) . '__to__table__' . $this->getForeignTableName() . '__columns__' . implode(',', $this->getUnquotedForeignColumns()); |
||
67 | } |
||
68 | return $this->cacheKey; |
||
69 | } |
||
70 | } |
||
71 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.