Skip to content

Commit 1f5a9b5

Browse files
authored
Use iterator criteria instead of QueryExpression (#23182)
1 parent 93f66d9 commit 1f5a9b5

8 files changed

Lines changed: 314 additions & 265 deletions

File tree

.phpstan-baseline.php

Lines changed: 0 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -4759,12 +4759,6 @@
47594759
'count' => 1,
47604760
'path' => __DIR__ . '/src/DbUtils.php',
47614761
];
4762-
$ignoreErrors[] = [
4763-
'message' => '#^Parameter \\#1 \\$itemtype of static method Session\\:\\:haveTranslations\\(\\) expects string, class\\-string\\<CommonDBTM\\>\\|null given\\.$#',
4764-
'identifier' => 'argument.type',
4765-
'count' => 3,
4766-
'path' => __DIR__ . '/src/DbUtils.php',
4767-
];
47684762
$ignoreErrors[] = [
47694763
'message' => '#^Parameter \\#1 \\$object_or_class of function is_a expects object\\|string, class\\-string\\<CommonDBTM\\>\\|null given\\.$#',
47704764
'identifier' => 'argument.type',
@@ -5311,12 +5305,6 @@
53115305
'count' => 1,
53125306
'path' => __DIR__ . '/src/Dropdown.php',
53135307
];
5314-
$ignoreErrors[] = [
5315-
'message' => '#^Parameter \\#1 \\$itemtype of static method Session\\:\\:haveTranslations\\(\\) expects string, class\\-string\\<CommonDBTM\\>\\|null given\\.$#',
5316-
'identifier' => 'argument.type',
5317-
'count' => 2,
5318-
'path' => __DIR__ . '/src/Dropdown.php',
5319-
];
53205308
$ignoreErrors[] = [
53215309
'message' => '#^Parameter \\#1 \\$object_or_class of function is_a expects object\\|string, class\\-string\\<CommonDBTM\\>\\|null given\\.$#',
53225310
'identifier' => 'argument.type',
@@ -13459,18 +13447,6 @@
1345913447
'count' => 1,
1346013448
'path' => __DIR__ . '/src/Glpi/Search/Provider/SQLProvider.php',
1346113449
];
13462-
$ignoreErrors[] = [
13463-
'message' => '#^Binary operation "\\." between array\\<string\\>\\|Glpi\\\\DBAL\\\\QueryExpression\\|string and \' \\<\\> \' results in an error\\.$#',
13464-
'identifier' => 'binaryOp.invalid',
13465-
'count' => 1,
13466-
'path' => __DIR__ . '/src/Glpi/Search/Provider/SQLProvider.php',
13467-
];
13468-
$ignoreErrors[] = [
13469-
'message' => '#^Binary operation "\\." between array\\<string\\>\\|Glpi\\\\DBAL\\\\QueryExpression\\|string and \' \\= \' results in an error\\.$#',
13470-
'identifier' => 'binaryOp.invalid',
13471-
'count' => 1,
13472-
'path' => __DIR__ . '/src/Glpi/Search/Provider/SQLProvider.php',
13473-
];
1347413450
$ignoreErrors[] = [
1347513451
'message' => '#^Cannot access offset \'id\' on non\\-empty\\-array\\<non\\-empty\\-string, string\\|null\\>\\|int\\<1, max\\>\\|string\\.$#',
1347613452
'identifier' => 'offsetAccess.nonOffsetAccessible',
@@ -13561,24 +13537,6 @@
1356113537
'count' => 2,
1356213538
'path' => __DIR__ . '/src/Glpi/Search/Provider/SQLProvider.php',
1356313539
];
13564-
$ignoreErrors[] = [
13565-
'message' => '#^Parameter \\#1 \\$expression of class Glpi\\\\DBAL\\\\QueryExpression constructor expects string, array\\<string\\>\\|string given\\.$#',
13566-
'identifier' => 'argument.type',
13567-
'count' => 2,
13568-
'path' => __DIR__ . '/src/Glpi/Search/Provider/SQLProvider.php',
13569-
];
13570-
$ignoreErrors[] = [
13571-
'message' => '#^Parameter \\#1 \\$expression of static method Glpi\\\\DBAL\\\\QueryFunction\\:\\:convert\\(\\) expects Glpi\\\\DBAL\\\\QueryExpression\\|string, array\\<string\\>\\|Glpi\\\\DBAL\\\\QueryExpression\\|string\\|null given\\.$#',
13572-
'identifier' => 'argument.type',
13573-
'count' => 1,
13574-
'path' => __DIR__ . '/src/Glpi/Search/Provider/SQLProvider.php',
13575-
];
13576-
$ignoreErrors[] = [
13577-
'message' => '#^Parameter \\#1 \\$field of static method Glpi\\\\Search\\\\Provider\\\\SQLProvider\\:\\:makeTextCriteria\\(\\) expects string, array\\<string\\>\\|Glpi\\\\DBAL\\\\QueryExpression\\|string given\\.$#',
13578-
'identifier' => 'argument.type',
13579-
'count' => 3,
13580-
'path' => __DIR__ . '/src/Glpi/Search/Provider/SQLProvider.php',
13581-
];
1358213540
$ignoreErrors[] = [
1358313541
'message' => '#^Parameter \\#1 \\$haystack of function str_contains expects string, int\\|string given\\.$#',
1358413542
'identifier' => 'argument.type',
@@ -13609,12 +13567,6 @@
1360913567
'count' => 1,
1361013568
'path' => __DIR__ . '/src/Glpi/Search/Provider/SQLProvider.php',
1361113569
];
13612-
$ignoreErrors[] = [
13613-
'message' => '#^Parameter \\#1 \\$itemtype of static method Session\\:\\:haveTranslations\\(\\) expects string, class\\-string\\<CommonDBTM\\>\\|null given\\.$#',
13614-
'identifier' => 'argument.type',
13615-
'count' => 4,
13616-
'path' => __DIR__ . '/src/Glpi/Search/Provider/SQLProvider.php',
13617-
];
1361813570
$ignoreErrors[] = [
1361913571
'message' => '#^Parameter \\#1 \\$num of function abs expects float\\|int, int\\<min, \\-1\\>\\|string given\\.$#',
1362013572
'identifier' => 'argument.type',
@@ -13657,12 +13609,6 @@
1365713609
'count' => 1,
1365813610
'path' => __DIR__ . '/src/Glpi/Search/Provider/SQLProvider.php',
1365913611
];
13660-
$ignoreErrors[] = [
13661-
'message' => '#^Parameter \\#1 \\$val of static method Glpi\\\\Search\\\\Provider\\\\SQLProvider\\:\\:makeTextSearchValue\\(\\) expects string, int\\|string given\\.$#',
13662-
'identifier' => 'argument.type',
13663-
'count' => 3,
13664-
'path' => __DIR__ . '/src/Glpi/Search/Provider/SQLProvider.php',
13665-
];
1366613612
$ignoreErrors[] = [
1366713613
'message' => '#^Parameter \\#1 \\$val of static method Html\\:\\:computeGenericDateTimeSearch\\(\\) expects string, int\\|string given\\.$#',
1366813614
'identifier' => 'argument.type',
@@ -13699,42 +13645,12 @@
1369913645
'count' => 4,
1370013646
'path' => __DIR__ . '/src/Glpi/Search/Provider/SQLProvider.php',
1370113647
];
13702-
$ignoreErrors[] = [
13703-
'message' => '#^Parameter \\#2 \\$val of static method Glpi\\\\Search\\\\Provider\\\\SQLProvider\\:\\:makeTextCriteria\\(\\) expects string, int\\|string given\\.$#',
13704-
'identifier' => 'argument.type',
13705-
'count' => 2,
13706-
'path' => __DIR__ . '/src/Glpi/Search/Provider/SQLProvider.php',
13707-
];
13708-
$ignoreErrors[] = [
13709-
'message' => '#^Parameter \\#3 \\$itemtype of static method Glpi\\\\Search\\\\Provider\\\\SQLProvider\\:\\:getDropdownTranslationJoinCriteria\\(\\) expects class\\-string\\<CommonDBTM\\>, class\\-string\\<CommonDBTM\\>\\|null given\\.$#',
13710-
'identifier' => 'argument.type',
13711-
'count' => 3,
13712-
'path' => __DIR__ . '/src/Glpi/Search/Provider/SQLProvider.php',
13713-
];
13714-
$ignoreErrors[] = [
13715-
'message' => '#^Part \\$date_computation \\(array\\<string\\>\\|Glpi\\\\DBAL\\\\QueryExpression\\|string\\|null\\) of encapsed string cannot be cast to string\\.$#',
13716-
'identifier' => 'encapsedStringPart.nonString',
13717-
'count' => 2,
13718-
'path' => __DIR__ . '/src/Glpi/Search/Provider/SQLProvider.php',
13719-
];
13720-
$ignoreErrors[] = [
13721-
'message' => '#^Part \\$tocompute \\(array\\<string\\>\\|Glpi\\\\DBAL\\\\QueryExpression\\|string\\) of encapsed string cannot be cast to string\\.$#',
13722-
'identifier' => 'encapsedStringPart.nonString',
13723-
'count' => 7,
13724-
'path' => __DIR__ . '/src/Glpi/Search/Provider/SQLProvider.php',
13725-
];
1372613648
$ignoreErrors[] = [
1372713649
'message' => '#^Part \\$token \\(array\\<int\\>\\|int\\|string\\|null\\) of encapsed string cannot be cast to string\\.$#',
1372813650
'identifier' => 'encapsedStringPart.nonString',
1372913651
'count' => 1,
1373013652
'path' => __DIR__ . '/src/Glpi/Search/Provider/SQLProvider.php',
1373113653
];
13732-
$ignoreErrors[] = [
13733-
'message' => '#^Possibly invalid array key type array\\<string\\>\\|Glpi\\\\DBAL\\\\QueryExpression\\|string\\.$#',
13734-
'identifier' => 'array.invalidKey',
13735-
'count' => 3,
13736-
'path' => __DIR__ . '/src/Glpi/Search/Provider/SQLProvider.php',
13737-
];
1373813654
$ignoreErrors[] = [
1373913655
'message' => '#^Parameter \\#1 \\$itemtype of function getTableForItemType expects class\\-string\\<CommonDBTM\\>, string given\\.$#',
1374013656
'identifier' => 'argument.type',

src/DbUtils.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1314,8 +1314,8 @@ public function getTreeLeafValueName($table, $ID, $withcomment = false, $transla
13141314
$SELECTCOMMENT = new QueryExpression("'' AS " . $DB->quoteName('transcomment'));
13151315
$JOIN = [];
13161316
$JOINS = [];
1317-
if ($translate) {
1318-
if (Session::haveTranslations($this->getItemTypeForTable($table), 'name')) {
1317+
if ($translate && $transitemtype = $this->getItemTypeForTable($table)) {
1318+
if (Session::haveTranslations($transitemtype, 'name')) {
13191319
$SELECTNAME = 'namet.value AS transname';
13201320
$JOINS['glpi_dropdowntranslations AS namet'] = [
13211321
'ON' => [
@@ -1330,7 +1330,8 @@ public function getTreeLeafValueName($table, $ID, $withcomment = false, $transla
13301330
],
13311331
];
13321332
}
1333-
if (Session::haveTranslations($this->getItemTypeForTable($table), 'comment')) {
1333+
$transitemtype = $this->getItemTypeForTable($table);
1334+
if ($transitemtype && Session::haveTranslations($transitemtype, 'comment')) {
13341335
$SELECTCOMMENT = 'namec.value AS transcomment';
13351336
$JOINS['glpi_dropdowntranslations AS namec'] = [
13361337
'ON' => [
@@ -1420,8 +1421,8 @@ public function getTreeValueCompleteName($table, $ID, $withcomment = false, $tra
14201421
$SELECTNAME = new QueryExpression("'' AS " . $DB->quoteName('transname'));
14211422
$JOIN = [];
14221423
$JOINS = [];
1423-
if ($translate) {
1424-
if (Session::haveTranslations($this->getItemTypeForTable($table), 'completename')) {
1424+
if ($translate && $transitemtype = $this->getItemTypeForTable($table)) {
1425+
if (Session::haveTranslations($transitemtype, 'completename')) {
14251426
$SELECTNAME = 'namet.value AS transname';
14261427
$JOINS['glpi_dropdowntranslations AS namet'] = [
14271428
'ON' => [

src/Dropdown.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,8 @@ public static function getDropdownName($table, $id, $withcomment = false, $trans
570570
if ($id) {
571571
$SELECTNAME = new QueryExpression("'' AS " . $DB->quoteName('transname'));
572572
$JOIN = [];
573-
if ($translate && Session::haveTranslations(getItemTypeForTable($table), 'name')) {
573+
$transitemtype = getItemTypeForTable($table);
574+
if ($translate && $transitemtype && Session::haveTranslations($transitemtype, 'name')) {
574575
$SELECTNAME = 'namet.value AS transname';
575576
$JOIN = [
576577
'LEFT JOIN' => [
@@ -682,6 +683,7 @@ public static function getDropdownComments(string $table, int $id, bool $transla
682683

683684
if (
684685
$translate
686+
&& $itemtype
685687
&& Session::haveTranslations($itemtype, 'comment')
686688
) {
687689
$criteria['SELECT'][] = 'comment_translations.value AS translated_comment';

src/Glpi/Api/HL/Search/RecordSet.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
namespace Glpi\Api\HL\Search;
3636

37+
use CommonDBTM;
3738
use Glpi\Api\HL\APIException;
3839
use Glpi\Api\HL\Doc as Doc;
3940
use Glpi\Api\HL\Search;
@@ -149,6 +150,7 @@ public function getHydrationCriteria(string $fkey, string $table, string $itemty
149150
$field_parts = explode('.', $sql_field);
150151
$field_only = end($field_parts);
151152
// Handle translatable fields
153+
/** @var class-string<CommonDBTM> $itemtype */
152154
if (Session::haveTranslations($itemtype, $field_only)) {
153155
$trans_alias = "{$join_name}__{$field_only}__trans";
154156
$trans_alias = hash('xxh3', $trans_alias);

src/Glpi/DBAL/Operator.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
/**
4+
* ---------------------------------------------------------------------
5+
*
6+
* GLPI - Gestionnaire Libre de Parc Informatique
7+
*
8+
* http://glpi-project.org
9+
*
10+
* @copyright 2015-2026 Teclib' and contributors.
11+
* @licence https://www.gnu.org/licenses/gpl-3.0.html
12+
*
13+
* ---------------------------------------------------------------------
14+
*
15+
* LICENSE
16+
*
17+
* This file is part of GLPI.
18+
*
19+
* This program is free software: you can redistribute it and/or modify
20+
* it under the terms of the GNU General Public License as published by
21+
* the Free Software Foundation, either version 3 of the License, or
22+
* (at your option) any later version.
23+
*
24+
* This program is distributed in the hope that it will be useful,
25+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
26+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27+
* GNU General Public License for more details.
28+
*
29+
* You should have received a copy of the GNU General Public License
30+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
31+
*
32+
* ---------------------------------------------------------------------
33+
*/
34+
35+
namespace Glpi\DBAL;
36+
37+
use InvalidArgumentException;
38+
39+
enum Operator: string
40+
{
41+
case AND = 'AND';
42+
case OR = 'OR';
43+
case NONE = '';
44+
45+
public static function fromString(?string $operator): Operator
46+
{
47+
if ($operator !== null) {
48+
$operator = strtoupper($operator);
49+
}
50+
return match ($operator) {
51+
'AND' => self::AND,
52+
'OR' => self::OR,
53+
null, '' => self::NONE,
54+
default => throw new InvalidArgumentException("Invalid operator: $operator"),
55+
};
56+
}
57+
}

0 commit comments

Comments
 (0)