108108use SebastianBergmann \Template \Template ;
109109
110110/**
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+ *
111116 * @internal This class is not covered by the backward compatibility promise for phpunit/php-code-coverage
112117 */
113118final class File extends Renderer
@@ -185,8 +190,13 @@ final class File extends Renderer
185190 T_YIELD => true ,
186191 T_YIELD_FROM => true ,
187192 ];
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+ */
188199 private static array $ formattedSourceCache = [];
189- private int $ htmlSpecialCharsFlags = ENT_COMPAT | ENT_HTML401 | ENT_SUBSTITUTE ;
190200
191201 public function render (FileNode $ node , string $ file ): void
192202 {
@@ -314,6 +324,9 @@ private function renderItems(FileNode $node): string
314324 return $ items ;
315325 }
316326
327+ /**
328+ * @param array<string, ProcessedClassType|ProcessedTraitType> $items
329+ */
317330 private function renderTraitOrClassItems (array $ items , Template $ template , Template $ methodItemTemplate ): string
318331 {
319332 $ buffer = '' ;
@@ -418,6 +431,9 @@ private function renderTraitOrClassItems(array $items, Template $template, Templ
418431 return $ buffer ;
419432 }
420433
434+ /**
435+ * @param array<string, ProcessedFunctionType> $functions
436+ */
421437 private function renderFunctionItems (array $ functions , Template $ template ): string
422438 {
423439 if (empty ($ functions )) {
@@ -436,6 +452,9 @@ private function renderFunctionItems(array $functions, Template $template): stri
436452 return $ buffer ;
437453 }
438454
455+ /**
456+ * @param ProcessedFunctionType|ProcessedMethodType $item
457+ */
439458 private function renderFunctionOrMethodItem (Template $ template , array $ item , string $ indent = '' ): string
440459 {
441460 $ numMethods = 0 ;
@@ -476,7 +495,7 @@ private function renderFunctionOrMethodItem(Template $template, array $item, str
476495 '%s<a href="#%d"><abbr title="%s">%s</abbr></a> ' ,
477496 $ indent ,
478497 $ item ['startLine ' ],
479- htmlspecialchars ($ item ['signature ' ], $ this -> htmlSpecialCharsFlags ),
498+ htmlspecialchars ($ item ['signature ' ], self :: HTML_SPECIAL_CHARS_FLAGS ),
480499 $ item ['functionName ' ] ?? $ item ['methodName ' ],
481500 ),
482501 'numMethods ' => $ numMethods ,
@@ -554,7 +573,7 @@ private function renderSourceWithLineCoverage(FileNode $node): string
554573 $ popover = sprintf (
555574 ' data-bs-title="%s" data-bs-content="%s" data-bs-placement="top" data-bs-html="true" ' ,
556575 $ popoverTitle ,
557- htmlspecialchars ($ popoverContent , $ this -> htmlSpecialCharsFlags ),
576+ htmlspecialchars ($ popoverContent , self :: HTML_SPECIAL_CHARS_FLAGS ),
558577 );
559578 }
560579
@@ -641,7 +660,7 @@ private function renderSourceWithBranchCoverage(FileNode $node): string
641660 $ popover = sprintf (
642661 ' data-bs-title="%s" data-bs-content="%s" data-bs-placement="top" data-bs-html="true" ' ,
643662 $ popoverTitle ,
644- htmlspecialchars ($ popoverContent , $ this -> htmlSpecialCharsFlags ),
663+ htmlspecialchars ($ popoverContent , self :: HTML_SPECIAL_CHARS_FLAGS ),
645664 );
646665 }
647666
@@ -731,7 +750,7 @@ private function renderSourceWithPathCoverage(FileNode $node): string
731750 $ popover = sprintf (
732751 ' data-bs-title="%s" data-bs-content="%s" data-bs-placement="top" data-bs-html="true" ' ,
733752 $ popoverTitle ,
734- htmlspecialchars ($ popoverContent , $ this -> htmlSpecialCharsFlags ),
753+ htmlspecialchars ($ popoverContent , self :: HTML_SPECIAL_CHARS_FLAGS ),
735754 );
736755 }
737756
@@ -768,7 +787,7 @@ private function renderBranchStructure(FileNode $node): string
768787 }
769788
770789 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" ;
772791 $ branches .= $ branchStructure ;
773792 }
774793 }
@@ -778,6 +797,9 @@ private function renderBranchStructure(FileNode $node): string
778797 return $ branchesTemplate ->render ();
779798 }
780799
800+ /**
801+ * @param list<string> $codeLines
802+ */
781803 private function renderBranchLines (array $ branch , array $ codeLines , array $ testData ): string
782804 {
783805 $ linesTemplate = new Template ($ this ->templatePath . 'lines.html.dist ' , '{{ ' , '}} ' );
@@ -829,7 +851,7 @@ private function renderBranchLines(array $branch, array $codeLines, array $testD
829851 $ popover = sprintf (
830852 ' data-bs-title="%s" data-bs-content="%s" data-bs-placement="top" data-bs-html="true" ' ,
831853 $ popoverTitle ,
832- htmlspecialchars ($ popoverContent , $ this -> htmlSpecialCharsFlags ),
854+ htmlspecialchars ($ popoverContent , self :: HTML_SPECIAL_CHARS_FLAGS ),
833855 );
834856 }
835857
@@ -874,7 +896,7 @@ private function renderPathStructure(FileNode $node): string
874896 }
875897
876898 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" ;
878900 $ paths .= $ pathStructure ;
879901 }
880902 }
@@ -884,6 +906,9 @@ private function renderPathStructure(FileNode $node): string
884906 return $ pathsTemplate ->render ();
885907 }
886908
909+ /**
910+ * @param list<string> $codeLines
911+ */
887912 private function renderPathLines (array $ path , array $ branches , array $ codeLines , array $ testData ): string
888913 {
889914 $ linesTemplate = new Template ($ this ->templatePath . 'lines.html.dist ' , '{{ ' , '}} ' );
@@ -944,7 +969,7 @@ private function renderPathLines(array $path, array $branches, array $codeLines,
944969 $ popover = sprintf (
945970 ' data-bs-title="%s" data-bs-content="%s" data-bs-placement="top" data-bs-html="true" ' ,
946971 $ popoverTitle ,
947- htmlspecialchars ($ popoverContent , $ this -> htmlSpecialCharsFlags ),
972+ htmlspecialchars ($ popoverContent , self :: HTML_SPECIAL_CHARS_FLAGS ),
948973 );
949974 }
950975
@@ -975,6 +1000,11 @@ private function renderLine(Template $template, int $lineNumber, string $lineCon
9751000 return $ template ->render ();
9761001 }
9771002
1003+ /**
1004+ * @param non-empty-string $file
1005+ *
1006+ * @return list<string>
1007+ */
9781008 private function loadFile (string $ file ): array
9791009 {
9801010 if (isset (self ::$ formattedSourceCache [$ file ])) {
@@ -995,14 +1025,14 @@ private function loadFile(string $file): array
9951025 if ($ token === '" ' && $ tokens [$ j - 1 ] !== '\\' ) {
9961026 $ result [$ i ] .= sprintf (
9971027 '<span class="string">%s</span> ' ,
998- htmlspecialchars ($ token , $ this -> htmlSpecialCharsFlags ),
1028+ htmlspecialchars ($ token , self :: HTML_SPECIAL_CHARS_FLAGS ),
9991029 );
10001030
10011031 $ stringFlag = !$ stringFlag ;
10021032 } else {
10031033 $ result [$ i ] .= sprintf (
10041034 '<span class="keyword">%s</span> ' ,
1005- htmlspecialchars ($ token , $ this -> htmlSpecialCharsFlags ),
1035+ htmlspecialchars ($ token , self :: HTML_SPECIAL_CHARS_FLAGS ),
10061036 );
10071037 }
10081038
@@ -1014,7 +1044,7 @@ private function loadFile(string $file): array
10141044 $ value = str_replace (
10151045 ["\t" , ' ' ],
10161046 [' ' , ' ' ],
1017- htmlspecialchars ($ value , $ this -> htmlSpecialCharsFlags ),
1047+ htmlspecialchars ($ value , self :: HTML_SPECIAL_CHARS_FLAGS ),
10181048 );
10191049
10201050 if ($ value === "\n" ) {
@@ -1113,7 +1143,7 @@ private function createPopoverContentForTest(string $test, array $testData): str
11131143 return sprintf (
11141144 '<li%s>%s</li> ' ,
11151145 $ testCSS ,
1116- htmlspecialchars ($ test , $ this -> htmlSpecialCharsFlags ),
1146+ htmlspecialchars ($ test , self :: HTML_SPECIAL_CHARS_FLAGS ),
11171147 );
11181148 }
11191149
0 commit comments