Skip to content

Commit 2cbf418

Browse files
Clean code Conditions classes. (#556)
1 parent b204de3 commit 2cbf418

11 files changed

Lines changed: 91 additions & 12 deletions

src/QueryBuilder/Condition/AbstractConjunctionCondition.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
use Yiisoft\Db\QueryBuilder\Condition\Interface\ConjunctionConditionInterface;
88

9+
/**
10+
* Class AbstractConjunctionCondition represents a conjunction condition.
11+
*/
912
abstract class AbstractConjunctionCondition implements ConjunctionConditionInterface
1013
{
1114
final public function __construct(protected array $expressions)

src/QueryBuilder/Condition/AndCondition.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010
final class AndCondition extends AbstractConjunctionCondition
1111
{
1212
/**
13-
* Returns the operator that is represented by this condition class, e.g. `AND`, `OR`.
14-
*
15-
* @psalm-return 'AND'
13+
* @return string The operator that is represented by this condition class, e.g. `AND`, `OR`.
1614
*/
1715
public function getOperator(): string
1816
{

src/QueryBuilder/Condition/BetweenColumnsCondition.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
use Yiisoft\Db\Expression\ExpressionInterface;
1010
use Yiisoft\Db\QueryBuilder\Condition\Interface\BetweenColumnsConditionInterface;
1111

12+
use function is_array;
13+
use function is_int;
14+
use function is_string;
15+
1216
/**
1317
* Class BetweenColumnCondition represents a `BETWEEN` condition where values are between two columns.
1418
*
@@ -65,7 +69,9 @@ public function getValue(): array|int|string|Iterator|ExpressionInterface
6569
}
6670

6771
/**
68-
* @throws InvalidArgumentException
72+
* Creates a condition based on the given operator and operands.
73+
*
74+
* @throws InvalidArgumentException If the number of operands is not 3.
6975
*/
7076
public static function fromArrayDefinition(string $operator, array $operands): self
7177
{
@@ -81,6 +87,11 @@ public static function fromArrayDefinition(string $operator, array $operands): s
8187
);
8288
}
8389

90+
/**
91+
* Validates the given value to be arrayed, int, string, Iterator or ExpressionInterface.
92+
*
93+
* @throws InvalidArgumentException If the value is not arrayed, int, string, Iterator or ExpressionInterface.
94+
*/
8495
private static function validateValue(
8596
string $operator,
8697
mixed $value
@@ -100,6 +111,11 @@ private static function validateValue(
100111
);
101112
}
102113

114+
/**
115+
* Validates the given interval start column to be string or ExpressionInterface.
116+
*
117+
* @throws InvalidArgumentException If the interval start column is not string or ExpressionInterface.
118+
*/
103119
private static function validateIntervalStartColumn(
104120
string $operator,
105121
mixed $intervalStartColumn
@@ -116,6 +132,11 @@ private static function validateIntervalStartColumn(
116132
);
117133
}
118134

135+
/**
136+
* Validates the given interval end column to be string or ExpressionInterface.
137+
*
138+
* @throws InvalidArgumentException If the interval end column is not string or ExpressionInterface.
139+
*/
119140
private static function validateIntervalEndColumn(
120141
string $operator,
121142
mixed $intervalEndColumn

src/QueryBuilder/Condition/BetweenCondition.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ public function getOperator(): string
4242
}
4343

4444
/**
45-
* @throws InvalidArgumentException
45+
* Creates a condition based on the given operator and operands.
46+
*
47+
* @throws InvalidArgumentException If the number of operands is not 3.
4648
*/
4749
public static function fromArrayDefinition(string $operator, array $operands): self
4850
{
@@ -53,6 +55,11 @@ public static function fromArrayDefinition(string $operator, array $operands): s
5355
return new self(self::validateColumn($operator, $operands[0]), $operator, $operands[1], $operands[2]);
5456
}
5557

58+
/**
59+
* Validates the given column to be string or ExpressionInterface.
60+
*
61+
* @throws InvalidArgumentException If the column is not string or ExpressionInterface.
62+
*/
5663
private static function validateColumn(string $operator, mixed $column): string|ExpressionInterface
5764
{
5865
if (is_string($column) || $column instanceof ExpressionInterface) {

src/QueryBuilder/Condition/ExistsCondition.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ public function getQuery(): QueryInterface
2828
}
2929

3030
/**
31-
* @throws InvalidArgumentException
31+
* Creates a condition based on the given operator and operands.
32+
*
33+
* @throws InvalidArgumentException If the number of operands is not 1 and the first operand is not a Query object.
3234
*/
3335
public static function fromArrayDefinition(string $operator, array $operands): self
3436
{

src/QueryBuilder/Condition/HashCondition.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ public function getHash(): array|null
2020
return $this->hash;
2121
}
2222

23+
/**
24+
* Creates a condition based on the given operator and operands.
25+
*/
2326
public static function fromArrayDefinition(string $operator, array $operands): self
2427
{
2528
return new self($operands);

src/QueryBuilder/Condition/InCondition.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ public function getValues(): int|iterable|Iterator|QueryInterface
3838
}
3939

4040
/**
41-
* @throws InvalidArgumentException
41+
* Creates a condition based on the given operator and operands.
42+
*
43+
* @throws InvalidArgumentException If the number of operands is not 2.
4244
*/
4345
public static function fromArrayDefinition(string $operator, array $operands): self
4446
{
@@ -53,6 +55,11 @@ public static function fromArrayDefinition(string $operator, array $operands): s
5355
);
5456
}
5557

58+
/**
59+
* Validates the given column to be string, array or ExpressionInterface.
60+
*
61+
* @throws InvalidArgumentException If the column is not string, array or ExpressionInterface.
62+
*/
5663
private static function validateColumn(string $operator, mixed $column): array|string|Iterator|ExpressionInterface
5764
{
5865
if (is_string($column) || is_array($column) || $column instanceof Iterator || $column instanceof ExpressionInterface) {
@@ -62,6 +69,11 @@ private static function validateColumn(string $operator, mixed $column): array|s
6269
throw new InvalidArgumentException("Operator '$operator' requires column to be string, array or Iterator.");
6370
}
6471

72+
/**
73+
* Validates the given values to be arrayed, Iterator, int or QueryInterface.
74+
*
75+
* @throws InvalidArgumentException If the values is not array, Iterator, int or QueryInterface.
76+
*/
6577
private static function validateValues(string $operator, mixed $values): int|iterable|Iterator|QueryInterface
6678
{
6779
if (is_array($values) || $values instanceof Iterator || is_int($values) || $values instanceof QueryInterface) {

src/QueryBuilder/Condition/LikeCondition.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
use Yiisoft\Db\Expression\ExpressionInterface;
1010
use Yiisoft\Db\QueryBuilder\Condition\Interface\LikeConditionInterface;
1111

12+
use function array_key_exists;
13+
use function is_array;
14+
use function is_int;
15+
use function is_string;
16+
1217
/**
1318
* Class LikeCondition represents a `LIKE` condition.
1419
*/
@@ -49,7 +54,9 @@ public function setEscapingReplacements(array|null $escapingReplacements): void
4954
}
5055

5156
/**
52-
* @throws InvalidArgumentException
57+
* Creates a condition based on the given operator and operands.
58+
*
59+
* @throws InvalidArgumentException If the number of operands is not 2.
5360
*/
5461
public static function fromArrayDefinition(string $operator, array $operands): self
5562
{
@@ -70,6 +77,11 @@ public static function fromArrayDefinition(string $operator, array $operands): s
7077
return $condition;
7178
}
7279

80+
/**
81+
* Validates the given column to be string or ExpressionInterface.
82+
*
83+
* @throws InvalidArgumentException
84+
*/
7385
private static function validateColumn(string $operator, mixed $column): string|ExpressionInterface
7486
{
7587
if (is_string($column) || $column instanceof ExpressionInterface) {
@@ -79,6 +91,11 @@ private static function validateColumn(string $operator, mixed $column): string|
7991
throw new InvalidArgumentException("Operator '$operator' requires column to be string or ExpressionInterface.");
8092
}
8193

94+
/**
95+
* Validates the given values to be string, array, Iterator or ExpressionInterface.
96+
*
97+
* @throws InvalidArgumentException If the values is not string, array, Iterator or ExpressionInterface.
98+
*/
8299
private static function validateValue(
83100
string $operator,
84101
mixed $value

src/QueryBuilder/Condition/NotCondition.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
use function array_shift;
1212
use function count;
13+
use function is_array;
14+
use function is_string;
1315

1416
/**
1517
* Condition that inverts passed {@see condition}.
@@ -26,20 +28,27 @@ public function getCondition(): ExpressionInterface|array|null|string
2628
}
2729

2830
/**
29-
* @throws InvalidArgumentException
31+
* Creates a condition based on the given operator and operands.
32+
*
33+
* @throws InvalidArgumentException If the number of operands is not 1.
3034
*/
3135
public static function fromArrayDefinition(string $operator, array $operands): self
3236
{
3337
return new self(self::validateCondition($operator, $operands));
3438
}
3539

40+
/**
41+
* Validate the given condition have at least 1 condition and to be arrayed, string, null or ExpressionInterface.
42+
*
43+
* @throws InvalidArgumentException If the number of operands is not 1.
44+
*/
3645
private static function validateCondition(string $operator, array $condition): ExpressionInterface|array|null|string
3746
{
3847
if (count($condition) !== 1) {
3948
throw new InvalidArgumentException("Operator '$operator' requires exactly one operand.");
4049
}
4150

42-
/** @var mixed $firstValue */
51+
/** @psalm-var mixed $firstValue */
4352
$firstValue = array_shift($condition);
4453

4554
if (

src/QueryBuilder/Condition/OrCondition.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
final class OrCondition extends AbstractConjunctionCondition
1111
{
1212
/**
13-
* Returns the operator that is represented by this condition class, e.g. `AND`, `OR`.
13+
* @return string The operator that is represented by this condition class, e.g. `AND`, `OR`.
1414
*/
1515
public function getOperator(): string
1616
{

0 commit comments

Comments
 (0)