From 7976e6d5b526d05a9737cbc73eda4dcd9a73fccd Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Tue, 3 Jan 2023 09:56:04 +0200 Subject: [PATCH] cs ref --- .../ORM/QueryComposer/BaseQueryComposer.php | 188 ++++++++---------- 1 file changed, 78 insertions(+), 110 deletions(-) diff --git a/application/Espo/ORM/QueryComposer/BaseQueryComposer.php b/application/Espo/ORM/QueryComposer/BaseQueryComposer.php index f598b24dff..f06821c5c4 100644 --- a/application/Espo/ORM/QueryComposer/BaseQueryComposer.php +++ b/application/Espo/ORM/QueryComposer/BaseQueryComposer.php @@ -89,17 +89,13 @@ abstract class BaseQueryComposer implements QueryComposer 'forShare', ]; - /** - * @var string[] - */ + /** @var string[] */ protected const SQL_OPERATORS = [ 'OR', 'AND', ]; - /** - * @var array - */ + /** @var array */ protected array $comparisonOperators = [ '!=s' => 'NOT IN', '=s' => 'IN', @@ -113,9 +109,7 @@ abstract class BaseQueryComposer implements QueryComposer '=' => '=', ]; - /** - * @var array - */ + /** @var array */ protected array $comparisonFunctionOperatorMap = [ 'LIKE' => 'LIKE', 'NOT_LIKE' => 'NOT LIKE', @@ -131,9 +125,7 @@ abstract class BaseQueryComposer implements QueryComposer 'NOT_IN' => 'NOT IN', ]; - /** - * @var array - */ + /** @var array */ protected array $mathFunctionOperatorMap = [ 'ADD' => '+', 'SUB' => '-', @@ -142,9 +134,7 @@ abstract class BaseQueryComposer implements QueryComposer 'MOD' => '%', ]; - /** - * @var array - */ + /** @var array */ protected array $matchFunctionMap = [ 'MATCH_BOOLEAN' => 'IN BOOLEAN MODE', 'MATCH_NATURAL_LANGUAGE' => 'IN NATURAL LANGUAGE MODE', @@ -152,38 +142,23 @@ abstract class BaseQueryComposer implements QueryComposer ]; protected const SELECT_METHOD = 'SELECT'; - protected const DELETE_METHOD = 'DELETE'; - protected const UPDATE_METHOD = 'UPDATE'; - protected const INSERT_METHOD = 'INSERT'; protected string $identifierQuoteCharacter = '`'; protected EntityFactory $entityFactory; - protected PDO $pdo; - protected Metadata $metadata; - protected ?FunctionConverterFactory $functionConverterFactory; - protected Helper $helper; - /** - * @var array - */ + /** @var array */ protected array $attributeDbMapCache = []; - - /** - * @var array> - */ + /** @var array> */ protected $aliasesCache = []; - - /** - * @var array - */ + /** @var array */ protected $seedCache = []; public function __construct( @@ -289,7 +264,7 @@ abstract class BaseQueryComposer implements QueryComposer /** * @deprecated * @todo Remove in v7.3. - * @param array|null $params + * @param array|null $params */ public function createSelectQuery(string $entityType, ?array $params = null): string { @@ -301,7 +276,7 @@ abstract class BaseQueryComposer implements QueryComposer } /** - * @param array|null $params + * @param array|null $params */ protected function createDeleteQuery(?array $params = null): string { @@ -336,7 +311,7 @@ abstract class BaseQueryComposer implements QueryComposer } /** - * @param array|null $params + * @param array|null $params */ protected function createUpdateQuery(?array $params = null): string { @@ -367,7 +342,7 @@ abstract class BaseQueryComposer implements QueryComposer } /** - * @param array|null $params + * @param array|null $params */ protected function createInsertQuery(?array $params): string { @@ -392,7 +367,7 @@ abstract class BaseQueryComposer implements QueryComposer } /** - * @param array $params + * @param array $params */ protected function getInsertValuesPart(string $entityType, array $params): string { @@ -422,7 +397,7 @@ abstract class BaseQueryComposer implements QueryComposer } /** - * @param array $params + * @param array $params */ protected function createUnionQuery(array $params): string { @@ -495,8 +470,8 @@ abstract class BaseQueryComposer implements QueryComposer } /** - * @param array $params - * @return array + * @param array $params + * @return array */ protected function normalizeInsertParams(array $params): array { @@ -560,8 +535,8 @@ abstract class BaseQueryComposer implements QueryComposer } /** - * @param array|null $params - * @return array + * @param array|null $params + * @return array */ protected function normalizeParams(string $method, ?array $params): array { @@ -601,7 +576,7 @@ abstract class BaseQueryComposer implements QueryComposer } /** - * @param array|null $params + * @param array|null $params */ protected function createSelectQueryInternal(?array $params = null): string { @@ -626,10 +601,8 @@ abstract class BaseQueryComposer implements QueryComposer } $selectPart = null; - $joinsPart = null; $orderPart = null; $havingPart = null; - $groupByPart = null; $tailPart = null; $wherePart = $this->getWherePart($entity, $whereClause, 'AND', $params); @@ -769,7 +742,7 @@ abstract class BaseQueryComposer implements QueryComposer } /** - * @param array $params + * @param array $params */ protected function createSelectQueryNoFrom(array $params): string { @@ -784,7 +757,7 @@ abstract class BaseQueryComposer implements QueryComposer } /** - * @param array $params + * @param array $params * @return string[]|null */ protected function getIndexKeyList(string $entityType, array $params): ?array @@ -812,7 +785,7 @@ abstract class BaseQueryComposer implements QueryComposer } /** - * @param array $params + * @param array $params */ protected function getJoinsPart(Entity $entity, array $params, bool $includeBelongsTo = false): string { @@ -877,7 +850,7 @@ abstract class BaseQueryComposer implements QueryComposer } /** - * @param array $params + * @param array $params */ protected function getGroupByPart(Entity $entity, array $params): ?string { @@ -895,7 +868,7 @@ abstract class BaseQueryComposer implements QueryComposer } /** - * @param array $params + * @param array $params */ protected function getAdditionalSelect(Entity $entity, array $params): ?string { @@ -938,7 +911,7 @@ abstract class BaseQueryComposer implements QueryComposer /** * @param string[] $argumentPartList - * @param array $params + * @param array $params */ protected function getFunctionPart( string $function, @@ -961,8 +934,9 @@ abstract class BaseQueryComposer implements QueryComposer throw new RuntimeException("ORM Query: Not allowed function '{$function}'."); } - if (strpos($function, 'YEAR_') === 0 && $function !== 'YEAR_NUMBER') { + if (str_starts_with($function, 'YEAR_') && $function !== 'YEAR_NUMBER') { $fiscalShift = substr($function, 5); + if (is_numeric($fiscalShift)) { $fiscalShift = (int) $fiscalShift; $fiscalFirstMonth = $fiscalShift + 1; @@ -974,7 +948,7 @@ abstract class BaseQueryComposer implements QueryComposer } } - if (strpos($function, 'QUARTER_') === 0 && $function !== 'QUARTER_NUMBER') { + if (str_starts_with($function, 'QUARTER_') && $function !== 'QUARTER_NUMBER') { $fiscalShift = substr($function, 8); if (is_numeric($fiscalShift)) { @@ -1165,7 +1139,7 @@ abstract class BaseQueryComposer implements QueryComposer $offsetHoursString = $argumentPartList[1]; - if (substr($offsetHoursString, 0, 1) === '\'' && substr($offsetHoursString, -1) === '\'') { + if (str_starts_with($offsetHoursString, '\'') && str_ends_with($offsetHoursString, '\'')) { $offsetHoursString = substr($offsetHoursString, 1, -1); } @@ -1190,7 +1164,7 @@ abstract class BaseQueryComposer implements QueryComposer } /** - * @param array $params + * @param array $params */ protected function convertMatchExpression(Entity $entity, string $expression, array $params): string { @@ -1207,7 +1181,7 @@ abstract class BaseQueryComposer implements QueryComposer throw new RuntimeException("ORM Query: Empty MATCH parameters."); } - if (substr($rest, 0, 1) === '(' && substr($rest, -1) === ')') { + if (str_starts_with($rest, '(') && str_ends_with($rest, ')')) { $rest = substr($rest, 1, -1); $argumentList = Util::parseArgumentListFromFunctionContent($rest); @@ -1217,12 +1191,14 @@ abstract class BaseQueryComposer implements QueryComposer } $columnList = []; + for ($i = 0; $i < count($argumentList) - 1; $i++) { $columnList[] = $argumentList[$i]; } $query = $argumentList[count($argumentList) - 1]; - } else { + } + else { throw new RuntimeException("ORM Query: Bad MATCH usage."); } @@ -1252,7 +1228,7 @@ abstract class BaseQueryComposer implements QueryComposer } /** - * @param array $params + * @param array $params */ protected function convertComplexExpression( ?Entity $entity, @@ -1280,7 +1256,7 @@ abstract class BaseQueryComposer implements QueryComposer $attribute = substr($attribute, $delimiterPosition + 1); - if (substr($attribute, 0, 1) === '(' && substr($attribute, -1) === ')') { + if (str_starts_with($attribute, '(') && str_ends_with($attribute, ')')) { $attribute = substr($attribute, 1, -1); } } @@ -1334,7 +1310,7 @@ abstract class BaseQueryComposer implements QueryComposer } /** - * @param array $params + * @param array $params */ protected function getFunctionArgumentPart( Entity $entity, @@ -1347,11 +1323,11 @@ abstract class BaseQueryComposer implements QueryComposer if (Util::isArgumentString($argument)) { $string = substr($argument, 1, -1); - $string = $this->quote($string); - return $string; + return $this->quote($string); } - else if (Util::isArgumentNumeric($argument)) { + + if (Util::isArgumentNumeric($argument)) { if (filter_var($argument, FILTER_VALIDATE_INT) !== false) { $argument = intval($argument); } @@ -1361,7 +1337,8 @@ abstract class BaseQueryComposer implements QueryComposer return $this->quote($argument); } - else if (Util::isArgumentBoolOrNull($argument)) { + + if (Util::isArgumentBoolOrNull($argument)) { return strtoupper($argument); } @@ -1419,7 +1396,7 @@ abstract class BaseQueryComposer implements QueryComposer } /** - * @param array|null $params + * @param array|null $params */ protected function getFromAlias(?array $params = null, ?string $entityType = null): string { @@ -1445,7 +1422,7 @@ abstract class BaseQueryComposer implements QueryComposer } /** - * @param array|null $params + * @param array|null $params */ protected function getAttributeOrderSql( Entity $entity, @@ -1524,7 +1501,7 @@ abstract class BaseQueryComposer implements QueryComposer } /** - * @param array|null $params + * @param array|null $params */ protected function getAttributeSql( Entity $entity, @@ -1581,8 +1558,8 @@ abstract class BaseQueryComposer implements QueryComposer } /** - * @param array $defs - * @param array $params + * @param array $defs + * @param array $params */ protected function applyAttributeCustomParams( array $defs, @@ -1687,7 +1664,7 @@ abstract class BaseQueryComposer implements QueryComposer } /** - * @param array $params + * @param array $params * @return string[] */ protected function getOrderByAttributeList(array $params): array @@ -1719,7 +1696,7 @@ abstract class BaseQueryComposer implements QueryComposer $expression = $item[0]; - if (strpos($expression, 'LIST:') === 0 && substr_count($expression, ':') === 2) { + if (str_starts_with($expression, 'LIST:') && substr_count($expression, ':') === 2) { $expression = explode(':', $expression)[1]; } @@ -1765,7 +1742,7 @@ abstract class BaseQueryComposer implements QueryComposer } /** - * @param array $params + * @param array $params */ protected function getSelectPart(?Entity $entity, array &$params): string { @@ -1805,11 +1782,7 @@ abstract class BaseQueryComposer implements QueryComposer if (is_string($item)) { if (strpos($item, ':')) { $itemList[$i] = [$item, $item]; - - continue; } - - continue; } } @@ -1848,9 +1821,9 @@ abstract class BaseQueryComposer implements QueryComposer } /** - * @param array $params + * @param array $params * @param string|string[] $attribute - * @return array{string,string}|null + * @return array{string, string}|null */ protected function getSelectPartItemPair(?Entity $entity, array &$params, $attribute): ?array { @@ -2005,7 +1978,7 @@ abstract class BaseQueryComposer implements QueryComposer } /** - * @param array|null $params + * @param array|null $params */ protected function getBelongsToJoinItemPart( Entity $entity, @@ -2043,7 +2016,7 @@ abstract class BaseQueryComposer implements QueryComposer } /** - * @param array $params + * @param array $params */ protected function getSelectTailPart(array $params): ?string { @@ -2064,7 +2037,7 @@ abstract class BaseQueryComposer implements QueryComposer /** * @param string[]|array $select * @param string[] $skipList - * @param array $params + * @param array $params */ protected function getBelongsToJoinsPart(Entity $entity, ?array $select, array $skipList, array $params): string { @@ -2174,7 +2147,7 @@ abstract class BaseQueryComposer implements QueryComposer } /** - * @param array|null $params + * @param array|null $params * @param mixed $orderBy * @param mixed $order */ @@ -2215,7 +2188,7 @@ abstract class BaseQueryComposer implements QueryComposer return implode(", ", $arr); } - if (strpos($orderBy, 'LIST:') === 0) { + if (str_starts_with($orderBy, 'LIST:')) { list($l, $field, $list) = explode(':', $orderBy); $fieldPath = $this->getAttributePathForOrderBy($entity, $field, $params ?? []); @@ -2226,6 +2199,7 @@ abstract class BaseQueryComposer implements QueryComposer foreach ($list as $i => $listItem) { $listItem = str_replace('_COMMA_', ',', $listItem); + $listQuoted[] = $this->quote($listItem); } @@ -2268,7 +2242,7 @@ abstract class BaseQueryComposer implements QueryComposer } /** - * @param array|null $params + * @param array|null $params * @param mixed $orderBy * @param mixed $order */ @@ -2278,7 +2252,7 @@ abstract class BaseQueryComposer implements QueryComposer } /** - * @param array $params + * @param array $params */ protected function getAttributePathForOrderBy(Entity $entity, string $orderBy, array $params): ?string { @@ -2295,7 +2269,7 @@ abstract class BaseQueryComposer implements QueryComposer } /** - * @param array $params + * @param array $params */ protected function getAggregationSelectPart( Entity $entity, @@ -2392,7 +2366,7 @@ abstract class BaseQueryComposer implements QueryComposer } /** - * @return array + * @return array */ protected function getTableAliases(Entity $entity): array { @@ -2428,7 +2402,7 @@ abstract class BaseQueryComposer implements QueryComposer } /** - * @param array $params + * @param array $params */ protected function getAttributePath(Entity $entity, string $attribute, array &$params): ?string { @@ -2499,8 +2473,8 @@ abstract class BaseQueryComposer implements QueryComposer } /** - * @param array $params - * @param array $whereClause + * @param array $params + * @param array $whereClause */ protected function getWherePart( Entity $entity, @@ -2529,7 +2503,7 @@ abstract class BaseQueryComposer implements QueryComposer } /** - * @param array $params + * @param array $params * @param mixed $leftKey * @param mixed $value */ @@ -2933,9 +2907,9 @@ abstract class BaseQueryComposer implements QueryComposer } /** - * @param array $params - * @param array $joinConditions - * @param array $joins + * @param array $params + * @param array $joinConditions + * @param array $joins */ protected function getJoinsTypePart( Entity $entity, @@ -3003,7 +2977,7 @@ abstract class BaseQueryComposer implements QueryComposer } /** - * @param array $params + * @param array $params * @param string $alias * @param mixed $left * @param mixed $right @@ -3140,9 +3114,9 @@ abstract class BaseQueryComposer implements QueryComposer } /** - * @param array $params - * @param array $joinParams - * @param array $conditions + * @param array $params + * @param array $joinParams + * @param array $conditions */ protected function getJoinItemPart( Entity $entity, @@ -3228,12 +3202,6 @@ abstract class BaseQueryComposer implements QueryComposer $this->getRelationParam($entity, $relationName, 'relationName') ); - $midAlias = lcfirst( - $this->sanitize( - $this->getRelationParam($entity, $relationName, 'relationName') - ) - ); - $distantTable = $this->toDb($foreignEntityType); $midAlias = $alias . 'Middle'; @@ -3540,8 +3508,8 @@ abstract class BaseQueryComposer implements QueryComposer } /** - * @param array $values - * @param array $params + * @param array $values + * @param array $params */ protected function getSetPart(Entity $entity, array $values, array $params): string { @@ -3554,7 +3522,7 @@ abstract class BaseQueryComposer implements QueryComposer foreach ($values as $attribute => $value) { $isNotValue = false; - if (substr($attribute, -1) == ':') { + if (str_ends_with($attribute, ':')) { $attribute = substr($attribute, 0, -1); $isNotValue = true; } @@ -3603,7 +3571,7 @@ abstract class BaseQueryComposer implements QueryComposer /** * @param string[] $columnList - * @param array $values + * @param array $values */ protected function getInsertValuesItemPart(array $columnList, array $values): string { @@ -3617,7 +3585,7 @@ abstract class BaseQueryComposer implements QueryComposer } /** - * @param array $values + * @param array $values */ protected function getInsertUpdatePart(array $values): string {