wiki.techinc.nl/tests/phan/RdbmsTypeHintsTest.php
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

71 lines
2.5 KiB
PHP

<?php
// phpcs:disable
/*
* Some RDBMS interfaces have methods with rather complicated Phan annotations. This test ensures
* that future versions of Phan still handle them correctly, and that future developers don't mess
* them up.
*
* If Phan reports new issues or unused suppressions in this file, DO NOT just fix the errors,
* and instead make sure that your patch is not breaking the annotations.
*/
die( 'This file should never be loaded' );
class RdbmsTypeHintsTest {
function testExpr( \Wikimedia\Rdbms\IDatabase $db ) {
$conds = [];
$expr = $db->expr( 'a', '=', 1 );
// Return value of ->and() etc. must be used
// @phan-suppress-next-line PhanPluginUseReturnValueKnown
$expr->and( 'a', '=', 1 );
// Typo in the operator
// @phan-suppress-next-line PhanTypeMismatchArgumentProbablyReal
$conds[] = $db->expr( 'a', '=<', 1 );
// We accept lots of types, but not all
// @phan-suppress-next-line PhanTypeMismatchArgumentProbablyReal
$conds[] = $db->expr( 'a', '=', new \stdClass );
// Empty arrays are not allowed
// @phan-suppress-next-line PhanTypeMismatchArgument
$conds[] = $db->expr( 'a', '=', [] );
// Null in arrays is not allowed (unlike in the `$qb->where( 'a' => [ ... ] )` syntax)
// @phan-suppress-next-line PhanTypeMismatchArgument
$conds[] = $db->expr( 'a', '=', [ 1, null, 2 ] );
// Associative arrays are not allowed
// @phan-suppress-next-line PhanTypeMismatchArgument
$conds[] = $db->expr( 'a', '=', [ 'a' => 1, 'b' => 2 ] );
// Arrays with gaps are not allowed (unlike in the `$qb->where( 'a' => [ ... ] )` syntax)
// @phan-suppress-next-line PhanTypeMismatchArgument
$conds[] = $db->expr( 'a', '=', array_unique( [ 1, 1, 2, 2 ] ) );
}
function testSelectConds( \Wikimedia\Rdbms\IDatabase $db ) {
// Missing key for array value
// @phan-suppress-next-line PhanTypeMismatchArgument
$db->select( 'a', 'b', [ [ 1, 2, 3 ] ] );
// Empty array value
// @phan-suppress-next-line PhanTypeMismatchArgument
$db->select( 'a', 'b', [ 'x' => [] ] );
// Unexpected key for IExpression value
// @phan-suppress-next-line PhanTypeMismatchArgument
$db->select( 'a', 'b', [ 'x' => $db->expr( 'x', '=', 1 ) ] );
// Nested array in array value
// @phan-suppress-next-line PhanTypeMismatchArgument
$db->select( 'a', 'b', [ 'x' => [ 1, 2, [ 3 ] ] ] );
// Associative arrays are not allowed
// @phan-suppress-next-line PhanTypeMismatchArgument
$db->select( 'a', 'b', [ 'x' => [ 'a' => 1, 'b' => 2 ] ] );
}
}