Completed
Push — master ( 1110fe...581a9c )
by Nikola
06:17
created

AbstractParser::buildConstraintFromStringUnit()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4.0466

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 19
ccs 12
cts 14
cp 0.8571
rs 9.2
cc 4
eloc 11
nc 4
nop 1
crap 4.0466
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\Exception;
19
20
/**
21
 * @author Nikola Posa <[email protected]>
22
 */
23
abstract class AbstractParser implements ParserInterface
24
{
25
    /**
26
     * @var string
27
     */
28
    protected $constraintString;
29
30
    /**
31
     * {@inheritDoc}
32
     */
33 12
    public function parse($constraintString)
34
    {
35 12
        if (!is_string($constraintString)) {
36 1
            throw InvalidConstraintStringException::forInvalidType($constraintString);
37
        }
38
39 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...
40
41 11
        if ($constraintString == '') {
42 1
            throw InvalidConstraintStringException::forEmptyCostraintString();
43
        }
44
45 10
        $this->constraintString = $constraintString;
46
47 10
        return $this->doParse();
48
    }
49
50
    /**
51
     * @return ConstraintInterface
52
     */
53
    abstract protected function doParse();
54
55 4
    protected function error()
56
    {
57 4
        throw InvalidConstraintStringException::forConstraintString($this->constraintString);
58
    }
59
60
    /**
61
     * @param string $constraintStringUnit
62
     * @return Constraint
63
     */
64 10
    protected function buildConstraintFromStringUnit($constraintStringUnit)
65
    {
66 10
        list($operator, $operandString) = array_values(
67 10
            $this->parseConstraintStringUnit($constraintStringUnit)
68 10
        );
69
70 10
        if (empty($operandString)) {
71 1
            $this->error();
72
        }
73
74
        try {
75 9
            return Constraint::fromProperties(
76 9
                $operator ?: Constraint::OPERATOR_EQ,
77 9
                Version::fromString($operandString)
78 7
            );
79 2
        } catch (Exception $ex) {
80 2
            $this->error();
81
        }
82
    }
83
84
    /**
85
     * @param string $constraintStringUnit
86
     * @return array
87
     */
88 10
    protected function parseConstraintStringUnit($constraintStringUnit)
89
    {
90 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...
91
92 10
        $i = 0;
93 10
        while (isset($constraintStringUnit[$i]) && !ctype_digit($constraintStringUnit[$i])) {
94 9
            $i++;
95 9
        }
96
97 10
        $operator = substr($constraintStringUnit, 0, $i);
98 10
        $operand = substr($constraintStringUnit, $i);
99
100
        return [
101 10
            'operator' => $operator,
102 10
            'operand' => $operand,
103 10
        ];
104
    }
105
}
106