108
108
use SebastianBergmann \Template \Template ;
109
109
110
110
/**
111
+ * @phpstan-import-type ProcessedClassType from FileNode
112
+ * @phpstan-import-type ProcessedTraitType from FileNode
113
+ * @phpstan-import-type ProcessedMethodType from FileNode
114
+ * @phpstan-import-type ProcessedFunctionType from FileNode
115
+ *
111
116
* @internal This class is not covered by the backward compatibility promise for phpunit/php-code-coverage
112
117
*/
113
118
final class File extends Renderer
@@ -185,8 +190,13 @@ final class File extends Renderer
185
190
T_YIELD => true ,
186
191
T_YIELD_FROM => true ,
187
192
];
193
+
194
+ private const int HTML_SPECIAL_CHARS_FLAGS = ENT_COMPAT | ENT_HTML401 | ENT_SUBSTITUTE ;
195
+
196
+ /**
197
+ * @var array<non-empty-string, list<string>>
198
+ */
188
199
private static array $ formattedSourceCache = [];
189
- private int $ htmlSpecialCharsFlags = ENT_COMPAT | ENT_HTML401 | ENT_SUBSTITUTE ;
190
200
191
201
public function render (FileNode $ node , string $ file ): void
192
202
{
@@ -314,6 +324,9 @@ private function renderItems(FileNode $node): string
314
324
return $ items ;
315
325
}
316
326
327
+ /**
328
+ * @param array<string, ProcessedClassType|ProcessedTraitType> $items
329
+ */
317
330
private function renderTraitOrClassItems (array $ items , Template $ template , Template $ methodItemTemplate ): string
318
331
{
319
332
$ buffer = '' ;
@@ -418,6 +431,9 @@ private function renderTraitOrClassItems(array $items, Template $template, Templ
418
431
return $ buffer ;
419
432
}
420
433
434
+ /**
435
+ * @param array<string, ProcessedFunctionType> $functions
436
+ */
421
437
private function renderFunctionItems (array $ functions , Template $ template ): string
422
438
{
423
439
if (empty ($ functions )) {
@@ -436,6 +452,9 @@ private function renderFunctionItems(array $functions, Template $template): stri
436
452
return $ buffer ;
437
453
}
438
454
455
+ /**
456
+ * @param ProcessedFunctionType|ProcessedMethodType $item
457
+ */
439
458
private function renderFunctionOrMethodItem (Template $ template , array $ item , string $ indent = '' ): string
440
459
{
441
460
$ numMethods = 0 ;
@@ -476,7 +495,7 @@ private function renderFunctionOrMethodItem(Template $template, array $item, str
476
495
'%s<a href="#%d"><abbr title="%s">%s</abbr></a> ' ,
477
496
$ indent ,
478
497
$ item ['startLine ' ],
479
- htmlspecialchars ($ item ['signature ' ], $ this -> htmlSpecialCharsFlags ),
498
+ htmlspecialchars ($ item ['signature ' ], self :: HTML_SPECIAL_CHARS_FLAGS ),
480
499
$ item ['functionName ' ] ?? $ item ['methodName ' ],
481
500
),
482
501
'numMethods ' => $ numMethods ,
@@ -554,7 +573,7 @@ private function renderSourceWithLineCoverage(FileNode $node): string
554
573
$ popover = sprintf (
555
574
' data-bs-title="%s" data-bs-content="%s" data-bs-placement="top" data-bs-html="true" ' ,
556
575
$ popoverTitle ,
557
- htmlspecialchars ($ popoverContent , $ this -> htmlSpecialCharsFlags ),
576
+ htmlspecialchars ($ popoverContent , self :: HTML_SPECIAL_CHARS_FLAGS ),
558
577
);
559
578
}
560
579
@@ -641,7 +660,7 @@ private function renderSourceWithBranchCoverage(FileNode $node): string
641
660
$ popover = sprintf (
642
661
' data-bs-title="%s" data-bs-content="%s" data-bs-placement="top" data-bs-html="true" ' ,
643
662
$ popoverTitle ,
644
- htmlspecialchars ($ popoverContent , $ this -> htmlSpecialCharsFlags ),
663
+ htmlspecialchars ($ popoverContent , self :: HTML_SPECIAL_CHARS_FLAGS ),
645
664
);
646
665
}
647
666
@@ -731,7 +750,7 @@ private function renderSourceWithPathCoverage(FileNode $node): string
731
750
$ popover = sprintf (
732
751
' data-bs-title="%s" data-bs-content="%s" data-bs-placement="top" data-bs-html="true" ' ,
733
752
$ popoverTitle ,
734
- htmlspecialchars ($ popoverContent , $ this -> htmlSpecialCharsFlags ),
753
+ htmlspecialchars ($ popoverContent , self :: HTML_SPECIAL_CHARS_FLAGS ),
735
754
);
736
755
}
737
756
@@ -768,7 +787,7 @@ private function renderBranchStructure(FileNode $node): string
768
787
}
769
788
770
789
if ($ branchStructure !== '' ) { // don't show empty branches
771
- $ branches .= '<h5 class="structure-heading"><a name=" ' . htmlspecialchars ($ methodName , $ this -> htmlSpecialCharsFlags ) . '"> ' . $ this ->abbreviateMethodName ($ methodName ) . '</a></h5> ' . "\n" ;
790
+ $ branches .= '<h5 class="structure-heading"><a name=" ' . htmlspecialchars ($ methodName , self :: HTML_SPECIAL_CHARS_FLAGS ) . '"> ' . $ this ->abbreviateMethodName ($ methodName ) . '</a></h5> ' . "\n" ;
772
791
$ branches .= $ branchStructure ;
773
792
}
774
793
}
@@ -778,6 +797,9 @@ private function renderBranchStructure(FileNode $node): string
778
797
return $ branchesTemplate ->render ();
779
798
}
780
799
800
+ /**
801
+ * @param list<string> $codeLines
802
+ */
781
803
private function renderBranchLines (array $ branch , array $ codeLines , array $ testData ): string
782
804
{
783
805
$ linesTemplate = new Template ($ this ->templatePath . 'lines.html.dist ' , '{{ ' , '}} ' );
@@ -829,7 +851,7 @@ private function renderBranchLines(array $branch, array $codeLines, array $testD
829
851
$ popover = sprintf (
830
852
' data-bs-title="%s" data-bs-content="%s" data-bs-placement="top" data-bs-html="true" ' ,
831
853
$ popoverTitle ,
832
- htmlspecialchars ($ popoverContent , $ this -> htmlSpecialCharsFlags ),
854
+ htmlspecialchars ($ popoverContent , self :: HTML_SPECIAL_CHARS_FLAGS ),
833
855
);
834
856
}
835
857
@@ -874,7 +896,7 @@ private function renderPathStructure(FileNode $node): string
874
896
}
875
897
876
898
if ($ pathStructure !== '' ) {
877
- $ paths .= '<h5 class="structure-heading"><a name=" ' . htmlspecialchars ($ methodName , $ this -> htmlSpecialCharsFlags ) . '"> ' . $ this ->abbreviateMethodName ($ methodName ) . '</a></h5> ' . "\n" ;
899
+ $ paths .= '<h5 class="structure-heading"><a name=" ' . htmlspecialchars ($ methodName , self :: HTML_SPECIAL_CHARS_FLAGS ) . '"> ' . $ this ->abbreviateMethodName ($ methodName ) . '</a></h5> ' . "\n" ;
878
900
$ paths .= $ pathStructure ;
879
901
}
880
902
}
@@ -884,6 +906,9 @@ private function renderPathStructure(FileNode $node): string
884
906
return $ pathsTemplate ->render ();
885
907
}
886
908
909
+ /**
910
+ * @param list<string> $codeLines
911
+ */
887
912
private function renderPathLines (array $ path , array $ branches , array $ codeLines , array $ testData ): string
888
913
{
889
914
$ linesTemplate = new Template ($ this ->templatePath . 'lines.html.dist ' , '{{ ' , '}} ' );
@@ -944,7 +969,7 @@ private function renderPathLines(array $path, array $branches, array $codeLines,
944
969
$ popover = sprintf (
945
970
' data-bs-title="%s" data-bs-content="%s" data-bs-placement="top" data-bs-html="true" ' ,
946
971
$ popoverTitle ,
947
- htmlspecialchars ($ popoverContent , $ this -> htmlSpecialCharsFlags ),
972
+ htmlspecialchars ($ popoverContent , self :: HTML_SPECIAL_CHARS_FLAGS ),
948
973
);
949
974
}
950
975
@@ -975,6 +1000,11 @@ private function renderLine(Template $template, int $lineNumber, string $lineCon
975
1000
return $ template ->render ();
976
1001
}
977
1002
1003
+ /**
1004
+ * @param non-empty-string $file
1005
+ *
1006
+ * @return list<string>
1007
+ */
978
1008
private function loadFile (string $ file ): array
979
1009
{
980
1010
if (isset (self ::$ formattedSourceCache [$ file ])) {
@@ -995,14 +1025,14 @@ private function loadFile(string $file): array
995
1025
if ($ token === '" ' && $ tokens [$ j - 1 ] !== '\\' ) {
996
1026
$ result [$ i ] .= sprintf (
997
1027
'<span class="string">%s</span> ' ,
998
- htmlspecialchars ($ token , $ this -> htmlSpecialCharsFlags ),
1028
+ htmlspecialchars ($ token , self :: HTML_SPECIAL_CHARS_FLAGS ),
999
1029
);
1000
1030
1001
1031
$ stringFlag = !$ stringFlag ;
1002
1032
} else {
1003
1033
$ result [$ i ] .= sprintf (
1004
1034
'<span class="keyword">%s</span> ' ,
1005
- htmlspecialchars ($ token , $ this -> htmlSpecialCharsFlags ),
1035
+ htmlspecialchars ($ token , self :: HTML_SPECIAL_CHARS_FLAGS ),
1006
1036
);
1007
1037
}
1008
1038
@@ -1014,7 +1044,7 @@ private function loadFile(string $file): array
1014
1044
$ value = str_replace (
1015
1045
["\t" , ' ' ],
1016
1046
[' ' , ' ' ],
1017
- htmlspecialchars ($ value , $ this -> htmlSpecialCharsFlags ),
1047
+ htmlspecialchars ($ value , self :: HTML_SPECIAL_CHARS_FLAGS ),
1018
1048
);
1019
1049
1020
1050
if ($ value === "\n" ) {
@@ -1113,7 +1143,7 @@ private function createPopoverContentForTest(string $test, array $testData): str
1113
1143
return sprintf (
1114
1144
'<li%s>%s</li> ' ,
1115
1145
$ testCSS ,
1116
- htmlspecialchars ($ test , $ this -> htmlSpecialCharsFlags ),
1146
+ htmlspecialchars ($ test , self :: HTML_SPECIAL_CHARS_FLAGS ),
1117
1147
);
1118
1148
}
1119
1149
0 commit comments