Passed
Push — master ( e7c9c0...9bf250 )
by Aleksandar
01:58
created

ConcatOperator   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 55
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 13 3
A evaluate() 0 14 3
1
<?php
2
/**
3
 * @author Aleksandar Panic
4
 * @license http://www.apache.org/licenses/LICENSE-2.0
5
 * @since 1.0.0
6
 **/
7
8
namespace ArekX\ArrayExpression\Operators;
9
10
11
use ArekX\ArrayExpression\Exceptions\InvalidEvaluationResultType;
12
use ArekX\ArrayExpression\Interfaces\ExpressionParser;
13
use ArekX\ArrayExpression\Interfaces\Operator;
14
use ArekX\ArrayExpression\Interfaces\ValueParser;
15
16
/**
17
 * Class ConcatOperator
18
 * Operator for returning concatenated strings
19
 *
20
 * @package ArekX\ArrayExpression\Operators
21
 */
22
class ConcatOperator extends BaseOperator
23
{
24
    /**
25
     * Expressions of which will be in concat operation.
26
     *
27
     * @var Operator[]
28
     */
29
    public $concatOperators = [];
30
31
    /**
32
     * Passes data from operator configuration.
33
     *
34
     * Depending on the operator this data can contain other sub-expressions which need to be parsed using
35
     * ExpressionParser
36
     *
37
     * @param array $config Expressions to be processed
38
     * @see ExpressionParser
39
     */
40 6
    public function configure(array $config)
41
    {
42 6
        $this->setName($config[0] ?? 'unknown');
43
44 6
        if (count($config) < 2) {
45 2
            throw new \InvalidArgumentException("Minimum format must be satisfied: ['{$this->getName()}', <expression>]");
46
        }
47
48 4
        $this->concatOperators = [];
49
50 4
        for ($i = 1; $i < count($config); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
51 4
            $this->assertIsExpression($config[$i]);
52 4
            $this->concatOperators[] = $this->parser->parse($config[$i]);
53
        }
54 4
    }
55
56
    /**
57
     * Evaluates one value.
58
     *
59
     * @param ValueParser $value Value to be evaluated
60
     * @return string Evaluation result
61
     * @throws InvalidEvaluationResultType
62
     */
63 2
    public function evaluate(ValueParser $value)
64
    {
65 2
        $result = "";
66 2
        foreach ($this->concatOperators as $operator) {
67 2
            $concatResult = $operator->evaluate($value);
68
69 2
            if (!is_string($concatResult)) {
70 1
                throw new InvalidEvaluationResultType($concatResult, 'string');
71
            }
72
73 2
            $result .= $concatResult;
74
        }
75
76 1
        return $result;
77
    }
78
79
}