Commit graph

4 commits

Author SHA1 Message Date
Bartosz Dziewoński
bce5fd0a18 rdbms: Add Phan type hint for allowed operators in expressions
It is really annoying that we have to escape '<' and '>'
as '\x3E' and '\x3C', otherwise Phan will not accept them
(emitting PhanUnextractableAnnotation).

Phan will also escape them in the output, generating errors like:
  Argument 2 ($op) is '=<' of type '=\x3c'
  but \Wikimedia\Rdbms\IDatabase::expr() takes
  '!='|'='|'LIKE'|'NOT LIKE'|'\x3c'|'\x3c='|'\x3e'|'\x3e='

I've wanted to do this for a while, but I wasn't sure if it was
worth it, because the escaping makes the type hints and the errors
hard to read. Today I noticed a bug in UserSelectQueryBuilder
that would have been caught by this, and convinced myself.

Change-Id: I7a72368446f463a99b3d0cc7a90e8f7cdca454fe
2024-05-22 01:38:31 +02:00
Bartosz Dziewoński
9ae6fbda79 rdbms: Warn when given an assoc array but keys are ignored
In some cases ISQLPlatform::makeList() will accept an associative
array (with string keys), but ignore the keys completely. Provide a
warning when this happens, and improve type hints so that Phan warns
about it too.

(We only warn about string keys, and not about gaps in an array
with int keys, because they arise often due to using array_diff()
or array_unique(), and that would trigger too many warnings.)

In Expression we already type-hinted it as a list (int keys with no
gaps), and we're intentionally less flexible about some other cases,
so check for gaps too and throw an exception instead of a warning.

Change-Id: I63717d16eae7cccd929b5d232944b97989113b1e
2024-05-20 17:20:28 +00:00
Bartosz Dziewoński
600b012b49 rdbms: Tighten type checks and PHPDoc for SQL WHERE/$conds arrays
In all cases, these mistakes would have already caused an error
("Wikimedia\Rdbms\Expression could not be converted to string")
or a notice ("Array to string conversion"). But now we also get
Phan warnings to avoid them, and a better message if they happen.

Add tests describing the invalid cases.

Change-Id: Idd4ba30a8145451e85018c35d5441910af76f009
2024-05-17 14:45:53 +00:00
Bartosz Dziewoński
e7c3381306 rdbms: Add more tests, and Phan tests, for expr() / Expression
I've been having fun documenting all the terrible array shapes
accepted by the various RDBMS methods with Phan type hints,
but I'm increasingly worried that either Phan will change how
it interprets them or that someone will break them by accident.

Add a test file, similar to the one we have for testing taint
annotations for Phan SecurityCheckPlugin, to guard against that.
Start filling it with some tests for expr() / Expression.

Add two missing test cases I noticed while writing these.

Change-Id: Icb54d5a7529f7f82ff5d130dcea0a22450155c10
2024-05-12 01:45:45 +02:00