phpstan-doctrine icon indicating copy to clipboard operation
phpstan-doctrine copied to clipboard

[Feature Request] StringMaxIndexLength Rule

Open alexander-schranz opened this issue 3 years ago • 0 comments

In the last months I worked in a project which uses MariaDB. While on MySQL utf8mb4 I'm not able to create Indexes longer then 191 chars, MariaDB in its default settings allow to create Indexes longer as 191 on utf8mb4 to be created. This sadly ends in runtime error as the datastorage (innodb) behind doesn't really support it. It would be nice to have a phpstan rule to avoid this kind of error. The tricky part is that combined indexes should also not go over the 191 limit this means if I have a field email (164) and a field phone (32) and a combined index on them. It is 160 which would also be too long and ends in strange runtime errors.

I thought maybe phpstan-doctrine could be used to create such a rule.

The following metadata on doctrine would need to analyzed to get to it Index, UniqueConstraint and Column with unique flag.

#[Index(name: "email_phone_idx", fields: ["email", "phone"])] // to long email and phone combined
// or
#[UniqueConstraint(name: "email_phone_idx", fields: ["email", "phone"])
class Test {
    #[Column(type: "string", length: 160)]
    protected $email;

    #[Column(type: "string", length: 32)]
    protected $phone;

    #[Column(type: "string", length: 255, unique: true)] // to long for unique index
    protected $other;
}

I know that other databses have also some kind of limitation for indexes but not sure. In MariaDB/MySQL utf8mb4 it is 191, other charsets it is 255. For postgres I could not yet find out what limit there is. But maybe the rule could be configurable.

Not sure if something similar already exists in phpstan-dba. /cc @staabm

alexander-schranz avatar Aug 17 '22 23:08 alexander-schranz