Completed
Push — master ( b56942...7c5a31 )
by Peter
05:38 queued 11s
created

ArgumentToOperandConverter::convert()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 8.8017
c 0
b 0
f 0
cc 6
nc 6
nop 1
1
<?php
2
3
namespace Happyr\DoctrineSpecification\Operand;
4
5
use Happyr\DoctrineSpecification\Exception\NotConvertibleException;
6
7
/**
8
 * This service is intended for backward compatibility and may be removed in the future.
9
 */
10
class ArgumentToOperandConverter
11
{
12
    /**
13
     * Convert the argument into the field operand if it is not an operand.
14
     *
15
     * @param Operand|string $argument
16
     *
17
     * @return Operand
18
     */
19
    public static function toField($argument)
20
    {
21
        if ($argument instanceof Operand) {
22
            return $argument;
23
        }
24
25
        return new Field($argument);
26
    }
27
28
    /**
29
     * Convert the argument into the value operand if it is not an operand.
30
     *
31
     * @param Operand|string $argument
32
     *
33
     * @return Operand
34
     */
35
    public static function toValue($argument)
36
    {
37
        if ($argument instanceof Operand) {
38
            return $argument;
39
        }
40
41
        return new Value($argument);
42
    }
43
44
    /**
45
     * Are all arguments is a operands?
46
     *
47
     * @param array $arguments
48
     *
49
     * @return bool
50
     */
51
    public static function isAllOperands(array $arguments)
52
    {
53
        foreach ($arguments as $argument) {
54
            if (!($argument instanceof Operand)) {
55
                return false;
56
            }
57
        }
58
59
        return true;
60
    }
61
62
    /**
63
     * Convert all possible arguments to operands.
64
     *
65
     * @param Operand[]|string[] $arguments
66
     *
67
     * @throws NotConvertibleException
68
     *
69
     * @return Operand[]
70
     */
71
    public static function convert(array $arguments)
72
    {
73
        if (!$arguments) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $arguments of type array<Happyr\DoctrineSpe...Operand\Operand|string> is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
74
            return [];
75
        }
76
77
        // always try convert the first argument to the field operand
78
        $field = self::toField(array_shift($arguments));
0 ignored issues
show
Bug introduced by
It seems like array_shift($arguments) targeting array_shift() can also be of type null; however, Happyr\DoctrineSpecifica...andConverter::toField() does only seem to accept object<Happyr\DoctrineSp...Operand\Operand>|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
79
80
        if (!$arguments) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $arguments of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
81
            return [$field];
82
        }
83
84
        // always try convert the last argument to the value operand
85
        $value = self::toValue(array_pop($arguments));
86
87
        if (!$arguments) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $arguments of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
88
            return [$field, $value];
89
        }
90
91
        $result = [$field];
92
        foreach ($arguments as $argument) {
93
            if (!($argument instanceof Operand)) {
94
                throw new NotConvertibleException('You passed arguments not all of which are operands.');
95
            }
96
            $result[] = $argument;
97
        }
98
        $result[] = $value;
99
100
        return $result;
101
    }
102
}
103