Skip to content

Commit 1323b7f

Browse files
committed
Add readonly where possible
1 parent 5ea9695 commit 1323b7f

34 files changed

+172
-170
lines changed

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,5 @@ This library is heavily inspired by Benjamin Eberlei's [blog post][blog_post]
168168
and [Happyr's Doctrine-Specification library][happyr_spec].
169169

170170
[specification_pattern]: http://en.wikipedia.org/wiki/Specification_pattern
171-
172171
[happyr_spec]: https://github.com/Happyr/Doctrine-Specification
173-
174172
[blog_post]: http://www.whitewashing.de/2013/03/04/doctrine_repositories.html

spec/Condition/NotInSpec.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use Doctrine\ORM\Query\Expr;
77
use Doctrine\ORM\QueryBuilder;
88
use PhpSpec\ObjectBehavior;
9-
use Purist\Specification\Doctrine\Condition\In;
9+
use Purist\Specification\Doctrine\Condition\NotIn;
1010
use Purist\Specification\Doctrine\SpecificationInterface;
1111

1212
class NotInSpec extends ObjectBehavior
@@ -23,7 +23,7 @@ public function let(): void
2323
public function it_is_an_expression(): void
2424
{
2525
$this->shouldBeAnInstanceOf(SpecificationInterface::class);
26-
$this->shouldBeAnInstanceOf(In::class);
26+
$this->shouldBeAnInstanceOf(NotIn::class);
2727
}
2828

2929
public function it_returns_an_expression_func_object(QueryBuilder $queryBuilder, ArrayCollection $parameters, Expr $expr): void

spec/Logic/NotSpec.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class NotSpec extends ObjectBehavior
1111
{
1212
public function let(SpecificationInterface $condition): void
1313
{
14-
$this->beConstructedWith($condition, null);
14+
$this->beConstructedWith($condition);
1515
}
1616

1717
public function it_calls_parent_match(QueryBuilder $queryBuilder, Expr $expr, SpecificationInterface $condition): void
@@ -32,16 +32,16 @@ public function it_calls_parent_match(QueryBuilder $queryBuilder, Expr $expr, Sp
3232
public function it_modifies_parent_query(QueryBuilder $queryBuilder, SpecificationInterface $specification): void
3333
{
3434
$dqlAlias = 'a';
35-
$this->beConstructedWith($specification, null);
35+
$this->beConstructedWith($specification);
3636

37-
$specification->modify($queryBuilder, $dqlAlias)->shouldBeCalled();
37+
$specification->modify($queryBuilder, $dqlAlias)->shouldBeCalled()->willReturn(null);
3838
$this->modify($queryBuilder, $dqlAlias);
3939
}
4040

4141
public function it_should_call_supports_on_parent(SpecificationInterface $specification): void
4242
{
4343
$className = 'foo';
44-
$this->beConstructedWith($specification, null);
44+
$this->beConstructedWith($specification);
4545

4646
$specification->isSatisfiedBy($className)->shouldBeCalled();
4747

spec/Query/HavingSpec.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,33 +38,30 @@ public function it_calls_having_on_query_builder(QueryBuilder $queryBuilder, Spe
3838
{
3939
$condition = 'foo';
4040
$specification->modify($queryBuilder, $this->dqlAlias)->willReturn($condition);
41-
42-
$this->setType(Having::HAVING);
4341
$queryBuilder->having($condition)->shouldBeCalled()->willReturn($queryBuilder);
4442

45-
$this->modify($queryBuilder, $this->dqlAlias);
43+
$having = $this->setType(Having::HAVING);
44+
$having->modify($queryBuilder, $this->dqlAlias);
4645
}
4746

4847
public function it_calls_andHaving_on_query_builder(QueryBuilder $queryBuilder, SpecificationInterface $specification): void
4948
{
5049
$condition = 'foo';
5150
$specification->modify($queryBuilder, $this->dqlAlias)->willReturn($condition);
52-
53-
$this->setType(Having::AND_HAVING);
5451
$queryBuilder->andHaving($condition)->shouldBeCalled()->willReturn($queryBuilder);
5552

56-
$this->modify($queryBuilder, $this->dqlAlias);
53+
$having = $this->setType(Having::AND_HAVING);
54+
$having->modify($queryBuilder, $this->dqlAlias);
5755
}
5856

5957
public function it_calls_orHaving_on_query_builder(QueryBuilder $queryBuilder, SpecificationInterface $specification): void
6058
{
6159
$condition = 'foo';
6260
$specification->modify($queryBuilder, $this->dqlAlias)->willReturn($condition);
63-
64-
$this->setType(Having::OR_HAVING);
6561
$queryBuilder->orHaving($condition)->shouldBeCalled()->willReturn($queryBuilder);
6662

67-
$this->modify($queryBuilder, $this->dqlAlias);
63+
$having = $this->setType(Having::OR_HAVING);
64+
$having->modify($queryBuilder, $this->dqlAlias);
6865
}
6966

7067
public function it_throws_exception_when_setting_illegal_type(): void

spec/Query/JoinSpec.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class JoinSpec extends ObjectBehavior
1313
{
1414
public function let(): void
1515
{
16-
$this->beConstructedWith('user', 'authUser', 'a');
16+
$this->beConstructedWith('user', 'authUser', Join::JOIN, 'a');
1717
}
1818

1919
public function it_is_a_specification(): void
@@ -46,12 +46,11 @@ public function it_should_use_be_able_to_use_join_conditions(QueryBuilder $query
4646

4747
$this->beConstructedWith('user', 'authUser');
4848

49-
$this->setConditionType($joinType)->shouldReturn($this);
50-
$this->setCondition($joinCondition)->shouldReturn($this);
49+
$join = $this->setConditionType($joinType)->setCondition($joinCondition);
5150

5251
$queryBuilder->join('a.user', 'authUser', $joinType, $joinCondition, null)->shouldBeCalled()->willReturn($queryBuilder);
5352

54-
$this->modify($queryBuilder, 'a');
53+
$join->modify($queryBuilder, 'a');
5554
}
5655

5756
public function it_should_be_able_to_set_index_by_for_join(QueryBuilder $queryBuilder): void
@@ -62,9 +61,9 @@ public function it_should_be_able_to_set_index_by_for_join(QueryBuilder $queryBu
6261

6362
$queryBuilder->join('a.user', 'authUser', null, null, $indexedBy)->shouldBeCalled()->willReturn($queryBuilder);
6463

65-
$this->setIndexedBy($indexedBy)->shouldReturn($this);
64+
$join = $this->setIndexedBy($indexedBy);
6665

67-
$this->modify($queryBuilder, 'a');
66+
$join->modify($queryBuilder, 'a');
6867
}
6968

7069
public function it_should_accept_specifications_as_condition(QueryBuilder $queryBuilder, SpecificationInterface $specification): void
@@ -78,9 +77,8 @@ public function it_should_accept_specifications_as_condition(QueryBuilder $query
7877

7978
$queryBuilder->join('a.user', 'authUser', $type, $condition, null)->shouldBeCalled()->willReturn($queryBuilder);
8079

81-
$this->setConditionType($type)->shouldReturn($this);
82-
$this->setCondition($specification)->shouldReturn($this);
83-
$this->modify($queryBuilder, 'a');
80+
$join = $this->setConditionType($type)->setCondition($specification);
81+
$join->modify($queryBuilder, 'a');
8482
}
8583

8684
public function it_throws_an_exception_when_setting_illegal_type(): void

src/AbstractSpecification.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Purist\Specification\Doctrine;
44

5-
abstract class AbstractSpecification implements SpecificationInterface
5+
abstract readonly class AbstractSpecification implements SpecificationInterface
66
{
77
public function __construct(protected string $field, protected ?string $dqlAlias = null)
88
{
@@ -31,7 +31,7 @@ protected function createAliasedName(string $value, ?string $dqlAlias): string
3131
return $value;
3232
}
3333

34-
if (null !== $this->dqlAlias && '' !== $this->dqlAlias && '0' !== $this->dqlAlias) {
34+
if (null !== $this->dqlAlias) {
3535
$dqlAlias = $this->dqlAlias;
3636
}
3737

src/Condition/Between.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Doctrine\ORM\QueryBuilder;
66
use Purist\Specification\Doctrine\AbstractSpecification;
77

8-
class Between extends AbstractSpecification
8+
readonly class Between extends AbstractSpecification
99
{
1010
public function __construct(string $field, protected mixed $from, protected mixed $to, ?string $dqlAlias = null)
1111
{

src/Condition/Comparison.php

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,39 +7,29 @@
77
use Purist\Specification\Doctrine\AbstractSpecification;
88
use Purist\Specification\Doctrine\Exception\InvalidArgumentException;
99

10-
class Comparison extends AbstractSpecification
10+
readonly class Comparison extends AbstractSpecification
1111
{
1212
public const string EQ = '=';
13-
1413
public const string NEQ = '<>';
15-
1614
public const string LT = '<';
17-
1815
public const string LTE = '<=';
19-
2016
public const string GT = '>';
21-
2217
public const string GTE = '>=';
23-
2418
public const string LIKE = 'LIKE';
25-
2619
/**
2720
* @var string[]
2821
*/
29-
protected static array $operators = [self::EQ, self::NEQ, self::LT, self::LTE, self::GT, self::GTE, self::LIKE];
30-
protected string $operator;
22+
protected const array OPERATORS = [self::EQ, self::NEQ, self::LT, self::LTE, self::GT, self::GTE, self::LIKE];
3123

3224
/**
3325
* @throws InvalidArgumentException
3426
*/
35-
public function __construct(string $operator, string $field, protected string $value, ?string $dqlAlias = null)
27+
public function __construct(protected string $operator, string $field, protected string $value, ?string $dqlAlias = null)
3628
{
37-
if (!in_array($operator, self::$operators, true)) {
38-
throw new InvalidArgumentException(sprintf('"%s" is not a valid operator. Valid operators: %s', $operator, implode(', ', self::$operators)));
29+
if (!in_array($operator, self::OPERATORS, true)) {
30+
throw new InvalidArgumentException(sprintf('"%s" is not a valid operator. Valid operators: %s', $operator, implode(', ', self::OPERATORS)));
3931
}
4032

41-
$this->operator = $operator;
42-
4333
parent::__construct($field, $dqlAlias);
4434
}
4535

src/Condition/Equals.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Purist\Specification\Doctrine\Exception\InvalidArgumentException;
66

7-
class Equals extends Comparison
7+
readonly class Equals extends Comparison
88
{
99
/**
1010
* @throws InvalidArgumentException

src/Condition/EqualsProperty.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use Doctrine\ORM\QueryBuilder;
77
use Purist\Specification\Doctrine\Exception\InvalidArgumentException;
88

9-
class EqualsProperty extends Comparison
9+
readonly class EqualsProperty extends Comparison
1010
{
1111
/**
1212
* @throws InvalidArgumentException

0 commit comments

Comments
 (0)