Skip to content

Commit

Permalink
cleanup, php 8 touch-up in collections namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
hrach committed Jul 22, 2023
1 parent 08f7f48 commit 9b42e74
Show file tree
Hide file tree
Showing 25 changed files with 93 additions and 81 deletions.
2 changes: 1 addition & 1 deletion src/Collection/Aggregations/AnyAggregator.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function aggregateExpression(
toExpression: $join->toExpression,
toArgs: $join->toArgs,
toAlias: $join->toAlias,
onExpression: "($join->onExpression) AND $expression->expression",
onExpression: "($join->onExpression) AND $expression->expression",
onArgs: array_merge($join->onArgs, $expression->args),
conventions: $join->conventions,
);
Expand Down
4 changes: 2 additions & 2 deletions src/Collection/DbalCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ public function getQueryBuilder(): QueryBuilder
$expression = $helper->processExpression(
$this->queryBuilder,
$args,
null
null,
);
$joins = $expression->joins;
if ($expression->isHavingClause) {
Expand Down Expand Up @@ -355,7 +355,7 @@ public function getQueryBuilder(): QueryBuilder
if (count($groupBy) > 0) {
$this->queryBuilder->groupBy(
'%ex' . str_repeat(', %ex', count($groupBy) - 1),
...$groupBy
...$groupBy,
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Collection/Functions/AvgAggregateFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function __construct()
return array_sum($values) / $count;
},
dbalAggregationFunction: 'AVG',
)
),
);
}
}
29 changes: 12 additions & 17 deletions src/Collection/Functions/BaseCompareFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function processArrayExpression(
ArrayCollectionHelper $helper,
IEntity $entity,
array $args,
?IArrayAggregator $aggregator = null
?IArrayAggregator $aggregator = null,
): ArrayExpressionResult
{
assert(count($args) === 2);
Expand All @@ -38,18 +38,18 @@ public function processArrayExpression(
function ($value) use ($targetValue): bool {
return $this->evaluateInPhp($value, $targetValue);
},
$valueReference->value
$valueReference->value,
);
return new ArrayExpressionResult(
$values,
$valueReference->aggregator,
null
value: $values,
aggregator: $valueReference->aggregator,
propertyMetadata: null,
);
} else {
return new ArrayExpressionResult(
$this->evaluateInPhp($valueReference->value, $targetValue),
null,
null
value: $this->evaluateInPhp($valueReference->value, $targetValue),
aggregator: null,
propertyMetadata: null,
);
}
}
Expand All @@ -59,7 +59,7 @@ public function processDbalExpression(
DbalQueryBuilderHelper $helper,
QueryBuilder $builder,
array $args,
?IDbalAggregator $aggregator = null
?IDbalAggregator $aggregator = null,
): DbalExpressionResult
{
assert(count($args) === 2);
Expand All @@ -77,20 +77,15 @@ public function processDbalExpression(
}


/**
* @param mixed $sourceValue
* @param mixed $targetValue
*/
abstract protected function evaluateInPhp($sourceValue, $targetValue): bool;
abstract protected function evaluateInPhp(mixed $sourceValue, mixed $targetValue): bool;


/**
* @param mixed $value
* @phpstan-param literal-string $modifier
*/
abstract protected function evaluateInDb(
DbalExpressionResult $expression,
$value,
string $modifier
mixed $value,
string $modifier,
): DbalExpressionResult;
}
4 changes: 2 additions & 2 deletions src/Collection/Functions/BaseNumericAggregateFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function processArrayExpression(
ArrayCollectionHelper $helper,
IEntity $entity,
array $args,
?IArrayAggregator $aggregator = null
?IArrayAggregator $aggregator = null,
): ArrayExpressionResult
{
assert(count($args) === 1 && is_string($args[0]));
Expand All @@ -54,7 +54,7 @@ public function processDbalExpression(
DbalQueryBuilderHelper $helper,
QueryBuilder $builder,
array $args,
?IDbalAggregator $aggregator = null
?IDbalAggregator $aggregator = null,
): DbalExpressionResult
{
assert(count($args) === 1 && is_string($args[0]));
Expand Down
5 changes: 3 additions & 2 deletions src/Collection/Functions/CollectionFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ public function processArrayExpression(
ArrayCollectionHelper $helper,
IEntity $entity,
array $args,
?IArrayAggregator $aggregator = null
?IArrayAggregator $aggregator = null,
): ArrayExpressionResult;


/**
* Returns true if entity should stay in the result collection; the condition is evaluated in database and this
* method just returns appropriate Nextras Dbal's filtering expression for passed args.
Expand All @@ -44,6 +45,6 @@ public function processDbalExpression(
DbalQueryBuilderHelper $helper,
QueryBuilder $builder,
array $args,
?IDbalAggregator $aggregator = null
?IDbalAggregator $aggregator = null,
): DbalExpressionResult;
}
8 changes: 6 additions & 2 deletions src/Collection/Functions/CompareEqualsFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

class CompareEqualsFunction extends BaseCompareFunction
{
protected function evaluateInPhp($sourceValue, $targetValue): bool
protected function evaluateInPhp(mixed $sourceValue, mixed $targetValue): bool
{
if (is_array($targetValue)) {
return in_array($sourceValue, $targetValue, true);
Expand All @@ -25,7 +25,11 @@ protected function evaluateInPhp($sourceValue, $targetValue): bool
}


protected function evaluateInDb(DbalExpressionResult $expression, $value, string $modifier): DbalExpressionResult
protected function evaluateInDb(
DbalExpressionResult $expression,
mixed $value,
string $modifier,
): DbalExpressionResult
{
if (is_array($value)) {
if (count($value) > 0) {
Expand Down
8 changes: 6 additions & 2 deletions src/Collection/Functions/CompareGreaterThanEqualsFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@

class CompareGreaterThanEqualsFunction extends BaseCompareFunction
{
protected function evaluateInPhp($sourceValue, $targetValue): bool
protected function evaluateInPhp(mixed $sourceValue, mixed $targetValue): bool
{
return $sourceValue >= $targetValue;
}


protected function evaluateInDb(DbalExpressionResult $expression, $value, string $modifier): DbalExpressionResult
protected function evaluateInDb(
DbalExpressionResult $expression,
mixed $value,
string $modifier,
): DbalExpressionResult
{
return $expression->append(">= $modifier", $value);
}
Expand Down
8 changes: 6 additions & 2 deletions src/Collection/Functions/CompareGreaterThanFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@

class CompareGreaterThanFunction extends BaseCompareFunction
{
protected function evaluateInPhp($sourceValue, $targetValue): bool
protected function evaluateInPhp(mixed $sourceValue, mixed $targetValue): bool
{
return $sourceValue > $targetValue;
}


protected function evaluateInDb(DbalExpressionResult $expression, $value, string $modifier): DbalExpressionResult
protected function evaluateInDb(
DbalExpressionResult $expression,
mixed $value,
string $modifier,
): DbalExpressionResult
{
return $expression->append("> $modifier", $value);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Collection/Functions/CompareLikeFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function processArrayExpression(
ArrayCollectionHelper $helper,
IEntity $entity,
array $args,
?IArrayAggregator $aggregator = null
?IArrayAggregator $aggregator = null,
): ArrayExpressionResult
{
assert(count($args) === 2);
Expand All @@ -46,7 +46,7 @@ public function processArrayExpression(
function ($value) use ($mode, $targetValue): bool {
return $this->evaluateInPhp($mode, $value, $targetValue);
},
$valueReference->value
$valueReference->value,
);
return new ArrayExpressionResult(
value: $values,
Expand All @@ -64,7 +64,7 @@ public function processDbalExpression(
DbalQueryBuilderHelper $helper,
QueryBuilder $builder,
array $args,
?IDbalAggregator $aggregator = null
?IDbalAggregator $aggregator = null,
): DbalExpressionResult
{
assert(count($args) === 2);
Expand Down
8 changes: 6 additions & 2 deletions src/Collection/Functions/CompareNotEqualsFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

class CompareNotEqualsFunction extends BaseCompareFunction
{
protected function evaluateInPhp($sourceValue, $targetValue): bool
protected function evaluateInPhp(mixed $sourceValue, mixed $targetValue): bool
{
if (is_array($targetValue)) {
return !in_array($sourceValue, $targetValue, true);
Expand All @@ -25,7 +25,11 @@ protected function evaluateInPhp($sourceValue, $targetValue): bool
}


protected function evaluateInDb(DbalExpressionResult $expression, $value, string $modifier): DbalExpressionResult
protected function evaluateInDb(
DbalExpressionResult $expression,
mixed $value,
string $modifier,
): DbalExpressionResult
{
if (is_array($value)) {
if (count($value) > 0) {
Expand Down
8 changes: 6 additions & 2 deletions src/Collection/Functions/CompareSmallerThanEqualsFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@

class CompareSmallerThanEqualsFunction extends BaseCompareFunction
{
protected function evaluateInPhp($sourceValue, $targetValue): bool
protected function evaluateInPhp(mixed $sourceValue, mixed $targetValue): bool
{
return $sourceValue <= $targetValue;
}


protected function evaluateInDb(DbalExpressionResult $expression, $value, string $modifier): DbalExpressionResult
protected function evaluateInDb(
DbalExpressionResult $expression,
mixed $value,
string $modifier,
): DbalExpressionResult
{
return $expression->append("<= $modifier", $value);
}
Expand Down
8 changes: 6 additions & 2 deletions src/Collection/Functions/CompareSmallerThanFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@

class CompareSmallerThanFunction extends BaseCompareFunction
{
protected function evaluateInPhp($sourceValue, $targetValue): bool
protected function evaluateInPhp(mixed $sourceValue, mixed $targetValue): bool
{
return $sourceValue < $targetValue;
}


protected function evaluateInDb(DbalExpressionResult $expression, $value, string $modifier): DbalExpressionResult
protected function evaluateInDb(
DbalExpressionResult $expression,
mixed $value,
string $modifier,
): DbalExpressionResult
{
return $expression->append("< $modifier", $value);
}
Expand Down
23 changes: 10 additions & 13 deletions src/Collection/Functions/ConjunctionOperatorFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,18 @@ class ConjunctionOperatorFunction implements CollectionFunction
use JunctionFunctionTrait;


/** @var ConditionParser */
protected $conditionParser;


public function __construct(ConditionParser $conditionParserHelper)
public function __construct(
private readonly ConditionParser $conditionParser,
)
{
$this->conditionParser = $conditionParserHelper;
}


public function processArrayExpression(
ArrayCollectionHelper $helper,
IEntity $entity,
array $args,
?IArrayAggregator $aggregator = null
?IArrayAggregator $aggregator = null,
): ArrayExpressionResult
{
[$normalized, $newAggregator] = $this->normalizeFunctions($args);
Expand Down Expand Up @@ -108,15 +105,15 @@ public function processDbalExpression(
DbalQueryBuilderHelper $helper,
QueryBuilder $builder,
array $args,
?IDbalAggregator $aggregator = null
?IDbalAggregator $aggregator = null,
): DbalExpressionResult
{
return $this->processQueryBuilderExpressionWithModifier(
'%and',
$helper,
$builder,
$args,
$aggregator
dbalModifier: '%and',
helper: $helper,
builder: $builder,
args: $args,
aggregator: $aggregator,
);
}
}
2 changes: 1 addition & 1 deletion src/Collection/Functions/CountAggregateFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function __construct()
new NumericAggregator(
arrayAggregation: static fn (array $values): int => count($values),
dbalAggregationFunction: 'COUNT',
)
),
);
}
}
23 changes: 10 additions & 13 deletions src/Collection/Functions/DisjunctionOperatorFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,18 @@ class DisjunctionOperatorFunction implements CollectionFunction
use JunctionFunctionTrait;


/** @var ConditionParser */
private $conditionParser;


public function __construct(ConditionParser $conditionParserHelper)
public function __construct(
private readonly ConditionParser $conditionParser,
)
{
$this->conditionParser = $conditionParserHelper;
}


public function processArrayExpression(
ArrayCollectionHelper $helper,
IEntity $entity,
array $args,
?IArrayAggregator $aggregator = null
?IArrayAggregator $aggregator = null,
): ArrayExpressionResult
{
[$normalized, $newAggregator] = $this->normalizeFunctions($args);
Expand Down Expand Up @@ -101,15 +98,15 @@ public function processDbalExpression(
DbalQueryBuilderHelper $helper,
QueryBuilder $builder,
array $args,
?IDbalAggregator $aggregator = null
?IDbalAggregator $aggregator = null,
): DbalExpressionResult
{
return $this->processQueryBuilderExpressionWithModifier(
'%or',
$helper,
$builder,
$args,
$aggregator
dbalModifier: '%or',
helper: $helper,
builder: $builder,
args: $args,
aggregator: $aggregator,
);
}
}
Loading

0 comments on commit 9b42e74

Please sign in to comment.