Test Failed
Push — fix-7 ( 3c71b3 )
by Wilmer
09:34 queued 01:55
created

LikeConditionBuilder::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Oracle\Builder;
6
7
use Exception;
8
use Yiisoft\Db\QueryBuilder\Conditions\Builder\LikeConditionBuilder as AbstractLikeConditionBuilder;
9
use Yiisoft\Db\QueryBuilder\Conditions\Interface\LikeConditionInterface;
10
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface;
11
use Yiisoft\Db\Schema\Quoter;
12
13
use function substr;
14
15
final class LikeConditionBuilder extends AbstractLikeConditionBuilder
16
{
17
    private string|null $escapeCharacter = '!';
18
19
    /**
20
     * `\` is initialized in {@see buildLikeCondition()} method since we need to choose replacement value based on
21
     * {@see Quoter::quoteValue()}.
22
     */
23
    protected array $escapingReplacements = [
24
        '%' => '!%',
25
        '_' => '!_',
26
        '!' => '!!',
27
    ];
28
29
    public function __construct(private QueryBuilderInterface $queryBuilder)
30
    {
31
        parent::__construct($queryBuilder, $this->getEscapeSql());
0 ignored issues
show
Unused Code introduced by
The call to Yiisoft\Db\QueryBuilder\...nBuilder::__construct() has too many arguments starting with $this->getEscapeSql(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

31
        parent::/** @scrutinizer ignore-call */ 
32
                __construct($queryBuilder, $this->getEscapeSql());

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
32
    }
33
34
    /**
35
     * @throws Exception
36
     */
37
    public function build(LikeConditionInterface $expression, array &$params = []): string
38
    {
39
        if (!isset($this->escapingReplacements['\\'])) {
40
            /*
41
             * Different pdo_oci8 versions may or may not implement PDO::quote(), so {@see Quoter::quoteValue()} may or
42
             * may not quote \.
43
             */
44
            $this->escapingReplacements['\\'] = substr((string) $this->queryBuilder->quoter()->quoteValue('\\'), 1, -1);
45
        }
46
47
        return parent::build($expression, $params);
48
    }
49
50
    /**
51
     * @return string character used to escape special characters in LIKE conditions.
52
     * By default, it's assumed to be `!`.
53
     */
54
    private function getEscapeSql(): string
55
    {
56
        if ($this->escapeCharacter !== null) {
57
            return " ESCAPE '{$this->escapeCharacter}'";
58
        }
59
60
        return '';
61
    }
62
}
63