Skip to content

Commit f14a523

Browse files
author
Wout Gevaert
committed
Uniformize class check errors.
Maybe Type checks should also be included but that's a bit more complicated
1 parent 9115dd0 commit f14a523

File tree

11 files changed

+106
-172
lines changed

11 files changed

+106
-172
lines changed

src/Assignment.php

Lines changed: 3 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\ErrorHandling\ErrorHelper;
2525
use WikibaseSolutions\CypherDSL\Types\AnyType;
2626

2727
/**
@@ -44,9 +44,7 @@ class Assignment extends BinaryOperator
4444
*/
4545
public function __construct(AnyType $left, AnyType $right)
4646
{
47-
if (!($left instanceof Property) && !($left instanceof Variable)) {
48-
throw new TypeError("\$left must be either a Property or a Variable");
49-
}
47+
ErrorHelper::assertClass('left', [Property::class, Variable::class], $left);
5048

5149
parent::__construct($left, $right, false);
5250
}
@@ -72,4 +70,4 @@ protected function getOperator(): string
7270
{
7371
return $this->mutate ? "+=" : "=";
7472
}
75-
}
73+
}

src/Clauses/CallProcedureClause.php

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

2222
namespace WikibaseSolutions\CypherDSL\Clauses;
2323

24-
use InvalidArgumentException;
25-
use TypeError;
26-
use WikibaseSolutions\CypherDSL\ErrorHandling\ErrorTextHelper;
24+
use WikibaseSolutions\CypherDSL\ErrorHandling\ErrorHelper;
2725
use WikibaseSolutions\CypherDSL\Types\AnyType;
2826
use WikibaseSolutions\CypherDSL\Variable;
2927

@@ -76,15 +74,7 @@ public function setProcedure(string $procedure): self
7674
public function withArguments(array $arguments): self
7775
{
7876
foreach ($arguments as $argument) {
79-
if (!($argument instanceof AnyType)) {
80-
throw new TypeError(
81-
ErrorTextHelper::getTypeErrorObjectArrayText(
82-
'arguments',
83-
'AnyType',
84-
$argument
85-
)
86-
);
87-
}
77+
ErrorHelper::assertClass('argument', AnyType::class, $argument);
8878
}
8979

9080
$this->arguments = $arguments;
@@ -115,9 +105,7 @@ public function addArgument(AnyType $argument): self
115105
public function yields(array $variables): self
116106
{
117107
foreach ($variables as $variable) {
118-
if (!($variable instanceof Variable)) {
119-
throw new InvalidArgumentException("\$variables should only consist of Variable objects");
120-
}
108+
ErrorHelper::assertClass('variable', Variable::class, $variable);
121109
}
122110

123111
$this->yieldVariables = $variables;
@@ -158,4 +146,4 @@ protected function getSubject(): string
158146
return sprintf("%s(%s)", $this->procedure, $arguments);
159147
}
160148
}
161-
}
149+
}

src/Clauses/RemoveClause.php

Lines changed: 3 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\ErrorHandling\ErrorHelper;
2525
use WikibaseSolutions\CypherDSL\Label;
2626
use WikibaseSolutions\CypherDSL\Property;
2727
use WikibaseSolutions\CypherDSL\QueryConvertable;
@@ -46,9 +46,7 @@ class RemoveClause extends Clause
4646
*/
4747
public function addExpression($expression): self
4848
{
49-
if (!($expression instanceof Property) && !($expression instanceof Label)) {
50-
throw new TypeError("\$expression must be either a Property or a Label");
51-
}
49+
ErrorHelper::assertClass('expression', [Property::class, Label::class], $expression);
5250

5351
$this->expressions[] = $expression;
5452

@@ -73,4 +71,4 @@ protected function getSubject(): string
7371
array_map(fn(QueryConvertable $expression) => $expression->toQuery(), $this->expressions)
7472
);
7573
}
76-
}
74+
}

src/Clauses/SetClause.php

Lines changed: 3 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\ErrorHandling\ErrorHelper;
2626
use WikibaseSolutions\CypherDSL\Label;
2727
use WikibaseSolutions\CypherDSL\QueryConvertable;
2828

@@ -46,9 +46,7 @@ class SetClause extends Clause
4646
*/
4747
public function addAssignment($expression): self
4848
{
49-
if (!($expression instanceof Assignment) && !($expression instanceof Label)) {
50-
throw new TypeError("\$expression should be either an Assignment object or a Label object");
51-
}
49+
ErrorHelper::assertClass('expression', [Assignment::class, Label::class], $expression);
5250

5351
$this->expressions[] = $expression;
5452

@@ -73,4 +71,4 @@ protected function getSubject(): string
7371
array_map(fn(QueryConvertable $expression): string => $expression->toQuery(), $this->expressions)
7472
);
7573
}
76-
}
74+
}

src/ErrorHandling/ErrorHelper.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
namespace WikibaseSolutions\CypherDSL\ErrorHandling;
4+
5+
use TypeError;
6+
7+
/**
8+
* Convenience class including simple assertions and error reporting functions
9+
*/
10+
class ErrorHelper {
11+
12+
/**
13+
* Asserts that $userInput is an instance of one of the provided $classNames (polyfill for php 8.0 Union types)
14+
*
15+
* @param string $varName The name of the userinput variable, to be used in the error message.
16+
* @param string|string[] $classNames The classnames that should be tested against
17+
* @param mixed $userInput The input that should be tested
18+
* @throws TypeError
19+
*/
20+
public static function assertClass(string $varName, $classNames, $userInput) : void {
21+
if (!is_array($classNames)) {
22+
$classNames = [$classNames];
23+
}
24+
foreach ($classNames as $class) {
25+
if ($userInput instanceof $class)
26+
return;
27+
}
28+
throw new TypeError(
29+
self::getTypeErrorText(
30+
$varName,
31+
$classNames,
32+
$userInput
33+
)
34+
);
35+
}
36+
37+
/**
38+
* Give a nice error message about $userInput not being an object with one of the $classNames types.
39+
*
40+
* @param string $varname The name of the variable to be used in the message (without trailing '$')
41+
* @param array $classNames The classnames that should be mentioned in the message
42+
* @param mixed $userInput The input that has been given.
43+
*/
44+
public static function getTypeErrorText(
45+
string $varName,
46+
array $classNames,
47+
$userInput
48+
) : string {
49+
return
50+
"\$$varName should be a " .
51+
implode(' or ', $classNames) . " object, " .
52+
self::getUserInputInfo($userInput) . "given.";
53+
}
54+
55+
/**
56+
* Simple function to determine what $userInput is.
57+
*
58+
* @param mixed $userInput
59+
* @return string A description of $userInput.
60+
*/
61+
public static function getUserInputInfo($userInput) : string {
62+
$info = gettype( $userInput );
63+
if ( $info === 'object' ) {
64+
$info = get_class( $userInput );
65+
} else {
66+
$info .= ' "' . (string) $userInput . '"';
67+
}
68+
return $info;
69+
}
70+
}

src/ErrorHandling/ErrorTextHelper.php

Lines changed: 0 additions & 29 deletions
This file was deleted.

src/ExpressionList.php

Lines changed: 3 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\ErrorHandling\ErrorHelper;
2626
use WikibaseSolutions\CypherDSL\Traits\ListTypeTrait;
2727
use WikibaseSolutions\CypherDSL\Types\AnyType;
2828
use WikibaseSolutions\CypherDSL\Types\CompositeTypes\ListType;
@@ -53,9 +53,7 @@ class ExpressionList implements ListType
5353
public function __construct(array $expressions)
5454
{
5555
foreach ($expressions as $expression) {
56-
if (!($expression instanceof AnyType)) {
57-
throw new TypeError("\$expressions must be an array of only AnyType objects");
58-
}
56+
ErrorHelper::assertClass('expression', AnyType::class, $expression);
5957
}
6058

6159
$this->expressions = $expressions;
@@ -73,4 +71,4 @@ public function toQuery(): string
7371

7472
return sprintf("[%s]", implode(", ", $expressions));
7573
}
76-
}
74+
}

src/Functions/IsEmpty.php

Lines changed: 3 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\ErrorHandling\ErrorHelper;
2525
use WikibaseSolutions\CypherDSL\Traits\BooleanTypeTrait;
2626
use WikibaseSolutions\CypherDSL\Types\AnyType;
2727
use WikibaseSolutions\CypherDSL\Types\CompositeTypes\ListType;
@@ -54,9 +54,7 @@ class IsEmpty extends FunctionCall implements BooleanType
5454
*/
5555
public function __construct(AnyType $list)
5656
{
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-
}
57+
ErrorHelper::assertClass('list', [ListType::class, MapType::class, StringType::class], $list);
6058

6159
$this->list = $list;
6260
}
@@ -76,4 +74,4 @@ protected function getParameters(): array
7674
{
7775
return [$this->list];
7876
}
79-
}
77+
}

src/Functions/RawFunction.php

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

2424
use InvalidArgumentException;
25-
use TypeError;
25+
use WikibaseSolutions\CypherDSL\ErrorHandling\ErrorHelper;
2626
use WikibaseSolutions\CypherDSL\Traits\BooleanTypeTrait;
2727
use WikibaseSolutions\CypherDSL\Traits\ListTypeTrait;
2828
use WikibaseSolutions\CypherDSL\Traits\MapTypeTrait;
@@ -79,15 +79,7 @@ class RawFunction extends FunctionCall implements
7979
public function __construct(string $functionName, array $parameters)
8080
{
8181
foreach ($parameters as $parameter) {
82-
if (!($parameter instanceof AnyType)) {
83-
throw new TypeError(
84-
ErrorTextHelper::getTypeErrorObjectArrayText(
85-
'parameters',
86-
'AnyType',
87-
$parameter
88-
)
89-
);
90-
}
82+
ErrorHelper::assertClass('parameter', AnyType::class, $parameter);
9183
}
9284

9385
if (!preg_match("/^[a-zA-Z0-9_]+$/", $functionName)) {
@@ -117,4 +109,4 @@ protected function getParameters(): array
117109
{
118110
return $this->parameters;
119111
}
120-
}
112+
}

src/PropertyMap.php

Lines changed: 3 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\ErrorHandling\ErrorHelper;
2525
use WikibaseSolutions\CypherDSL\Traits\EscapeTrait;
2626
use WikibaseSolutions\CypherDSL\Traits\MapTypeTrait;
2727
use WikibaseSolutions\CypherDSL\Types\AnyType;
@@ -53,9 +53,7 @@ class PropertyMap implements MapType
5353
public function __construct(array $properties = [])
5454
{
5555
foreach ($properties as $property) {
56-
if (!($property instanceof AnyType)) {
57-
throw new InvalidArgumentException("\$properties must be an array of only AnyType objects");
58-
}
56+
ErrorHelper::assertClass('property', AnyType::class, $property);
5957
}
6058

6159
$this->properties = $properties;
@@ -101,4 +99,4 @@ public function toQuery(): string
10199

102100
return sprintf("{%s}", implode(", ", $pairs));
103101
}
104-
}
102+
}

0 commit comments

Comments
 (0)