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

MyIntervals / PHP-CSS-Parser / 13818275499

12 Mar 2025 05:58PM UTC coverage: 55.455% (-0.1%) from 55.591%
13818275499

push

github

web-flow
[CLEANUP] Avoid magic method forwarding in `DeclarationBlock` (#1161)

Part of #1147

5 of 5 new or added lines in 1 file covered. (100.0%)

3 existing lines in 1 file now uncovered.

1042 of 1879 relevant lines covered (55.46%)

12.35 hits per line

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

97.7
/src/OutputFormat.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Sabberworm\CSS;
6

7
final class OutputFormat
8
{
9
    /**
10
     * Value format: `"` means double-quote, `'` means single-quote
11
     *
12
     * @var string
13
     */
14
    private $stringQuotingType = '"';
15

16
    /**
17
     * Output RGB colors in hash notation if possible
18
     *
19
     * @var bool
20
     */
21
    private $usesRgbHashNotation = true;
22

23
    /**
24
     * Declaration format
25
     *
26
     * Semicolon after the last rule of a declaration block can be omitted. To do that, set this false.
27
     *
28
     * @var bool
29
     */
30
    private $renderSemicolonAfterLastRule = true;
31

32
    /**
33
     * Spacing
34
     * Note that these strings are not sanity-checked: the value should only consist of whitespace
35
     * Any newline character will be indented according to the current level.
36
     * The triples (After, Before, Between) can be set using a wildcard
37
     * (e.g. `$outputFormat->set('Space*Rules', "\n");`)
38
     *
39
     * @var string
40
     */
41
    private $spaceAfterRuleName = ' ';
42

43
    /**
44
     * @var string
45
     */
46
    private $spaceBeforeRules = '';
47

48
    /**
49
     * @var string
50
     */
51
    private $spaceAfterRules = '';
52

53
    /**
54
     * @var string
55
     */
56
    private $spaceBetweenRules = '';
57

58
    /**
59
     * @var string
60
     */
61
    private $spaceBeforeBlocks = '';
62

63
    /**
64
     * @var string
65
     */
66
    private $spaceAfterBlocks = '';
67

68
    /**
69
     * @var string
70
     */
71
    private $spaceBetweenBlocks = "\n";
72

73
    /**
74
     * Content injected in and around at-rule blocks.
75
     *
76
     * @var string
77
     */
78
    private $contentBeforeAtRuleBlock = '';
79

80
    /**
81
     * @var string
82
     */
83
    private $contentAfterAtRuleBlock = '';
84

85
    /**
86
     * This is what’s printed before and after the comma if a declaration block contains multiple selectors.
87
     *
88
     * @var string
89
     */
90
    private $spaceBeforeSelectorSeparator = '';
91

92
    /**
93
     * @var string
94
     */
95
    private $spaceAfterSelectorSeparator = ' ';
96

97
    /**
98
     * This is what’s inserted before the separator in value lists, by default.
99
     *
100
     * @var string
101
     */
102
    private $spaceBeforeListArgumentSeparator = '';
103

104
    /**
105
     * Keys are separators (e.g. `,`).  Values are the space sequence to insert, or an empty string.
106
     *
107
     * @var array<non-empty-string, string>
108
     */
109
    private $spaceBeforeListArgumentSeparators = [];
110

111
    /**
112
     * This is what’s inserted after the separator in value lists, by default.
113
     *
114
     * @var string
115
     */
116
    private $spaceAfterListArgumentSeparator = '';
117

118
    /**
119
     * Keys are separators (e.g. `,`).  Values are the space sequence to insert, or an empty string.
120
     *
121
     * @var array<non-empty-string, string>
122
     */
123
    private $spaceAfterListArgumentSeparators = [];
124

125
    /**
126
     * @var string
127
     */
128
    private $spaceBeforeOpeningBrace = ' ';
129

130
    /**
131
     * Content injected in and around declaration blocks.
132
     *
133
     * @var string
134
     */
135
    private $contentBeforeDeclarationBlock = '';
136

137
    /**
138
     * @var string
139
     */
140
    private $contentAfterDeclarationBlockSelectors = '';
141

142
    /**
143
     * @var string
144
     */
145
    private $contentAfterDeclarationBlock = '';
146

147
    /**
148
     * Indentation character(s) per level. Only applicable if newlines are used in any of the spacing settings.
149
     *
150
     * @var string
151
     */
152
    private $indentation = "\t";
153

154
    /**
155
     * Output exceptions.
156
     *
157
     * @var bool
158
     */
159
    private $shouldIgnoreExceptions = false;
160

161
    /**
162
     * Render comments for lists and RuleSets
163
     *
164
     * @var bool
165
     */
166
    private $shouldRenderComments = false;
167

168
    /**
169
     * @var OutputFormatter|null
170
     */
171
    private $outputFormatter;
172

173
    /**
174
     * @var OutputFormat|null
175
     */
176
    private $nextLevelFormat;
177

178
    /**
179
     * @var int
180
     */
181
    private $indentationLevel = 0;
182

183
    /**
184
     * @param non-empty-string $methodName
185
     * @param array<array-key, mixed> $arguments
186
     *
187
     * @return mixed
188
     *
189
     * @throws \Exception
190
     */
UNCOV
191
    public function __call(string $methodName, array $arguments)
×
192
    {
UNCOV
193
        if (\method_exists(OutputFormatter::class, $methodName)) {
×
194
            // @deprecated since 8.8.0, will be removed in 9.0.0. Call the method on the formatter directly instead.
UNCOV
195
            return \call_user_func_array([$this->getFormatter(), $methodName], $arguments);
×
196
        } else {
197
            throw new \Exception('Unknown OutputFormat method called: ' . $methodName);
×
198
        }
199
    }
200

201
    /**
202
     * @internal
203
     */
204
    public function getStringQuotingType(): string
21✔
205
    {
206
        return $this->stringQuotingType;
21✔
207
    }
208

209
    /**
210
     * @return $this fluent interface
211
     */
212
    public function setStringQuotingType(string $quotingType): self
3✔
213
    {
214
        $this->stringQuotingType = $quotingType;
3✔
215

216
        return $this;
3✔
217
    }
218

219
    /**
220
     * @internal
221
     */
222
    public function usesRgbHashNotation(): bool
23✔
223
    {
224
        return $this->usesRgbHashNotation;
23✔
225
    }
226

227
    /**
228
     * @return $this fluent interface
229
     */
230
    public function setRGBHashNotation(bool $usesRgbHashNotation): self
4✔
231
    {
232
        $this->usesRgbHashNotation = $usesRgbHashNotation;
4✔
233

234
        return $this;
4✔
235
    }
236

237
    /**
238
     * @internal
239
     */
240
    public function shouldRenderSemicolonAfterLastRule(): bool
23✔
241
    {
242
        return $this->renderSemicolonAfterLastRule;
23✔
243
    }
244

245
    /**
246
     * @return $this fluent interface
247
     */
248
    public function setSemicolonAfterLastRule(bool $renderSemicolonAfterLastRule): self
4✔
249
    {
250
        $this->renderSemicolonAfterLastRule = $renderSemicolonAfterLastRule;
4✔
251

252
        return $this;
4✔
253
    }
254

255
    /**
256
     * @internal
257
     */
258
    public function getSpaceAfterRuleName(): string
25✔
259
    {
260
        return $this->spaceAfterRuleName;
25✔
261
    }
262

263
    /**
264
     * @return $this fluent interface
265
     */
266
    public function setSpaceAfterRuleName(string $whitespace): self
20✔
267
    {
268
        $this->spaceAfterRuleName = $whitespace;
20✔
269

270
        return $this;
20✔
271
    }
272

273
    /**
274
     * @internal
275
     */
276
    public function getSpaceBeforeRules(): string
24✔
277
    {
278
        return $this->spaceBeforeRules;
24✔
279
    }
280

281
    /**
282
     * @return $this fluent interface
283
     */
284
    public function setSpaceBeforeRules(string $whitespace): self
35✔
285
    {
286
        $this->spaceBeforeRules = $whitespace;
35✔
287

288
        return $this;
35✔
289
    }
290

291
    /**
292
     * @internal
293
     */
294
    public function getSpaceAfterRules(): string
24✔
295
    {
296
        return $this->spaceAfterRules;
24✔
297
    }
298

299
    /**
300
     * @return $this fluent interface
301
     */
302
    public function setSpaceAfterRules(string $whitespace): self
35✔
303
    {
304
        $this->spaceAfterRules = $whitespace;
35✔
305

306
        return $this;
35✔
307
    }
308

309
    /**
310
     * @internal
311
     */
312
    public function getSpaceBetweenRules(): string
22✔
313
    {
314
        return $this->spaceBetweenRules;
22✔
315
    }
316

317
    /**
318
     * @return $this fluent interface
319
     */
320
    public function setSpaceBetweenRules(string $whitespace): self
35✔
321
    {
322
        $this->spaceBetweenRules = $whitespace;
35✔
323

324
        return $this;
35✔
325
    }
326

327
    /**
328
     * @internal
329
     */
330
    public function getSpaceBeforeBlocks(): string
24✔
331
    {
332
        return $this->spaceBeforeBlocks;
24✔
333
    }
334

335
    /**
336
     * @return $this fluent interface
337
     */
338
    public function setSpaceBeforeBlocks(string $whitespace): self
35✔
339
    {
340
        $this->spaceBeforeBlocks = $whitespace;
35✔
341

342
        return $this;
35✔
343
    }
344

345
    /**
346
     * @internal
347
     */
348
    public function getSpaceAfterBlocks(): string
24✔
349
    {
350
        return $this->spaceAfterBlocks;
24✔
351
    }
352

353
    /**
354
     * @return $this fluent interface
355
     */
356
    public function setSpaceAfterBlocks(string $whitespace): self
35✔
357
    {
358
        $this->spaceAfterBlocks = $whitespace;
35✔
359

360
        return $this;
35✔
361
    }
362

363
    /**
364
     * @internal
365
     */
366
    public function getSpaceBetweenBlocks(): string
23✔
367
    {
368
        return $this->spaceBetweenBlocks;
23✔
369
    }
370

371
    /**
372
     * @return $this fluent interface
373
     */
374
    public function setSpaceBetweenBlocks(string $whitespace): self
36✔
375
    {
376
        $this->spaceBetweenBlocks = $whitespace;
36✔
377

378
        return $this;
36✔
379
    }
380

381
    /**
382
     * @internal
383
     */
384
    public function getContentBeforeAtRuleBlock(): string
22✔
385
    {
386
        return $this->contentBeforeAtRuleBlock;
22✔
387
    }
388

389
    /**
390
     * @return $this fluent interface
391
     */
392
    public function setBeforeAtRuleBlock(string $content): self
2✔
393
    {
394
        $this->contentBeforeAtRuleBlock = $content;
2✔
395

396
        return $this;
2✔
397
    }
398

399
    /**
400
     * @internal
401
     */
402
    public function getContentAfterAtRuleBlock(): string
22✔
403
    {
404
        return $this->contentAfterAtRuleBlock;
22✔
405
    }
406

407
    /**
408
     * @return $this fluent interface
409
     */
410
    public function setAfterAtRuleBlock(string $content): self
2✔
411
    {
412
        $this->contentAfterAtRuleBlock = $content;
2✔
413

414
        return $this;
2✔
415
    }
416

417
    /**
418
     * @internal
419
     */
420
    public function getSpaceBeforeSelectorSeparator(): string
22✔
421
    {
422
        return $this->spaceBeforeSelectorSeparator;
22✔
423
    }
424

425
    /**
426
     * @return $this fluent interface
427
     */
428
    public function setSpaceBeforeSelectorSeparator(string $whitespace): self
2✔
429
    {
430
        $this->spaceBeforeSelectorSeparator = $whitespace;
2✔
431

432
        return $this;
2✔
433
    }
434

435
    /**
436
     * @internal
437
     */
438
    public function getSpaceAfterSelectorSeparator(): string
24✔
439
    {
440
        return $this->spaceAfterSelectorSeparator;
24✔
441
    }
442

443
    /**
444
     * @return $this fluent interface
445
     */
446
    public function setSpaceAfterSelectorSeparator(string $whitespace): self
19✔
447
    {
448
        $this->spaceAfterSelectorSeparator = $whitespace;
19✔
449

450
        return $this;
19✔
451
    }
452

453
    /**
454
     * @internal
455
     */
456
    public function getSpaceBeforeListArgumentSeparator(): string
20✔
457
    {
458
        return $this->spaceBeforeListArgumentSeparator;
20✔
459
    }
460

461
    /**
462
     * @return $this fluent interface
463
     */
464
    public function setSpaceBeforeListArgumentSeparator(string $whitespace): self
2✔
465
    {
466
        $this->spaceBeforeListArgumentSeparator = $whitespace;
2✔
467

468
        return $this;
2✔
469
    }
470

471
    /**
472
     * @return array<non-empty-string, string>
473
     *
474
     * @internal
475
     */
476
    public function getSpaceBeforeListArgumentSeparators(): array
20✔
477
    {
478
        return $this->spaceBeforeListArgumentSeparators;
20✔
479
    }
480

481
    /**
482
     * @param array<non-empty-string, string> $separatorSpaces
483
     *
484
     * @return $this fluent interface
485
     */
486
    public function setSpaceBeforeListArgumentSeparators(array $separatorSpaces): self
2✔
487
    {
488
        $this->spaceBeforeListArgumentSeparators = $separatorSpaces;
2✔
489

490
        return $this;
2✔
491
    }
492

493
    /**
494
     * @internal
495
     */
496
    public function getSpaceAfterListArgumentSeparator(): string
19✔
497
    {
498
        return $this->spaceAfterListArgumentSeparator;
19✔
499
    }
500

501
    /**
502
     * @return $this fluent interface
503
     */
504
    public function setSpaceAfterListArgumentSeparator(string $whitespace): self
4✔
505
    {
506
        $this->spaceAfterListArgumentSeparator = $whitespace;
4✔
507

508
        return $this;
4✔
509
    }
510

511
    /**
512
     * @return array<non-empty-string, string>
513
     *
514
     * @internal
515
     */
516
    public function getSpaceAfterListArgumentSeparators(): array
22✔
517
    {
518
        return $this->spaceAfterListArgumentSeparators;
22✔
519
    }
520

521
    /**
522
     * @param array<non-empty-string, string> $separatorSpaces
523
     *
524
     * @return $this fluent interface
525
     */
526
    public function setSpaceAfterListArgumentSeparators(array $separatorSpaces): self
19✔
527
    {
528
        $this->spaceAfterListArgumentSeparators = $separatorSpaces;
19✔
529

530
        return $this;
19✔
531
    }
532

533
    /**
534
     * @internal
535
     */
536
    public function getSpaceBeforeOpeningBrace(): string
24✔
537
    {
538
        return $this->spaceBeforeOpeningBrace;
24✔
539
    }
540

541
    /**
542
     * @return $this fluent interface
543
     */
544
    public function setSpaceBeforeOpeningBrace(string $whitespace): self
19✔
545
    {
546
        $this->spaceBeforeOpeningBrace = $whitespace;
19✔
547

548
        return $this;
19✔
549
    }
550

551
    /**
552
     * @internal
553
     */
554
    public function getContentBeforeDeclarationBlock(): string
22✔
555
    {
556
        return $this->contentBeforeDeclarationBlock;
22✔
557
    }
558

559
    /**
560
     * @return $this fluent interface
561
     */
562
    public function setBeforeDeclarationBlock(string $content): self
2✔
563
    {
564
        $this->contentBeforeDeclarationBlock = $content;
2✔
565

566
        return $this;
2✔
567
    }
568

569
    /**
570
     * @internal
571
     */
572
    public function getContentAfterDeclarationBlockSelectors(): string
22✔
573
    {
574
        return $this->contentAfterDeclarationBlockSelectors;
22✔
575
    }
576

577
    /**
578
     * @return $this fluent interface
579
     */
580
    public function setAfterDeclarationBlockSelectors(string $content): self
2✔
581
    {
582
        $this->contentAfterDeclarationBlockSelectors = $content;
2✔
583

584
        return $this;
2✔
585
    }
586

587
    /**
588
     * @internal
589
     */
590
    public function getContentAfterDeclarationBlock(): string
22✔
591
    {
592
        return $this->contentAfterDeclarationBlock;
22✔
593
    }
594

595
    /**
596
     * @return $this fluent interface
597
     */
598
    public function setAfterDeclarationBlock(string $content): self
2✔
599
    {
600
        $this->contentAfterDeclarationBlock = $content;
2✔
601

602
        return $this;
2✔
603
    }
604

605
    /**
606
     * @internal
607
     */
608
    public function getIndentation(): string
33✔
609
    {
610
        return $this->indentation;
33✔
611
    }
612

613
    /**
614
     * @return $this fluent interface
615
     */
616
    public function setIndentation(string $indentation): self
16✔
617
    {
618
        $this->indentation = $indentation;
16✔
619

620
        return $this;
16✔
621
    }
622

623
    /**
624
     * @internal
625
     */
626
    public function shouldIgnoreExceptions(): bool
24✔
627
    {
628
        return $this->shouldIgnoreExceptions;
24✔
629
    }
630

631
    /**
632
     * @return $this fluent interface
633
     */
634
    public function setIgnoreExceptions(bool $ignoreExceptions): self
6✔
635
    {
636
        $this->shouldIgnoreExceptions = $ignoreExceptions;
6✔
637

638
        return $this;
6✔
639
    }
640

641
    /**
642
     * @internal
643
     */
644
    public function shouldRenderComments(): bool
25✔
645
    {
646
        return $this->shouldRenderComments;
25✔
647
    }
648

649
    /**
650
     * @return $this fluent interface
651
     */
652
    public function setRenderComments(bool $renderComments): self
33✔
653
    {
654
        $this->shouldRenderComments = $renderComments;
33✔
655

656
        return $this;
33✔
657
    }
658

659
    /**
660
     * @internal
661
     */
662
    public function getIndentationLevel(): int
22✔
663
    {
664
        return $this->indentationLevel;
22✔
665
    }
666

667
    /**
668
     * @return $this fluent interface
669
     */
670
    public function indentWithTabs(int $numberOfTabs = 1): self
6✔
671
    {
672
        return $this->setIndentation(\str_repeat("\t", $numberOfTabs));
6✔
673
    }
674

675
    /**
676
     * @return $this fluent interface
677
     */
678
    public function indentWithSpaces(int $numberOfSpaces = 2): self
7✔
679
    {
680
        return $this->setIndentation(\str_repeat(' ', $numberOfSpaces));
7✔
681
    }
682

683
    /**
684
     * @internal since V8.8.0
685
     */
686
    public function nextLevel(): self
25✔
687
    {
688
        if ($this->nextLevelFormat === null) {
25✔
689
            $this->nextLevelFormat = clone $this;
25✔
690
            $this->nextLevelFormat->indentationLevel++;
25✔
691
            $this->nextLevelFormat->outputFormatter = null;
25✔
692
        }
693
        return $this->nextLevelFormat;
25✔
694
    }
695

696
    public function beLenient(): void
1✔
697
    {
698
        $this->shouldIgnoreExceptions = true;
1✔
699
    }
1✔
700

701
    /**
702
     * @internal since 8.8.0
703
     */
704
    public function getFormatter(): OutputFormatter
23✔
705
    {
706
        if ($this->outputFormatter === null) {
23✔
707
            $this->outputFormatter = new OutputFormatter($this);
23✔
708
        }
709

710
        return $this->outputFormatter;
23✔
711
    }
712

713
    /**
714
     * Creates an instance of this class without any particular formatting settings.
715
     */
716
    public static function create(): self
48✔
717
    {
718
        return new OutputFormat();
48✔
719
    }
720

721
    /**
722
     * Creates an instance of this class with a preset for compact formatting.
723
     */
724
    public static function createCompact(): self
16✔
725
    {
726
        $format = self::create();
16✔
727
        $format
728
            ->setSpaceBeforeRules('')
16✔
729
            ->setSpaceBetweenRules('')
16✔
730
            ->setSpaceAfterRules('')
16✔
731
            ->setSpaceBeforeBlocks('')
16✔
732
            ->setSpaceBetweenBlocks('')
16✔
733
            ->setSpaceAfterBlocks('')
16✔
734
            ->setSpaceAfterRuleName('')
16✔
735
            ->setSpaceBeforeOpeningBrace('')
16✔
736
            ->setSpaceAfterSelectorSeparator('')
16✔
737
            ->setRenderComments(false);
16✔
738

739
        return $format;
16✔
740
    }
741

742
    /**
743
     * Creates an instance of this class with a preset for pretty formatting.
744
     */
745
    public static function createPretty(): self
16✔
746
    {
747
        $format = self::create();
16✔
748
        $format
749
            ->setSpaceBeforeRules("\n")
16✔
750
            ->setSpaceBetweenRules("\n")
16✔
751
            ->setSpaceAfterRules("\n")
16✔
752
            ->setSpaceBeforeBlocks("\n")
16✔
753
            ->setSpaceBetweenBlocks("\n\n")
16✔
754
            ->setSpaceAfterBlocks("\n")
16✔
755
            ->setSpaceAfterListArgumentSeparators([',' => ' '])
16✔
756
            ->setRenderComments(true);
16✔
757

758
        return $format;
16✔
759
    }
760
}
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