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

MyIntervals / PHP-CSS-Parser / 16112982700

07 Jul 2025 09:18AM UTC coverage: 57.935%. Remained the same
16112982700

Pull #1307

github

web-flow
Merge 81b4533d5 into 674cfa00c
Pull Request #1307: [DOCS] Temporarily drop some markers from the class diagram

1055 of 1821 relevant lines covered (57.94%)

16.67 hits per line

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

0.0
/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
×
30
    {
31
        $operators = ['+', '-', '*', '/'];
×
32
        $function = $parserState->parseIdentifier();
×
33
        if ($parserState->peek() != '(') {
×
34
            // Found ; or end of line before an opening bracket
35
            throw new UnexpectedTokenException('(', $parserState->peek(), 'literal', $parserState->currentLine());
×
36
        } elseif ($function !== 'calc') {
×
37
            // Found invalid calc definition. Example calc (...
38
            throw new UnexpectedTokenException('calc', $function, 'literal', $parserState->currentLine());
×
39
        }
40
        $parserState->consume('(');
×
41
        $calcRuleValueList = new CalcRuleValueList($parserState->currentLine());
×
42
        $list = new RuleValueList(',', $parserState->currentLine());
×
43
        $nestingLevel = 0;
×
44
        $lastComponentType = null;
×
45
        while (!$parserState->comes(')') || $nestingLevel > 0) {
×
46
            if ($parserState->isEnd() && $nestingLevel === 0) {
×
47
                break;
×
48
            }
49

50
            $parserState->consumeWhiteSpace();
×
51
            if ($parserState->comes('(')) {
×
52
                $nestingLevel++;
×
53
                $calcRuleValueList->addListComponent($parserState->consume(1));
×
54
                $parserState->consumeWhiteSpace();
×
55
                continue;
×
56
            } elseif ($parserState->comes(')')) {
×
57
                $nestingLevel--;
×
58
                $calcRuleValueList->addListComponent($parserState->consume(1));
×
59
                $parserState->consumeWhiteSpace();
×
60
                continue;
×
61
            }
62
            if ($lastComponentType != CalcFunction::T_OPERAND) {
×
63
                $value = Value::parsePrimitiveValue($parserState);
×
64
                $calcRuleValueList->addListComponent($value);
×
65
                $lastComponentType = CalcFunction::T_OPERAND;
×
66
            } else {
67
                if (\in_array($parserState->peek(), $operators, true)) {
×
68
                    if (($parserState->comes('-') || $parserState->comes('+'))) {
×
69
                        if (
70
                            $parserState->peek(1, -1) != ' '
×
71
                            || !($parserState->comes('- ')
×
72
                                || $parserState->comes('+ '))
×
73
                        ) {
74
                            throw new UnexpectedTokenException(
×
75
                                " {$parserState->peek()} ",
×
76
                                $parserState->peek(1, -1) . $parserState->peek(2),
×
77
                                'literal',
×
78
                                $parserState->currentLine()
×
79
                            );
80
                        }
81
                    }
82
                    $calcRuleValueList->addListComponent($parserState->consume(1));
×
83
                    $lastComponentType = CalcFunction::T_OPERATOR;
×
84
                } else {
85
                    throw new UnexpectedTokenException(
×
86
                        \sprintf(
×
87
                            'Next token was expected to be an operand of type %s. Instead "%s" was found.',
×
88
                            \implode(', ', $operators),
×
89
                            $parserState->peek()
×
90
                        ),
91
                        '',
×
92
                        'custom',
×
93
                        $parserState->currentLine()
×
94
                    );
95
                }
96
            }
97
            $parserState->consumeWhiteSpace();
×
98
        }
99
        $list->addListComponent($calcRuleValueList);
×
100
        if (!$parserState->isEnd()) {
×
101
            $parserState->consume(')');
×
102
        }
103
        return new CalcFunction($function, $list, ',', $parserState->currentLine());
×
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