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

MyIntervals / PHP-CSS-Parser / 13632504266

03 Mar 2025 01:54PM UTC coverage: 55.643% (-0.1%) from 55.749%
13632504266

push

github

web-flow
[TASK] Avoid the deprecated `__toString()` in `ParserTest` (#1056)

Part of #998

1055 of 1896 relevant lines covered (55.64%)

12.18 hits per line

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

86.84
/src/Value/CSSString.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Sabberworm\CSS\Value;
6

7
use Sabberworm\CSS\OutputFormat;
8
use Sabberworm\CSS\Parsing\ParserState;
9
use Sabberworm\CSS\Parsing\SourceException;
10
use Sabberworm\CSS\Parsing\UnexpectedEOFException;
11
use Sabberworm\CSS\Parsing\UnexpectedTokenException;
12

13
/**
14
 * This class is a wrapper for quoted strings to distinguish them from keywords.
15
 *
16
 * `CSSString`s always output with double quotes.
17
 */
18
class CSSString extends PrimitiveValue
19
{
20
    /**
21
     * @var string
22
     */
23
    private $string;
24

25
    /**
26
     * @param string $string
27
     * @param int<0, max> $lineNumber
28
     */
29
    public function __construct($string, $lineNumber = 0)
28✔
30
    {
31
        $this->string = $string;
28✔
32
        parent::__construct($lineNumber);
28✔
33
    }
28✔
34

35
    /**
36
     * @throws SourceException
37
     * @throws UnexpectedEOFException
38
     * @throws UnexpectedTokenException
39
     *
40
     * @internal since V8.8.0
41
     */
42
    public static function parse(ParserState $parserState): CSSString
22✔
43
    {
44
        $sBegin = $parserState->peek();
22✔
45
        $sQuote = null;
22✔
46
        if ($sBegin === "'") {
22✔
47
            $sQuote = "'";
10✔
48
        } elseif ($sBegin === '"') {
17✔
49
            $sQuote = '"';
15✔
50
        }
51
        if ($sQuote !== null) {
22✔
52
            $parserState->consume($sQuote);
20✔
53
        }
54
        $result = '';
22✔
55
        $sContent = null;
22✔
56
        if ($sQuote === null) {
22✔
57
            // Unquoted strings end in whitespace or with braces, brackets, parentheses
58
            while (\preg_match('/[\\s{}()<>\\[\\]]/isu', $parserState->peek()) !== 1) {
5✔
59
                $result .= $parserState->parseCharacter(false);
5✔
60
            }
61
        } else {
62
            while (!$parserState->comes($sQuote)) {
20✔
63
                $sContent = $parserState->parseCharacter(false);
20✔
64
                if ($sContent === null) {
20✔
65
                    throw new SourceException(
×
66
                        "Non-well-formed quoted string {$parserState->peek(3)}",
×
67
                        $parserState->currentLine()
×
68
                    );
69
                }
70
                $result .= $sContent;
20✔
71
            }
72
            $parserState->consume($sQuote);
20✔
73
        }
74
        return new CSSString($result, $parserState->currentLine());
22✔
75
    }
76

77
    /**
78
     * @param string $string
79
     */
80
    public function setString($string): void
1✔
81
    {
82
        $this->string = $string;
1✔
83
    }
1✔
84

85
    /**
86
     * @return string
87
     */
88
    public function getString()
8✔
89
    {
90
        return $this->string;
8✔
91
    }
92

93
    /**
94
     * @deprecated in V8.8.0, will be removed in V9.0.0. Use `render` instead.
95
     */
96
    public function __toString(): string
×
97
    {
98
        return $this->render(new OutputFormat());
×
99
    }
100

101
    public function render(OutputFormat $outputFormat): string
13✔
102
    {
103
        $string = \addslashes($this->string);
13✔
104
        $string = \str_replace("\n", '\\A', $string);
13✔
105
        return $outputFormat->getStringQuotingType() . $string . $outputFormat->getStringQuotingType();
13✔
106
    }
107
}
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