Completed
Push — master ( a47a63...7aba9f )
by Nikola
9s
created

AbstractParser::parseConstraintStringUnit()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 17
ccs 11
cts 11
cp 1
rs 9.4285
cc 3
eloc 10
nc 2
nop 1
crap 3
1
<?php
2
3
/**
4
 * This file is part of the Version package.
5
 *
6
 * Copyright (c) Nikola Posa <[email protected]>
7
 *
8
 * For full copyright and license information, please refer to the LICENSE file,
9
 * located at the package root folder.
10
 */
11
12
namespace Version\Constraint\Parser;
13
14
use Version\Constraint\ConstraintInterface;
15
use Version\Constraint\Constraint;
16
use Version\Version;
17
use Version\Exception\InvalidConstraintStringException;
18
use Version\Exception\InvalidVersionStringException;
19
use Version\Exception\InvalidConstraintException;
20
21
/**
22
 * @author Nikola Posa <[email protected]>
23
 */
24
abstract class AbstractParser implements ParserInterface
25
{
26
    /**
27
     * @var string
28
     */
29
    protected $constraintString;
30
31
    /**
32
     * {@inheritDoc}
33
     */
34 12
    public function parse($constraintString)
35
    {
36 12
        if (!is_string($constraintString)) {
37 1
            throw InvalidConstraintStringException::forInvalidType($constraintString);
38
        }
39
40 11
        $constraintString = trim($constraintString);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $constraintString. This often makes code more readable.
Loading history...
41
42 11
        if ($constraintString == '') {
43 1
            throw InvalidConstraintStringException::forEmptyCostraintString();
44
        }
45
46 10
        $this->constraintString = $constraintString;
47
48 10
        return $this->doParse();
49
    }
50
51
    /**
52
     * @return ConstraintInterface
53
     */
54
    abstract protected function doParse();
55
56 4
    protected function error()
57
    {
58 4
        throw InvalidConstraintStringException::forConstraintString($this->constraintString);
59
    }
60
61
    /**
62
     * @param string $constraintStringUnit
63
     * @return Constraint
64
     */
65 10
    protected function buildConstraintFromStringUnit($constraintStringUnit)
66
    {
67 10
        list($operator, $operandString) = array_values(
68 10
            $this->parseConstraintStringUnit($constraintStringUnit)
69 10
        );
70
71 10
        if (empty($operandString)) {
72 1
            $this->error();
73
        }
74
75 9
        $operand = null;
76
        try {
77 9
            $operand = Version::fromString($operandString);
78 9
        } catch (InvalidVersionStringException $ex) {
79 2
            $this->error();
80
        }
81
82 7
        if (empty($operator)) {
83 1
            $operator = Constraint::OPERATOR_EQ;
84 1
        }
85
86
        try {
87 7
            return Constraint::fromProperties($operator, $operand);
88
        } catch (InvalidConstraintException $ex) {
89
            $this->error();
90
        }
91
    }
92
93
    /**
94
     * @param string $constraintStringUnit
95
     * @return array
96
     */
97 10
    protected function parseConstraintStringUnit($constraintStringUnit)
98
    {
99 10
        $operator = $operand = '';
0 ignored issues
show
Unused Code introduced by
$operand is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
Unused Code introduced by
$operator is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
100
101 10
        $i = 0;
102 10
        while (isset($constraintStringUnit[$i]) && !ctype_digit($constraintStringUnit[$i])) {
103 9
            $i++;
104 9
        }
105
106 10
        $operator = substr($constraintStringUnit, 0, $i);
107 10
        $operand = substr($constraintStringUnit, $i);
108
109
        return [
110 10
            'operator' => $operator,
111 10
            'operand' => $operand,
112 10
        ];
113
    }
114
}
115