ParametersReplacingVisitor::replaceParameters()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 18
c 0
b 0
f 0
ccs 11
cts 11
cp 1
rs 9.9332
cc 3
nc 3
nop 1
crap 3
1
<?php
2
3
namespace Xsolve\SalesforceClient\QueryBuilder\Visitor\Parameters;
4
5
use Xsolve\SalesforceClient\QueryBuilder\Expr\Compare\AbstractMultiCompare;
6
use Xsolve\SalesforceClient\QueryBuilder\Expr\Compare\AbstractSingleCompare;
7
use Xsolve\SalesforceClient\QueryBuilder\Visitor\VisitorInterface;
8
9
class ParametersReplacingVisitor implements VisitorInterface
10
{
11
    /**
12
     * @var array
13
     */
14
    private $parameters;
15
16
    /**
17
     * @var ReplacingStrategyCollection
18
     */
19
    private $replacingStrategies;
20
21 21
    public function __construct(array $parameters)
22
    {
23 21
        $this->parameters = $parameters;
24 21
        $this->replacingStrategies = new ReplacingStrategyCollection();
25 21
    }
26
27 16
    public function visitSingleCompare(AbstractSingleCompare $compare)
28
    {
29 16
        $compare->update([
30 16
            'left' => $this->replaceParameters($compare->getLeft()),
31 16
            'right' => $this->replaceParameters($compare->getRight()),
32
        ]);
33 16
    }
34
35 6
    public function visitMultiCompare(AbstractMultiCompare $multiCompare)
36
    {
37 6
        $replacedValues = array_map([$this, 'replaceParameters'], $multiCompare->getValues());
38 6
        $multiCompare->update($replacedValues);
39 6
    }
40
41 21
    protected function replaceParameters(string $subject): string
42
    {
43 21
        foreach ($this->parameters as $name => $value) {
44 21
            $type = null;
45
46 21
            if (is_array($value)) {
47 1
                $type = $value['type'];
48 1
                $value = $value['value'];
49
            }
50
51 21
            $subject = preg_replace(
52 21
                sprintf('/(\{%s\})/', preg_quote($name, '/')),
53 21
                $this->replacingStrategies[$type]->replace($value),
0 ignored issues
show
Bug introduced by
The method replace() does not exist on null. ( Ignorable by Annotation )

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

53
                $this->replacingStrategies[$type]->/** @scrutinizer ignore-call */ 
54
                                                   replace($value),

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
54 21
                $subject
55
            );
56
        }
57
58 21
        return $subject;
59
    }
60
}
61