Completed
Pull Request — master (#7)
by Marcus
04:20 queued 02:09
created

Assertions::assertNumber()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 3
cts 4
cp 0.75
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 2
crap 2.0625
1
<?php
2
3
namespace LesserPhp\Library;
4
5
use LesserPhp\Exception\GeneralException;
6
7
/**
8
 * lesserphp
9
 * https://www.maswaba.de/lesserphp
10
 *
11
 * LESS CSS compiler, adapted from http://lesscss.org
12
 *
13
 * Copyright 2013, Leaf Corcoran <[email protected]>
14
 * Copyright 2016, Marcus Schwarz <[email protected]>
15
 * Licensed under MIT or GPLv3, see LICENSE
16
 * @package LesserPhp
17
 */
18
class Assertions
19
{
20
    private $coerce;
21
22 64
    public function __construct(Coerce $coerce)
23
    {
24 64
        $this->coerce = $coerce;
25 64
    }
26
27 2
    public function assertColor($value, $error = "expected color value")
28
    {
29 2
        $color = $this->coerce->coerceColor($value);
30 2
        if ($color === null) {
31
            throw new GeneralException($error);
32
        }
33
34 2
        return $color;
35
    }
36
37 4
    public function assertNumber($value, $error = "expecting number")
38
    {
39 4
        if ($value[0] === "number") {
40 4
            return $value[1];
41
        }
42
        throw new GeneralException($error);
43
    }
44
45 2
    public function assertArgs($value, $expectedArgs, $name = "")
46
    {
47 2
        if ($expectedArgs == 1) {
48
            return $value;
49
        } else {
50 2
            if ($value[0] !== "list" || $value[1] !== ",") {
51
                throw new GeneralException('expecting list');
52
            }
53 2
            $values = $value[2];
54 2
            $numValues = count($values);
55 2
            if ($expectedArgs != $numValues) {
56
                if ($name) {
57
                    $name .= ": ";
58
                }
59
                throw new GeneralException("${name}expecting $expectedArgs arguments, got $numValues");
60
            }
61
62 2
            return $values;
63
        }
64
    }
65
66
    public function assertMinArgs($value, $expectedMinArgs, $name = "")
67
    {
68 4
        if ($value[0] !== "list" || $value[1] !== ",") {
69
            throw new GeneralException("expecting list");
70
        }
71 4
        $values = $value[2];
72 4
        $numValues = count($values);
73 4
        if ($expectedMinArgs > $numValues) {
74 2
            if ($name) {
75 2
                $name .= ": ";
76 2
            }
77
78 2
            throw new GeneralException("${name}expecting at least $expectedMinArgs arguments, got $numValues");
79
        }
80
81 2
        return $values;
82
    }
83
}
84