Skip to content

Commit

Permalink
IBX-6937: Changed expected min and max value types to numeric instead…
Browse files Browse the repository at this point in the history
… of int (#308)

* Changed expected min and max value types to numeric instead of int

* [PHPStan] Regenerated baseline

* Fixed typo in validation messages: grater to greater

---------

Co-authored-by: Maciej Kobus <webhdx@users.noreply.github.com>
  • Loading branch information
ciastektk and webhdx authored Jan 5, 2024
1 parent 1f34f08 commit fb6e450
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 87 deletions.
4 changes: 2 additions & 2 deletions phpstan-baseline-7.4.neon
Original file line number Diff line number Diff line change
Expand Up @@ -376,12 +376,12 @@ parameters:
path: src/lib/Search/Common/FieldNameResolver.php

-
message: "#^Parameter \\#1 \\$str of function trim expects string, int\\|string given\\.$#"
message: "#^Parameter \\#1 \\$str of function trim expects string, bool\\|float\\|int\\|string given\\.$#"
count: 1
path: src/lib/Search/Legacy/Content/Gateway/CriterionHandler/Ancestor.php

-
message: "#^Parameter \\#1 \\$str of function trim expects string, int\\|string given\\.$#"
message: "#^Parameter \\#1 \\$str of function trim expects string, bool\\|float\\|int\\|string given\\.$#"
count: 1
path: src/lib/Search/Legacy/Content/Location/Gateway/CriterionHandler/Ancestor.php

Expand Down
4 changes: 2 additions & 2 deletions phpstan-baseline-8.0.neon
Original file line number Diff line number Diff line change
Expand Up @@ -311,12 +311,12 @@ parameters:
path: src/lib/Search/Common/FieldNameResolver.php

-
message: "#^Parameter \\#1 \\$string of function trim expects string, int\\|string given\\.$#"
message: "#^Parameter \\#1 \\$string of function trim expects string, bool\\|float\\|int\\|string given\\.$#"
count: 1
path: src/lib/Search/Legacy/Content/Gateway/CriterionHandler/Ancestor.php

-
message: "#^Parameter \\#1 \\$string of function trim expects string, int\\|string given\\.$#"
message: "#^Parameter \\#1 \\$string of function trim expects string, bool\\|float\\|int\\|string given\\.$#"
count: 1
path: src/lib/Search/Legacy/Content/Location/Gateway/CriterionHandler/Ancestor.php

Expand Down
113 changes: 51 additions & 62 deletions phpstan-baseline.neon

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/contracts/Repository/Values/Content/Query/Criterion.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ abstract class Criterion implements CriterionInterface
/**
* The value(s) matched by the criteria.
*
* @var string[]|int[]|int|string|bool
* @var scalar[]|scalar
*/
public $value;

Expand All @@ -50,7 +50,7 @@ abstract class Criterion implements CriterionInterface
* @param string|null $operator
* The operator the Criterion uses. If null is given, will default to Operator::IN if $value is an array,
* Operator::EQ if it is not.
* @param string[]|int[]|int|string|bool $value
* @param scalar[]|scalar $value
* @param \Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\Value|null $valueData
*
* @todo Add a dedicated exception
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
* @template TImageCriteria of array
*
* @phpstan-type Range array{
* min?: int|null,
* max?: int|null,
* min?: numeric|null,
* max?: numeric|null,
* }
*/
abstract class AbstractImageCompositeCriterion extends CompositeCriterion
Expand Down Expand Up @@ -91,17 +91,21 @@ protected function validate(
}

/**
* @param array{min?: int|null} $data
* @param array{min?: numeric|null} $data
*
* @return numeric
*/
protected function getMinValue(array $data): int
protected function getMinValue(array $data)
{
return $data['min'] ?? 0;
}

/**
* @param array{max?: int|null} $data
* @param array{max?: numeric|null} $data
*
* @return numeric|null
*/
protected function getMaxValue(array $data): ?int
protected function getMaxValue(array $data)
{
return $data['max'] ?? null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,22 @@
abstract class AbstractImageRangeCriterion extends Criterion
{
/**
* @param numeric $minValue
* @param numeric|null $maxValue
*
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException
*/
public function __construct(
string $fieldDefIdentifier,
int $minValue = 0,
?int $maxValue = null
$minValue = 0,
$maxValue = null
) {
$this->validate($minValue, $maxValue);

$value[] = $minValue;
$operator = Operator::GTE;

if ($maxValue >= 1) {
if ($maxValue > 0) {
$operator = Operator::BETWEEN;
$value[] = $maxValue;
}
Expand Down Expand Up @@ -57,26 +60,27 @@ public function getSpecifications(): array
}

/**
* @param numeric $minValue
* @param numeric|null $maxValue
*
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException
*/
protected function validate(
int $minValue,
?int $maxValue
): void {
protected function validate($minValue, $maxValue): void
{
if ($minValue < 0) {
throw new InvalidArgumentException(
'$minValue',
'Value should be grater or equal 0'
'Value should be greater or equal 0'
);
}

if (
null !== $maxValue
&& $maxValue < 1
&& $maxValue <= 0
) {
throw new InvalidArgumentException(
'$maxValue',
'Value should be grater or equal 1'
'Value should be greater than 0'
);
}

Expand All @@ -86,7 +90,7 @@ protected function validate(
) {
throw new InvalidArgumentException(
'$minValue',
'Value should be grater than' . $maxValue
'Value should be greater than' . $maxValue
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@
final class FileSize extends AbstractImageRangeCriterion
{
/**
* @param numeric $minFileSize
* @param numeric|null $maxFileSize
*
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException
*/
public function __construct(
string $fieldDefIdentifier,
int $minFileSize = 0,
?int $maxFileSize = null
$minFileSize = 0,
$maxFileSize = null
) {
if ($minFileSize > 0) {
$minFileSize *= 1024 * 1024;
Expand Down

0 comments on commit fb6e450

Please sign in to comment.