Passed
Push — float-arrays ( d487a4...cd1ee0 )
by Martin
10:59
created

BaseFunction::customiseFunction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 2
c 1
b 1
f 0
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MartinGeorgiev\Doctrine\ORM\Query\AST\Functions;
6
7
use Doctrine\ORM\Query\AST\Functions\FunctionNode;
8
use Doctrine\ORM\Query\AST\Node;
9
use Doctrine\ORM\Query\Lexer;
10
use Doctrine\ORM\Query\Parser;
11
use Doctrine\ORM\Query\SqlWalker;
12
use Doctrine\ORM\Query\TokenType;
13
use MartinGeorgiev\Utils\DoctrineOrm;
14
15
/**
16
 * @since 0.1
17
 *
18
 * @author Martin Georgiev <[email protected]>
19
 */
20
abstract class BaseFunction extends FunctionNode
21
{
22
    protected string $functionPrototype;
23
24
    /**
25
     * @var list<string>
0 ignored issues
show
Bug introduced by
The type MartinGeorgiev\Doctrine\...uery\AST\Functions\list was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
26
     */
27
    protected array $nodesMapping = [];
28
29
    /**
30
     * @var list<Node|null>
31
     */
32
    protected array $nodes = [];
33
34
    abstract protected function customizeFunction(): void;
35
36 100
    protected function setFunctionPrototype(string $functionPrototype): void
37
    {
38 100
        $this->functionPrototype = $functionPrototype;
39
    }
40
41 82
    protected function addNodeMapping(string $parserMethod): void
42
    {
43 82
        $this->nodesMapping[] = $parserMethod;
44
    }
45
46 98
    public function parse(Parser $parser): void
47
    {
48 98
        $shouldUseLexer = DoctrineOrm::isPre219();
49
50 98
        $this->customizeFunction();
51
52 98
        $parser->match($shouldUseLexer ? Lexer::T_IDENTIFIER : TokenType::T_IDENTIFIER);
0 ignored issues
show
Bug introduced by
The constant Doctrine\ORM\Query\Lexer::T_IDENTIFIER was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
53 98
        $parser->match($shouldUseLexer ? Lexer::T_OPEN_PARENTHESIS : TokenType::T_OPEN_PARENTHESIS);
0 ignored issues
show
Bug introduced by
The constant Doctrine\ORM\Query\Lexer::T_OPEN_PARENTHESIS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
54 98
        $this->feedParserWithNodes($parser);
55 91
        $parser->match($shouldUseLexer ? Lexer::T_CLOSE_PARENTHESIS : TokenType::T_CLOSE_PARENTHESIS);
0 ignored issues
show
Bug introduced by
The constant Doctrine\ORM\Query\Lexer::T_CLOSE_PARENTHESIS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
56
    }
57
58
    /**
59
     * Feeds given parser with previously set nodes.
60
     */
61 82
    protected function feedParserWithNodes(Parser $parser): void
62
    {
63 82
        $nodesMappingCount = \count($this->nodesMapping);
64 82
        $lastNode = $nodesMappingCount - 1;
65 82
        for ($i = 0; $i < $nodesMappingCount; $i++) {
66 82
            $parserMethod = $this->nodesMapping[$i];
67
            // @phpstan-ignore-next-line
68 82
            $this->nodes[$i] = $parser->{$parserMethod}();
69 82
            if ($i < $lastNode) {
70 55
                $parser->match(\class_exists(TokenType::class) ? TokenType::T_COMMA : Lexer::T_COMMA);
0 ignored issues
show
Bug introduced by
The constant Doctrine\ORM\Query\Lexer::T_COMMA was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
71
            }
72
        }
73
    }
74
75 82
    public function getSql(SqlWalker $sqlWalker): string
76
    {
77 82
        $dispatched = [];
78 82
        foreach ($this->nodes as $node) {
79 82
            $dispatched[] = $node instanceof Node ? $node->dispatch($sqlWalker) : 'null';
80
        }
81
82 82
        return \vsprintf($this->functionPrototype, $dispatched);
83
    }
84
}
85