Skip to content

Commit

Permalink
Merge pull request #182 from getkirby/refactor/query-strict-types
Browse files Browse the repository at this point in the history
Strict types for `Query` package
  • Loading branch information
bastianallgeier committed Apr 25, 2023
2 parents 0f48453 + decec30 commit 90657de
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 11 deletions.
5 changes: 5 additions & 0 deletions src/Query/Argument.php
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);

namespace Kirby\Query;

Expand Down Expand Up @@ -70,6 +71,10 @@ public static function factory(string $argument): static

// numeric
if (is_numeric($argument) === true) {
if (strpos($argument, '.') === false) {
return new static((int)$argument);
}

return new static((float)$argument);
}

Expand Down
1 change: 1 addition & 0 deletions src/Query/Arguments.php
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);

namespace Kirby\Query;

Expand Down
1 change: 1 addition & 0 deletions src/Query/Expression.php
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);

namespace Kirby\Query;

Expand Down
1 change: 1 addition & 0 deletions src/Query/Query.php
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);

namespace Kirby\Query;

Expand Down
1 change: 1 addition & 0 deletions src/Query/Segment.php
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);

namespace Kirby\Query;

Expand Down
1 change: 1 addition & 0 deletions src/Query/Segments.php
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);

namespace Kirby\Query;

Expand Down
8 changes: 5 additions & 3 deletions tests/Query/ArgumentTest.php
Expand Up @@ -43,7 +43,9 @@ public function testFactory()

// numbers
$argument = Argument::factory(' 23 ');
$this->assertSame(23.0, $argument->value);
$this->assertSame(23, $argument->value);
$argument = Argument::factory(' 23.3 ');
$this->assertSame(23.3, $argument->value);

// null
$argument = Argument::factory(' null ');
Expand All @@ -70,8 +72,8 @@ public function testResolve()
$this->assertSame(' 23 ', $argument);

// arrays
$argument = Argument::factory('[1, "a", 3]')->resolve();
$this->assertSame([1.0, 'a', 3.0], $argument);
$argument = Argument::factory('[1, "a", 3.3]')->resolve();
$this->assertSame([1, 'a', 3.3], $argument);

// nested query
$argument = Argument::factory('foo')->resolve(['foo' => 'bar']);
Expand Down
14 changes: 7 additions & 7 deletions tests/Query/ArgumentsTest.php
Expand Up @@ -33,19 +33,19 @@ public function testFactory()
*/
public function testResolve()
{
$arguments = Arguments::factory('1, 2, 3');
$this->assertSame([1.0 , 2.0, 3.0], $arguments->resolve());
$arguments = Arguments::factory('1, 2.3, 3');
$this->assertSame([1 , 2.3, 3], $arguments->resolve());

$arguments = Arguments::factory('1, 2, [3, 4]');
$this->assertSame([1.0 , 2.0, [3.0, 4.0]], $arguments->resolve());
$arguments = Arguments::factory('1, 2, [3.3, 4]');
$this->assertSame([1 , 2, [3.3, 4]], $arguments->resolve());

$arguments = Arguments::factory('1, 2, \'3, 4\'');
$this->assertSame([1.0 , 2.0, '3, 4'], $arguments->resolve());
$this->assertSame([1 , 2, '3, 4'], $arguments->resolve());

$arguments = Arguments::factory('1, 2, "3, 4"');
$this->assertSame([1.0 , 2.0, '3, 4'], $arguments->resolve());
$this->assertSame([1 , 2, '3, 4'], $arguments->resolve());

$arguments = Arguments::factory('1, 2, \'(3, 4)\'');
$this->assertSame([1.0 , 2.0, '(3, 4)'], $arguments->resolve());
$this->assertSame([1 , 2, '(3, 4)'], $arguments->resolve());
}
}
2 changes: 1 addition & 1 deletion tests/Query/ExpressionTest.php
Expand Up @@ -91,7 +91,7 @@ public function providerResolve(): array
['0 ?: "no"', 'no'],
['null ?? null ?? null ?? "yes"', 'yes'],
['"yes" ?? "no"', 'yes'],
['null ?? (null ?? null ?? (false ? "what" : 42)) ?? "no"', 42.0],
['null ?? (null ?? null ?? (false ? "what" : 42)) ?? "no"', 42],
];
}

Expand Down

0 comments on commit 90657de

Please sign in to comment.