• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

MyIntervals / PHP-CSS-Parser / 13660109122

04 Mar 2025 06:09PM UTC coverage: 55.726%. Remained the same
13660109122

push

github

web-flow
[CLEANUP] Avoid Hungarian notation for `function` (#1070)

Part of #756

5 of 6 new or added lines in 2 files covered. (83.33%)

1056 of 1895 relevant lines covered (55.73%)

12.2 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.36
/src/Value/CalcFunction.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Sabberworm\CSS\Value;
6

7
use Sabberworm\CSS\Parsing\ParserState;
8
use Sabberworm\CSS\Parsing\UnexpectedEOFException;
9
use Sabberworm\CSS\Parsing\UnexpectedTokenException;
10

11
class CalcFunction extends CSSFunction
12
{
13
    /**
14
     * @var int
15
     */
16
    private const T_OPERAND = 1;
17

18
    /**
19
     * @var int
20
     */
21
    private const T_OPERATOR = 2;
22

23
    /**
24
     * @throws UnexpectedTokenException
25
     * @throws UnexpectedEOFException
26
     *
27
     * @internal since V8.8.0
28
     */
29
    public static function parse(ParserState $parserState, bool $ignoreCase = false): CSSFunction
5✔
30
    {
31
        $aOperators = ['+', '-', '*', '/'];
5✔
32
        $function = $parserState->parseIdentifier();
5✔
33
        if ($parserState->peek() != '(') {
5✔
34
            // Found ; or end of line before an opening bracket
35
            throw new UnexpectedTokenException('(', $parserState->peek(), 'literal', $parserState->currentLine());
×
36
        } elseif (!\in_array($function, ['calc', '-moz-calc', '-webkit-calc'], true)) {
5✔
37
            // Found invalid calc definition. Example calc (...
NEW
38
            throw new UnexpectedTokenException('calc', $function, 'literal', $parserState->currentLine());
×
39
        }
40
        $parserState->consume('(');
5✔
41
        $oCalcList = new CalcRuleValueList($parserState->currentLine());
5✔
42
        $list = new RuleValueList(',', $parserState->currentLine());
5✔
43
        $iNestingLevel = 0;
5✔
44
        $iLastComponentType = null;
5✔
45
        while (!$parserState->comes(')') || $iNestingLevel > 0) {
5✔
46
            if ($parserState->isEnd() && $iNestingLevel === 0) {
5✔
47
                break;
1✔
48
            }
49

50
            $parserState->consumeWhiteSpace();
5✔
51
            if ($parserState->comes('(')) {
5✔
52
                $iNestingLevel++;
3✔
53
                $oCalcList->addListComponent($parserState->consume(1));
3✔
54
                $parserState->consumeWhiteSpace();
3✔
55
                continue;
3✔
56
            } elseif ($parserState->comes(')')) {
5✔
57
                $iNestingLevel--;
3✔
58
                $oCalcList->addListComponent($parserState->consume(1));
3✔
59
                $parserState->consumeWhiteSpace();
3✔
60
                continue;
3✔
61
            }
62
            if ($iLastComponentType != CalcFunction::T_OPERAND) {
5✔
63
                $oVal = Value::parsePrimitiveValue($parserState);
5✔
64
                $oCalcList->addListComponent($oVal);
5✔
65
                $iLastComponentType = CalcFunction::T_OPERAND;
5✔
66
            } else {
67
                if (\in_array($parserState->peek(), $aOperators, true)) {
5✔
68
                    if (($parserState->comes('-') || $parserState->comes('+'))) {
4✔
69
                        if (
70
                            $parserState->peek(1, -1) != ' '
4✔
71
                            || !($parserState->comes('- ')
4✔
72
                                || $parserState->comes('+ '))
4✔
73
                        ) {
74
                            throw new UnexpectedTokenException(
1✔
75
                                " {$parserState->peek()} ",
1✔
76
                                $parserState->peek(1, -1) . $parserState->peek(2),
1✔
77
                                'literal',
1✔
78
                                $parserState->currentLine()
1✔
79
                            );
80
                        }
81
                    }
82
                    $oCalcList->addListComponent($parserState->consume(1));
3✔
83
                    $iLastComponentType = CalcFunction::T_OPERATOR;
3✔
84
                } else {
85
                    throw new UnexpectedTokenException(
1✔
86
                        \sprintf(
1✔
87
                            'Next token was expected to be an operand of type %s. Instead "%s" was found.',
1✔
88
                            \implode(', ', $aOperators),
1✔
89
                            $parserState->peek()
1✔
90
                        ),
91
                        '',
1✔
92
                        'custom',
1✔
93
                        $parserState->currentLine()
1✔
94
                    );
95
                }
96
            }
97
            $parserState->consumeWhiteSpace();
5✔
98
        }
99
        $list->addListComponent($oCalcList);
4✔
100
        if (!$parserState->isEnd()) {
4✔
101
            $parserState->consume(')');
4✔
102
        }
103
        return new CalcFunction($function, $list, ',', $parserState->currentLine());
4✔
104
    }
105
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc