Skip to content

Commit 0ca5ff1

Browse files
Merge pull request #29 from wgevaert/feature-requests-wout
improve error messages by including info about user input
2 parents be393ee + 161bf3e commit 0ca5ff1

17 files changed

+438
-84
lines changed

src/Assignment.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
namespace WikibaseSolutions\CypherDSL;
2323

24-
use TypeError;
24+
use WikibaseSolutions\CypherDSL\Traits\ErrorTrait;
2525
use WikibaseSolutions\CypherDSL\Types\AnyType;
2626

2727
/**
@@ -32,6 +32,8 @@
3232
*/
3333
class Assignment extends BinaryOperator
3434
{
35+
use ErrorTrait;
36+
3537
/**
3638
* @var bool Whether to use the property mutation instead of the property replacement
3739
* operator.
@@ -44,9 +46,7 @@ class Assignment extends BinaryOperator
4446
*/
4547
public function __construct(AnyType $left, AnyType $right)
4648
{
47-
if (!($left instanceof Property) && !($left instanceof Variable)) {
48-
throw new TypeError("\$left must be either a Property or a Variable");
49-
}
49+
$this->assertClass('left', [Property::class, Variable::class], $left);
5050

5151
parent::__construct($left, $right, false);
5252
}
@@ -72,4 +72,4 @@ protected function getOperator(): string
7272
{
7373
return $this->mutate ? "+=" : "=";
7474
}
75-
}
75+
}

src/Clauses/CallProcedureClause.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
namespace WikibaseSolutions\CypherDSL\Clauses;
2323

24-
use InvalidArgumentException;
24+
use WikibaseSolutions\CypherDSL\Traits\ErrorTrait;
2525
use WikibaseSolutions\CypherDSL\Types\AnyType;
2626
use WikibaseSolutions\CypherDSL\Variable;
2727

@@ -32,6 +32,8 @@
3232
*/
3333
class CallProcedureClause extends Clause
3434
{
35+
use ErrorTrait;
36+
3537
/**
3638
* @var string|null The procedure to call
3739
*/
@@ -74,9 +76,7 @@ public function setProcedure(string $procedure): self
7476
public function withArguments(array $arguments): self
7577
{
7678
foreach ($arguments as $argument) {
77-
if (!($argument instanceof AnyType)) {
78-
throw new InvalidArgumentException("\$arguments should only consist of AnyType objects");
79-
}
79+
$this->assertClass('argument', AnyType::class, $argument);
8080
}
8181

8282
$this->arguments = $arguments;
@@ -107,9 +107,7 @@ public function addArgument(AnyType $argument): self
107107
public function yields(array $variables): self
108108
{
109109
foreach ($variables as $variable) {
110-
if (!($variable instanceof Variable)) {
111-
throw new InvalidArgumentException("\$variables should only consist of Variable objects");
112-
}
110+
$this->assertClass('variable', Variable::class, $variable);
113111
}
114112

115113
$this->yieldVariables = $variables;
@@ -150,4 +148,4 @@ protected function getSubject(): string
150148
return sprintf("%s(%s)", $this->procedure, $arguments);
151149
}
152150
}
153-
}
151+
}

src/Clauses/RemoveClause.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
namespace WikibaseSolutions\CypherDSL\Clauses;
2323

24-
use TypeError;
24+
use WikibaseSolutions\CypherDSL\Traits\ErrorTrait;
2525
use WikibaseSolutions\CypherDSL\Label;
2626
use WikibaseSolutions\CypherDSL\Property;
2727
use WikibaseSolutions\CypherDSL\QueryConvertable;
@@ -33,6 +33,8 @@
3333
*/
3434
class RemoveClause extends Clause
3535
{
36+
use ErrorTrait;
37+
3638
/**
3739
* @var Property[]|Label[] The expressions in this REMOVE clause.
3840
*/
@@ -46,9 +48,7 @@ class RemoveClause extends Clause
4648
*/
4749
public function addExpression($expression): self
4850
{
49-
if (!($expression instanceof Property) && !($expression instanceof Label)) {
50-
throw new TypeError("\$expression must be either a Property or a Label");
51-
}
51+
$this->assertClass('expression', [Property::class, Label::class], $expression);
5252

5353
$this->expressions[] = $expression;
5454

@@ -73,4 +73,4 @@ protected function getSubject(): string
7373
array_map(fn(QueryConvertable $expression) => $expression->toQuery(), $this->expressions)
7474
);
7575
}
76-
}
76+
}

src/Clauses/SetClause.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121

2222
namespace WikibaseSolutions\CypherDSL\Clauses;
2323

24-
use TypeError;
2524
use WikibaseSolutions\CypherDSL\Assignment;
25+
use WikibaseSolutions\CypherDSL\Traits\ErrorTrait;
2626
use WikibaseSolutions\CypherDSL\Label;
2727
use WikibaseSolutions\CypherDSL\QueryConvertable;
2828

@@ -33,6 +33,8 @@
3333
*/
3434
class SetClause extends Clause
3535
{
36+
use ErrorTrait;
37+
3638
/**
3739
* @var Assignment[]|Label[] $expressions The expressions to set
3840
*/
@@ -46,9 +48,7 @@ class SetClause extends Clause
4648
*/
4749
public function addAssignment($expression): self
4850
{
49-
if (!($expression instanceof Assignment) && !($expression instanceof Label)) {
50-
throw new TypeError("\$expression should be either an Assignment object or a Label object");
51-
}
51+
$this->assertClass('expression', [Assignment::class, Label::class], $expression);
5252

5353
$this->expressions[] = $expression;
5454

@@ -73,4 +73,4 @@ protected function getSubject(): string
7373
array_map(fn(QueryConvertable $expression): string => $expression->toQuery(), $this->expressions)
7474
);
7575
}
76-
}
76+
}

src/ExpressionList.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121

2222
namespace WikibaseSolutions\CypherDSL;
2323

24-
use TypeError;
2524
use WikibaseSolutions\CypherDSL\Traits\EscapeTrait;
25+
use WikibaseSolutions\CypherDSL\Traits\ErrorTrait;
2626
use WikibaseSolutions\CypherDSL\Traits\ListTypeTrait;
2727
use WikibaseSolutions\CypherDSL\Types\AnyType;
2828
use WikibaseSolutions\CypherDSL\Types\CompositeTypes\ListType;
@@ -38,6 +38,7 @@
3838
class ExpressionList implements ListType
3939
{
4040
use EscapeTrait;
41+
use ErrorTrait;
4142
use ListTypeTrait;
4243

4344
/**
@@ -53,9 +54,7 @@ class ExpressionList implements ListType
5354
public function __construct(array $expressions)
5455
{
5556
foreach ($expressions as $expression) {
56-
if (!($expression instanceof AnyType)) {
57-
throw new TypeError("\$expressions must be an array of only AnyType objects");
58-
}
57+
$this->assertClass('expression', AnyType::class, $expression);
5958
}
6059

6160
$this->expressions = $expressions;
@@ -73,4 +72,4 @@ public function toQuery(): string
7372

7473
return sprintf("[%s]", implode(", ", $expressions));
7574
}
76-
}
75+
}

src/Functions/IsEmpty.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
namespace WikibaseSolutions\CypherDSL\Functions;
2323

24-
use TypeError;
24+
use WikibaseSolutions\CypherDSL\Traits\ErrorTrait;
2525
use WikibaseSolutions\CypherDSL\Traits\BooleanTypeTrait;
2626
use WikibaseSolutions\CypherDSL\Types\AnyType;
2727
use WikibaseSolutions\CypherDSL\Types\CompositeTypes\ListType;
@@ -37,6 +37,7 @@
3737
class IsEmpty extends FunctionCall implements BooleanType
3838
{
3939
use BooleanTypeTrait;
40+
use ErrorTrait;
4041

4142
/**
4243
* @var ListType|MapType|StringType An expression that returns a list
@@ -54,9 +55,7 @@ class IsEmpty extends FunctionCall implements BooleanType
5455
*/
5556
public function __construct(AnyType $list)
5657
{
57-
if (!($list instanceof ListType) && !($list instanceof MapType) && !($list instanceof StringType)) {
58-
throw new TypeError("\$list must be either a ListType, MapType or a StringType");
59-
}
58+
$this->assertClass('list', [ListType::class, MapType::class, StringType::class], $list);
6059

6160
$this->list = $list;
6261
}
@@ -76,4 +75,4 @@ protected function getParameters(): array
7675
{
7776
return [$this->list];
7877
}
79-
}
78+
}

src/Functions/RawFunction.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
namespace WikibaseSolutions\CypherDSL\Functions;
2323

2424
use InvalidArgumentException;
25+
use WikibaseSolutions\CypherDSL\Traits\ErrorTrait;
2526
use WikibaseSolutions\CypherDSL\Traits\BooleanTypeTrait;
2627
use WikibaseSolutions\CypherDSL\Traits\ListTypeTrait;
2728
use WikibaseSolutions\CypherDSL\Traits\MapTypeTrait;
@@ -51,6 +52,7 @@ class RawFunction extends FunctionCall implements
5152
PathType
5253
{
5354
use BooleanTypeTrait;
55+
use ErrorTrait;
5456
use ListTypeTrait;
5557
use MapTypeTrait;
5658
use NodeTypeTrait;
@@ -77,9 +79,7 @@ class RawFunction extends FunctionCall implements
7779
public function __construct(string $functionName, array $parameters)
7880
{
7981
foreach ($parameters as $parameter) {
80-
if (!($parameter instanceof AnyType)) {
81-
throw new InvalidArgumentException("\$parameters should only consist of AnyType objects");
82-
}
82+
$this->assertClass('parameter', AnyType::class, $parameter);
8383
}
8484

8585
if (!preg_match("/^[a-zA-Z0-9_]+$/", $functionName)) {
@@ -109,4 +109,4 @@ protected function getParameters(): array
109109
{
110110
return $this->parameters;
111111
}
112-
}
112+
}

src/PropertyMap.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
namespace WikibaseSolutions\CypherDSL;
2323

24-
use InvalidArgumentException;
24+
use WikibaseSolutions\CypherDSL\Traits\ErrorTrait;
2525
use WikibaseSolutions\CypherDSL\Traits\EscapeTrait;
2626
use WikibaseSolutions\CypherDSL\Traits\MapTypeTrait;
2727
use WikibaseSolutions\CypherDSL\Types\AnyType;
@@ -38,6 +38,7 @@
3838
class PropertyMap implements MapType
3939
{
4040
use EscapeTrait;
41+
use ErrorTrait;
4142
use MapTypeTrait;
4243

4344
/**
@@ -53,9 +54,7 @@ class PropertyMap implements MapType
5354
public function __construct(array $properties = [])
5455
{
5556
foreach ($properties as $property) {
56-
if (!($property instanceof AnyType)) {
57-
throw new InvalidArgumentException("\$properties must be an array of only AnyType objects");
58-
}
57+
$this->assertClass('property', AnyType::class, $property);
5958
}
6059

6160
$this->properties = $properties;
@@ -101,4 +100,4 @@ public function toQuery(): string
101100

102101
return sprintf("{%s}", implode(", ", $pairs));
103102
}
104-
}
103+
}

0 commit comments

Comments
 (0)