Skip to content

Commit 5eb1c97

Browse files
committed
Fixed extrqctor and loader configuration issues
1 parent f6a1304 commit 5eb1c97

File tree

8 files changed

+176
-137
lines changed

8 files changed

+176
-137
lines changed

src/Builder/Extractor.php

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,16 @@ public function addParameter(string|int $key, Node\Expr $parameter): StepBuilder
9494
public function getNode(): Node
9595
{
9696
return new Node\Expr\New_(
97-
class: new Node\Name\FullyQualified('Kiboko\Component\Flow\SQL\Extractor'),
97+
class: new Node\Name\FullyQualified('Kibok\\Component\\Flow\\SQL\\Extractor'),
9898
args: [
9999
new Node\Arg(
100100
value: $this->connection->getNode()
101101
),
102102
new Node\Arg(
103103
value: $this->query
104104
),
105-
$this->parameters ? new Node\Arg(
106-
value: new Node\Expr\Closure(
105+
count($this->parameters) > 0
106+
? new Node\Arg(value: new Node\Expr\Closure(
107107
subNodes: [
108108
'params' => [
109109
new Node\Param(
@@ -115,22 +115,18 @@ class: new Node\Name\FullyQualified('Kiboko\Component\Flow\SQL\Extractor'),
115115
...$this->compileParameters()
116116
],
117117
],
118-
),
119-
) : new Node\Expr\ConstFetch(new Node\Name('null')),
120-
$this->beforeQueries ? new Node\Arg(
121-
value: $this->compileBeforeQueries()
122-
) : new Node\Expr\Array_(
123-
attributes: [
118+
))
119+
: new Node\Expr\ConstFetch(new Node\Name('null')),
120+
count($this->beforeQueries) > 0
121+
? new Node\Arg(value: $this->compileBeforeQueries())
122+
: new Node\Expr\Array_(attributes: [
124123
'kind' => Node\Expr\Array_::KIND_SHORT
125-
],
126-
),
127-
$this->afterQueries ? new Node\Arg(
128-
value: $this->compileAfterQueries()
129-
): new Node\Expr\Array_(
130-
attributes: [
124+
]),
125+
count($this->afterQueries) > 0
126+
? new Node\Arg(value: $this->compileAfterQueries())
127+
: new Node\Expr\Array_(attributes: [
131128
'kind' => Node\Expr\Array_::KIND_SHORT
132-
],
133-
),
129+
]),
134130
],
135131
);
136132
}
@@ -183,7 +179,6 @@ public function compileBeforeQueries(): Node\Expr
183179
);
184180
}
185181

186-
187182
public function compileAfterQueries(): Node\Expr
188183
{
189184
$output = [];

src/Builder/Inline.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111

1212
final class Inline implements Builder
1313
{
14-
public function __construct(private ArrayBuilderInterface|ObjectBuilderInterface $mapper)
15-
{
16-
}
14+
public function __construct(
15+
private ArrayBuilderInterface|ObjectBuilderInterface $mapper
16+
) {}
1717

1818
public function getNode(): Node
1919
{

src/Builder/Loader.php

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,16 @@ public function addParameter(string|int $key, Node\Expr $parameter): StepBuilder
9494
public function getNode(): Node
9595
{
9696
return new Node\Expr\New_(
97-
class: new Node\Name\FullyQualified('Kiboko\Component\Flow\SQL\Loader'),
97+
class: new Node\Name\FullyQualified('Kiboko\\Component\\Flow\\SQL\\Loader'),
9898
args: [
9999
new Node\Arg(
100100
value: $this->connection->getNode()
101101
),
102102
new Node\Arg(
103103
value: $this->query
104104
),
105-
$this->parameters ? new Node\Arg(
106-
value: new Node\Expr\Closure(
105+
count($this->parameters) > 0
106+
? new Node\Arg(value: new Node\Expr\Closure(
107107
subNodes: [
108108
'params' => [
109109
new Node\Param(
@@ -116,24 +116,20 @@ class: new Node\Name\FullyQualified('Kiboko\Component\Flow\SQL\Loader'),
116116
],
117117
'stmts' => [
118118
...$this->compileParameters()
119-
]
119+
],
120120
],
121-
),
122-
) : new Node\Expr\ConstFetch(new Node\Name('null')),
123-
$this->beforeQueries ? new Node\Arg(
124-
value: $this->compileBeforeQueries()
125-
) : new Node\Expr\Array_(
126-
attributes: [
121+
))
122+
: new Node\Expr\ConstFetch(new Node\Name('null')),
123+
count($this->beforeQueries) > 0
124+
? new Node\Arg(value: $this->compileBeforeQueries())
125+
: new Node\Expr\Array_(attributes: [
127126
'kind' => Node\Expr\Array_::KIND_SHORT
128-
]
129-
),
130-
$this->afterQueries ? new Node\Arg(
131-
value: $this->compileAfterQueries()
132-
): new Node\Expr\Array_(
133-
attributes: [
127+
]),
128+
count($this->afterQueries) > 0
129+
? new Node\Arg(value: $this->compileAfterQueries())
130+
: new Node\Expr\Array_(attributes: [
134131
'kind' => Node\Expr\Array_::KIND_SHORT
135-
]
136-
)
132+
]),
137133
],
138134
);
139135
}
@@ -146,18 +142,18 @@ public function compileParameters(): iterable
146142
var: new Node\Expr\Variable('statement'),
147143
name: new Node\Identifier('bindParam'),
148144
args: [
149-
new Node\Arg(
150-
is_string($key) ? new Node\Scalar\Encapsed(
151-
[
152-
new Node\Scalar\EncapsedStringPart(':'),
153-
new Node\Scalar\EncapsedStringPart($key)
154-
]
155-
) : new Node\Scalar\LNumber($key)
156-
),
157-
new Node\Arg(
158-
$parameter
159-
),
160-
],
145+
new Node\Arg(
146+
is_string($key) ? new Node\Scalar\Encapsed(
147+
[
148+
new Node\Scalar\EncapsedStringPart(':'),
149+
new Node\Scalar\EncapsedStringPart($key)
150+
]
151+
) : new Node\Scalar\LNumber($key)
152+
),
153+
new Node\Arg(
154+
$parameter
155+
),
156+
],
161157
)
162158
);
163159
}

src/Configuration/Parameters.php

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,32 +23,13 @@ public function getConfigTreeBuilder()
2323
})
2424
->thenUnset()
2525
->end()
26-
->arrayPrototype()
27-
->children()
28-
->variableNode('key')
29-
->isRequired()
30-
->cannotBeEmpty()
31-
->validate()
32-
->ifTrue(fn ($data) => !is_string($data) && !is_int($data))
33-
->thenInvalid('The key of your parameter must be a string or an integer.')
34-
->end()
35-
->validate()
36-
->ifTrue(fn ($data) => is_int($data) && $data < 1)
37-
->thenInvalid('The key of your parameter must be greater or equal to 1.')
38-
->end()
39-
->validate()
40-
->ifTrue(isExpression())
41-
->then(asExpression())
42-
->end()
43-
->end()
44-
->variableNode('value')
45-
->isRequired()
46-
->cannotBeEmpty()
47-
->validate()
48-
->ifTrue(isExpression())
49-
->then(asExpression())
50-
->end()
51-
->end()
26+
->useAttributeAsKey('key')
27+
->variablePrototype()
28+
->isRequired()
29+
->cannotBeEmpty()
30+
->validate()
31+
->ifTrue(isExpression())
32+
->then(asExpression())
5233
->end()
5334
->end();
5435

src/Factory/Extractor.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function validate(array $config): bool
4343
$this->processor->processConfiguration($this->configuration, $config);
4444

4545
return true;
46-
} catch (Symfony\InvalidTypeException|Symfony\InvalidConfigurationException) {
46+
} catch (Symfony\InvalidTypeException|Symfony\InvalidConfigurationException $exception) {
4747
return false;
4848
}
4949
}
@@ -55,8 +55,8 @@ public function compile(array $config): SQL\Factory\Repository\Extractor
5555
);
5656

5757
if ($config['parameters'] != null) {
58-
foreach ($config["parameters"] as $parameter) {
59-
$extractor->addParameter($parameter['key'], compileValue($this->interpreter, $parameter['value']));
58+
foreach ($config["parameters"] as $key => $value) {
59+
$extractor->addParameter($key, compileValue($this->interpreter, $value));
6060
}
6161
}
6262

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace functional\Kiboko\Plugin\SQL;
4+
5+
use Kiboko\Component\PHPUnitExtension\Assert;
6+
use Kiboko\Plugin\SQL\Factory\Extractor;
7+
use PHPUnit\Framework\TestCase;
8+
use Symfony\Component\ExpressionLanguage\Expression;
9+
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
10+
11+
final class ExtractorTest extends TestCase
12+
{
13+
use Assert\PipelineBuilderAssertTrait;
14+
use Assert\ExtractorBuilderAssertTrait;
15+
16+
public function testValidatingConfiguration(): void
17+
{
18+
$extractor = new Extractor(new ExpressionLanguage());
19+
20+
$this->assertTrue(
21+
$extractor->validate([
22+
[
23+
'query' => 'SELECT * FROM foo WHERE value IS NOT NULL AND id <= :identifier',
24+
'parameters' => [
25+
'identifier' => '@=3',
26+
],
27+
],
28+
]),
29+
);
30+
}
31+
32+
public function testNormalizingConfiguration(): void
33+
{
34+
$extractor = new Extractor(new ExpressionLanguage());
35+
36+
$this->assertEquals(
37+
[
38+
'query' => 'SELECT * FROM foo WHERE value IS NOT NULL AND id <= :identifier',
39+
'parameters' => [
40+
'identifier' => new Expression('3'),
41+
],
42+
],
43+
$extractor->normalize([
44+
[
45+
'query' => 'SELECT * FROM foo WHERE value IS NOT NULL AND id <= :identifier',
46+
'parameters' => [
47+
'identifier' => '@=3',
48+
],
49+
],
50+
]),
51+
);
52+
}
53+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace functional\Kiboko\Plugin\SQL;
4+
5+
use Kiboko\Component\PHPUnitExtension\Assert;
6+
use Kiboko\Plugin\SQL\Factory\Loader;
7+
use PHPUnit\Framework\TestCase;
8+
use Symfony\Component\ExpressionLanguage\Expression;
9+
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
10+
11+
final class LoaderTest extends TestCase
12+
{
13+
use Assert\PipelineBuilderAssertTrait;
14+
use Assert\ExtractorBuilderAssertTrait;
15+
16+
public function testValidatingConfiguration(): void
17+
{
18+
$extractor = new Loader(new ExpressionLanguage());
19+
20+
$this->assertTrue(
21+
$extractor->validate([
22+
[
23+
'query' => 'INSERT INTO foo WHERE value IS NOT NULL AND id <= :identifier',
24+
'parameters' => [
25+
'identifier' => '@=3',
26+
'value' => '@=36',
27+
],
28+
],
29+
]),
30+
);
31+
}
32+
33+
public function testNormalizingConfiguration(): void
34+
{
35+
$extractor = new Loader(new ExpressionLanguage());
36+
37+
$this->assertEquals(
38+
[
39+
'query' => 'INSERT INTO foo WHERE value IS NOT NULL AND id <= :identifier',
40+
'parameters' => [
41+
'identifier' => new Expression('3'),
42+
'value' => new Expression('36'),
43+
],
44+
],
45+
$extractor->normalize([
46+
[
47+
'query' => 'INSERT INTO foo WHERE value IS NOT NULL AND id <= :identifier',
48+
'parameters' => [
49+
'identifier' => '@=3',
50+
'value' => '@=36',
51+
],
52+
],
53+
]),
54+
);
55+
}
56+
}

0 commit comments

Comments
 (0)