Skip to content

Commit

Permalink
Add readonly where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholasruunu committed Jul 2, 2024
1 parent 5ea9695 commit 1323b7f
Show file tree
Hide file tree
Showing 34 changed files with 172 additions and 170 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,5 @@ This library is heavily inspired by Benjamin Eberlei's [blog post][blog_post]
and [Happyr's Doctrine-Specification library][happyr_spec].

[specification_pattern]: http://en.wikipedia.org/wiki/Specification_pattern

[happyr_spec]: https://github.com/Happyr/Doctrine-Specification

[blog_post]: http://www.whitewashing.de/2013/03/04/doctrine_repositories.html
4 changes: 2 additions & 2 deletions spec/Condition/NotInSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Doctrine\ORM\Query\Expr;
use Doctrine\ORM\QueryBuilder;
use PhpSpec\ObjectBehavior;
use Purist\Specification\Doctrine\Condition\In;
use Purist\Specification\Doctrine\Condition\NotIn;
use Purist\Specification\Doctrine\SpecificationInterface;

class NotInSpec extends ObjectBehavior
Expand All @@ -23,7 +23,7 @@ public function let(): void
public function it_is_an_expression(): void
{
$this->shouldBeAnInstanceOf(SpecificationInterface::class);
$this->shouldBeAnInstanceOf(In::class);
$this->shouldBeAnInstanceOf(NotIn::class);
}

public function it_returns_an_expression_func_object(QueryBuilder $queryBuilder, ArrayCollection $parameters, Expr $expr): void
Expand Down
8 changes: 4 additions & 4 deletions spec/Logic/NotSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class NotSpec extends ObjectBehavior
{
public function let(SpecificationInterface $condition): void
{
$this->beConstructedWith($condition, null);
$this->beConstructedWith($condition);
}

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

$specification->modify($queryBuilder, $dqlAlias)->shouldBeCalled();
$specification->modify($queryBuilder, $dqlAlias)->shouldBeCalled()->willReturn(null);
$this->modify($queryBuilder, $dqlAlias);
}

public function it_should_call_supports_on_parent(SpecificationInterface $specification): void
{
$className = 'foo';
$this->beConstructedWith($specification, null);
$this->beConstructedWith($specification);

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

Expand Down
15 changes: 6 additions & 9 deletions spec/Query/HavingSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,33 +38,30 @@ public function it_calls_having_on_query_builder(QueryBuilder $queryBuilder, Spe
{
$condition = 'foo';
$specification->modify($queryBuilder, $this->dqlAlias)->willReturn($condition);

$this->setType(Having::HAVING);
$queryBuilder->having($condition)->shouldBeCalled()->willReturn($queryBuilder);

$this->modify($queryBuilder, $this->dqlAlias);
$having = $this->setType(Having::HAVING);
$having->modify($queryBuilder, $this->dqlAlias);
}

public function it_calls_andHaving_on_query_builder(QueryBuilder $queryBuilder, SpecificationInterface $specification): void
{
$condition = 'foo';
$specification->modify($queryBuilder, $this->dqlAlias)->willReturn($condition);

$this->setType(Having::AND_HAVING);
$queryBuilder->andHaving($condition)->shouldBeCalled()->willReturn($queryBuilder);

$this->modify($queryBuilder, $this->dqlAlias);
$having = $this->setType(Having::AND_HAVING);
$having->modify($queryBuilder, $this->dqlAlias);
}

public function it_calls_orHaving_on_query_builder(QueryBuilder $queryBuilder, SpecificationInterface $specification): void
{
$condition = 'foo';
$specification->modify($queryBuilder, $this->dqlAlias)->willReturn($condition);

$this->setType(Having::OR_HAVING);
$queryBuilder->orHaving($condition)->shouldBeCalled()->willReturn($queryBuilder);

$this->modify($queryBuilder, $this->dqlAlias);
$having = $this->setType(Having::OR_HAVING);
$having->modify($queryBuilder, $this->dqlAlias);
}

public function it_throws_exception_when_setting_illegal_type(): void
Expand Down
16 changes: 7 additions & 9 deletions spec/Query/JoinSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class JoinSpec extends ObjectBehavior
{
public function let(): void
{
$this->beConstructedWith('user', 'authUser', 'a');
$this->beConstructedWith('user', 'authUser', Join::JOIN, 'a');
}

public function it_is_a_specification(): void
Expand Down Expand Up @@ -46,12 +46,11 @@ public function it_should_use_be_able_to_use_join_conditions(QueryBuilder $query

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

$this->setConditionType($joinType)->shouldReturn($this);
$this->setCondition($joinCondition)->shouldReturn($this);
$join = $this->setConditionType($joinType)->setCondition($joinCondition);

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

$this->modify($queryBuilder, 'a');
$join->modify($queryBuilder, 'a');
}

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

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

$this->setIndexedBy($indexedBy)->shouldReturn($this);
$join = $this->setIndexedBy($indexedBy);

$this->modify($queryBuilder, 'a');
$join->modify($queryBuilder, 'a');
}

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

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

$this->setConditionType($type)->shouldReturn($this);
$this->setCondition($specification)->shouldReturn($this);
$this->modify($queryBuilder, 'a');
$join = $this->setConditionType($type)->setCondition($specification);
$join->modify($queryBuilder, 'a');
}

public function it_throws_an_exception_when_setting_illegal_type(): void
Expand Down
4 changes: 2 additions & 2 deletions src/AbstractSpecification.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Purist\Specification\Doctrine;

abstract class AbstractSpecification implements SpecificationInterface
abstract readonly class AbstractSpecification implements SpecificationInterface
{
public function __construct(protected string $field, protected ?string $dqlAlias = null)
{
Expand Down Expand Up @@ -31,7 +31,7 @@ protected function createAliasedName(string $value, ?string $dqlAlias): string
return $value;
}

if (null !== $this->dqlAlias && '' !== $this->dqlAlias && '0' !== $this->dqlAlias) {
if (null !== $this->dqlAlias) {
$dqlAlias = $this->dqlAlias;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Condition/Between.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Doctrine\ORM\QueryBuilder;
use Purist\Specification\Doctrine\AbstractSpecification;

class Between extends AbstractSpecification
readonly class Between extends AbstractSpecification
{
public function __construct(string $field, protected mixed $from, protected mixed $to, ?string $dqlAlias = null)
{
Expand Down
20 changes: 5 additions & 15 deletions src/Condition/Comparison.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,29 @@
use Purist\Specification\Doctrine\AbstractSpecification;
use Purist\Specification\Doctrine\Exception\InvalidArgumentException;

class Comparison extends AbstractSpecification
readonly class Comparison extends AbstractSpecification
{
public const string EQ = '=';

public const string NEQ = '<>';

public const string LT = '<';

public const string LTE = '<=';

public const string GT = '>';

public const string GTE = '>=';

public const string LIKE = 'LIKE';

/**
* @var string[]
*/
protected static array $operators = [self::EQ, self::NEQ, self::LT, self::LTE, self::GT, self::GTE, self::LIKE];
protected string $operator;
protected const array OPERATORS = [self::EQ, self::NEQ, self::LT, self::LTE, self::GT, self::GTE, self::LIKE];

/**
* @throws InvalidArgumentException
*/
public function __construct(string $operator, string $field, protected string $value, ?string $dqlAlias = null)
public function __construct(protected string $operator, string $field, protected string $value, ?string $dqlAlias = null)
{
if (!in_array($operator, self::$operators, true)) {
throw new InvalidArgumentException(sprintf('"%s" is not a valid operator. Valid operators: %s', $operator, implode(', ', self::$operators)));
if (!in_array($operator, self::OPERATORS, true)) {
throw new InvalidArgumentException(sprintf('"%s" is not a valid operator. Valid operators: %s', $operator, implode(', ', self::OPERATORS)));
}

$this->operator = $operator;

parent::__construct($field, $dqlAlias);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Condition/Equals.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Purist\Specification\Doctrine\Exception\InvalidArgumentException;

class Equals extends Comparison
readonly class Equals extends Comparison
{
/**
* @throws InvalidArgumentException
Expand Down
2 changes: 1 addition & 1 deletion src/Condition/EqualsProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Doctrine\ORM\QueryBuilder;
use Purist\Specification\Doctrine\Exception\InvalidArgumentException;

class EqualsProperty extends Comparison
readonly class EqualsProperty extends Comparison
{
/**
* @throws InvalidArgumentException
Expand Down
2 changes: 1 addition & 1 deletion src/Condition/GreaterThan.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Purist\Specification\Doctrine\Exception\InvalidArgumentException;

class GreaterThan extends Comparison
readonly class GreaterThan extends Comparison
{
/**
* @throws InvalidArgumentException
Expand Down
2 changes: 1 addition & 1 deletion src/Condition/GreaterThanOrEquals.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Purist\Specification\Doctrine\Exception\InvalidArgumentException;

class GreaterThanOrEquals extends Comparison
readonly class GreaterThanOrEquals extends Comparison
{
/**
* @throws InvalidArgumentException
Expand Down
2 changes: 1 addition & 1 deletion src/Condition/In.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Doctrine\ORM\QueryBuilder;
use Purist\Specification\Doctrine\AbstractSpecification;

class In extends AbstractSpecification
readonly class In extends AbstractSpecification
{
public function __construct(string $field, protected mixed $value, ?string $dqlAlias = null)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Condition/IsInstanceOf.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Doctrine\ORM\QueryBuilder;
use Purist\Specification\Doctrine\AbstractSpecification;

class IsInstanceOf extends AbstractSpecification
readonly class IsInstanceOf extends AbstractSpecification
{
public function __construct(string $field, private string $className, ?string $dqlAlias = null)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Condition/IsMemberOf.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Doctrine\ORM\QueryBuilder;
use Purist\Specification\Doctrine\AbstractSpecification;

class IsMemberOf extends AbstractSpecification
readonly class IsMemberOf extends AbstractSpecification
{
public function __construct(string $field, private string $className, ?string $dqlAlias = null)
{
Expand Down
3 changes: 2 additions & 1 deletion src/Condition/IsNotNull.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
namespace Purist\Specification\Doctrine\Condition;

use Doctrine\ORM\QueryBuilder;
use Purist\Specification\Doctrine\AbstractSpecification;

class IsNotNull extends IsNull
readonly class IsNotNull extends AbstractSpecification
{
#[\Override]
public function modify(QueryBuilder $queryBuilder, ?string $dqlAlias = null): string
Expand Down
2 changes: 1 addition & 1 deletion src/Condition/IsNull.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Doctrine\ORM\QueryBuilder;
use Purist\Specification\Doctrine\AbstractSpecification;

class IsNull extends AbstractSpecification
readonly class IsNull extends AbstractSpecification
{
#[\Override]
public function modify(QueryBuilder $queryBuilder, ?string $dqlAlias = null): string
Expand Down
2 changes: 1 addition & 1 deletion src/Condition/LessThan.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Purist\Specification\Doctrine\Exception\InvalidArgumentException;

class LessThan extends Comparison
readonly class LessThan extends Comparison
{
/**
* @throws InvalidArgumentException
Expand Down
2 changes: 1 addition & 1 deletion src/Condition/LessThanOrEquals.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Purist\Specification\Doctrine\Exception\InvalidArgumentException;

class LessThanOrEquals extends Comparison
readonly class LessThanOrEquals extends Comparison
{
/**
* @throws InvalidArgumentException
Expand Down
4 changes: 1 addition & 3 deletions src/Condition/Like.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@

use Purist\Specification\Doctrine\Exception\InvalidArgumentException;

class Like extends Comparison
readonly class Like extends Comparison
{
public const string CONTAINS = '%%%s%%';

public const string ENDS_WITH = '%%%s';

public const string STARTS_WITH = '%s%%';

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Condition/NotEquals.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Purist\Specification\Doctrine\Exception\InvalidArgumentException;

class NotEquals extends Comparison
readonly class NotEquals extends Comparison
{
/**
* @throws InvalidArgumentException
Expand Down
9 changes: 7 additions & 2 deletions src/Condition/NotIn.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@
namespace Purist\Specification\Doctrine\Condition;

use Doctrine\ORM\QueryBuilder;
use Purist\Specification\Doctrine\AbstractSpecification;

class NotIn extends In
readonly class NotIn extends AbstractSpecification
{
public function __construct(string $field, protected mixed $value, ?string $dqlAlias = null)
{
parent::__construct($field, $dqlAlias);
}

#[\Override]
public function modify(QueryBuilder $queryBuilder, ?string $dqlAlias = null): string
{
Expand All @@ -18,7 +24,6 @@ public function modify(QueryBuilder $queryBuilder, ?string $dqlAlias = null): st
);
}

#[\Override]
protected function generateParameterName(QueryBuilder $queryBuilder): string
{
return sprintf('not_in_%d', count($queryBuilder->getParameters()));
Expand Down
3 changes: 2 additions & 1 deletion src/Logic/Not.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public function __construct(private SpecificationInterface $parent)
public function modify(QueryBuilder $queryBuilder, ?string $dqlAlias = null): ?string
{
$filter = $this->parent->modify($queryBuilder, $dqlAlias);
if (null === $filter || '' === $filter || '0' === $filter) {

if (null === $filter) {
return '';
}

Expand Down
Loading

0 comments on commit 1323b7f

Please sign in to comment.