Skip to content

Commit fed0367

Browse files
refactor: introduce BaseRegexpFunction and ParserException (martin-georgiev#269)
1 parent a078b0f commit fed0367

File tree

7 files changed

+70
-24
lines changed

7 files changed

+70
-24
lines changed

.github/workflows/sloth.yml

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,21 @@ name: Sloth
33
on:
44
merge_group:
55
pull_request:
6-
branches:
7-
- main
6+
7+
permissions:
8+
contents: read
9+
checks: read
810

911
jobs:
1012
sloth:
1113
runs-on: ubuntu-latest
1214
steps:
1315
- name: Sloth
14-
uses: lendable/sloth@0.2
16+
uses: lendable/sloth@0.2.0
1517
with:
1618
token: ${{ secrets.GITHUB_TOKEN }}
1719
interval: 10
18-
ignored:
19-
- Auto Request Review
20-
- CodeRabbit
21-
- Scrutinizer
20+
ignored: |
21+
Auto Request Review
22+
CodeRabbit
23+
Scrutinizer
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MartinGeorgiev\Doctrine\ORM\Query\AST\Functions;
6+
7+
abstract class BaseRegexpFunction extends BaseFunction
8+
{
9+
abstract protected function getFunctionName(): string;
10+
11+
abstract protected function getParameterCount(): int;
12+
13+
protected function customiseFunction(): void
14+
{
15+
$parameters = \str_repeat(', %s', $this->getParameterCount() - 1);
16+
$this->setFunctionPrototype($this->getFunctionName().'(%s'.$parameters.')');
17+
18+
for ($i = 0; $i < $this->getParameterCount(); $i++) {
19+
$this->addNodeMapping('StringPrimary');
20+
}
21+
}
22+
}

src/MartinGeorgiev/Doctrine/ORM/Query/AST/Functions/BaseVariadicFunction.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Doctrine\ORM\Query\Parser;
1010
use Doctrine\ORM\Query\SqlWalker;
1111
use Doctrine\ORM\Query\TokenType;
12+
use MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\Exception\ParserException;
1213
use MartinGeorgiev\Utils\DoctrineOrm;
1314

1415
/**
@@ -24,7 +25,7 @@ public function feedParserWithNodes(Parser $parser): void
2425

2526
$this->nodes[] = $parser->{$this->commonNodeMapping}();
2627
if ($lexer->lookahead?->type === null) {
27-
throw new \RuntimeException('The parser\'s "lookahead" property is not populated with a type');
28+
throw ParserException::missingLookaheadType();
2829
}
2930

3031
$aheadType = $lexer->lookahead->type;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\Exception;
6+
7+
class ParserException extends \RuntimeException
8+
{
9+
public static function missingLookaheadType(): self
10+
{
11+
return new self('The parser\'s "lookahead" property is not populated with a type');
12+
}
13+
}

src/MartinGeorgiev/Doctrine/ORM/Query/AST/Functions/RegexpLike.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,15 @@
1212
*
1313
* @author Martin Georgiev <martin.georgiev@gmail.com>
1414
*/
15-
class RegexpLike extends BaseFunction
15+
class RegexpLike extends BaseRegexpFunction
1616
{
17-
protected function customiseFunction(): void
17+
protected function getFunctionName(): string
1818
{
19-
$this->setFunctionPrototype('regexp_like(%s, %s)');
20-
$this->addNodeMapping('StringPrimary');
21-
$this->addNodeMapping('StringPrimary');
19+
return 'regexp_like';
20+
}
21+
22+
protected function getParameterCount(): int
23+
{
24+
return 2;
2225
}
2326
}

src/MartinGeorgiev/Doctrine/ORM/Query/AST/Functions/RegexpMatch.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,15 @@
1212
*
1313
* @author Martin Georgiev <martin.georgiev@gmail.com>
1414
*/
15-
class RegexpMatch extends BaseFunction
15+
class RegexpMatch extends BaseRegexpFunction
1616
{
17-
protected function customiseFunction(): void
17+
protected function getFunctionName(): string
1818
{
19-
$this->setFunctionPrototype('regexp_match(%s, %s)');
20-
$this->addNodeMapping('StringPrimary');
21-
$this->addNodeMapping('StringPrimary');
19+
return 'regexp_match';
20+
}
21+
22+
protected function getParameterCount(): int
23+
{
24+
return 2;
2225
}
2326
}

src/MartinGeorgiev/Doctrine/ORM/Query/AST/Functions/RegexpReplace.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@
1212
*
1313
* @author Colin Doig
1414
*/
15-
class RegexpReplace extends BaseFunction
15+
class RegexpReplace extends BaseRegexpFunction
1616
{
17-
protected function customiseFunction(): void
17+
protected function getFunctionName(): string
1818
{
19-
$this->setFunctionPrototype('regexp_replace(%s, %s, %s)');
20-
$this->addNodeMapping('StringPrimary');
21-
$this->addNodeMapping('StringPrimary');
22-
$this->addNodeMapping('StringPrimary');
19+
return 'regexp_replace';
20+
}
21+
22+
protected function getParameterCount(): int
23+
{
24+
return 3;
2325
}
2426
}

0 commit comments

Comments
 (0)