PHP_CodeSniffer
[ class tree: PHP_CodeSniffer ] [ index: PHP_CodeSniffer ] [ all elements ]

Source for file HTML.php

Documentation is available at HTML.php

  1. <?php
  2. /**
  3.  * A doc generator that outputs documentation in one big HTML file.
  4.  *
  5.  * Output is in one large HTML file and is designed for you to style with
  6.  * your own stylesheet. It contains a table of contents at the top with anchors
  7.  * to each sniff.
  8.  *
  9.  * @author    Greg Sherwood <gsherwood@squiz.net>
  10.  * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
  11.  * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  12.  */
  13.  
  14. namespace PHP_CodeSniffer\Generators;
  15.  
  16. use PHP_CodeSniffer\Config;
  17.  
  18. class HTML extends Generator
  19. {
  20.  
  21.  
  22.     /**
  23.      * Generates the documentation for a standard.
  24.      *
  25.      * @return void 
  26.      * @see    processSniff()
  27.      */
  28.     public function generate()
  29.     {
  30.         ob_start();
  31.         $this->printHeader();
  32.         $this->printToc();
  33.  
  34.         foreach ($this->docFiles as $file{
  35.             $doc = new \DOMDocument();
  36.             $doc->load($file);
  37.             $documentation $doc->getElementsByTagName('documentation')->item(0);
  38.             $this->processSniff($documentation);
  39.         }
  40.  
  41.         $this->printFooter();
  42.  
  43.         $content ob_get_contents();
  44.         ob_end_clean();
  45.  
  46.         echo $content;
  47.  
  48.     }//end generate()
  49.  
  50.  
  51.     /**
  52.      * Print the header of the HTML page.
  53.      *
  54.      * @return void 
  55.      */
  56.     protected function printHeader()
  57.     {
  58.         $standard $this->ruleset->name;
  59.         echo '<html>'.PHP_EOL;
  60.         echo ' <head>'.PHP_EOL;
  61.         echo "  <title>$standard Coding Standards</title>".PHP_EOL;
  62.         echo '  <style>
  63.                     body {
  64.                         background-color: #FFFFFF;
  65.                         font-size: 14px;
  66.                         font-family: Arial, Helvetica, sans-serif;
  67.                         color: #000000;
  68.                     }
  69.  
  70.                     h1 {
  71.                         color: #666666;
  72.                         font-size: 20px;
  73.                         font-weight: bold;
  74.                         margin-top: 0px;
  75.                         background-color: #E6E7E8;
  76.                         padding: 20px;
  77.                         border: 1px solid #BBBBBB;
  78.                     }
  79.  
  80.                     h2 {
  81.                         color: #00A5E3;
  82.                         font-size: 16px;
  83.                         font-weight: normal;
  84.                         margin-top: 50px;
  85.                     }
  86.  
  87.                     .code-comparison {
  88.                         width: 100%;
  89.                     }
  90.  
  91.                     .code-comparison td {
  92.                         border: 1px solid #CCCCCC;
  93.                     }
  94.  
  95.                     .code-comparison-title, .code-comparison-code {
  96.                         font-family: Arial, Helvetica, sans-serif;
  97.                         font-size: 12px;
  98.                         color: #000000;
  99.                         vertical-align: top;
  100.                         padding: 4px;
  101.                         width: 50%;
  102.                         background-color: #F1F1F1;
  103.                         line-height: 15px;
  104.                     }
  105.  
  106.                     .code-comparison-code {
  107.                         font-family: Courier;
  108.                         background-color: #F9F9F9;
  109.                     }
  110.  
  111.                     .code-comparison-highlight {
  112.                         background-color: #DDF1F7;
  113.                         border: 1px solid #00A5E3;
  114.                         line-height: 15px;
  115.                     }
  116.  
  117.                     .tag-line {
  118.                         text-align: center;
  119.                         width: 100%;
  120.                         margin-top: 30px;
  121.                         font-size: 12px;
  122.                     }
  123.  
  124.                     .tag-line a {
  125.                         color: #000000;
  126.                     }
  127.                 </style>'.PHP_EOL;
  128.         echo ' </head>'.PHP_EOL;
  129.         echo ' <body>'.PHP_EOL;
  130.         echo "  <h1>$standard Coding Standards</h1>".PHP_EOL;
  131.  
  132.     }//end printHeader()
  133.  
  134.  
  135.     /**
  136.      * Print the table of contents for the standard.
  137.      *
  138.      * The TOC is just an unordered list of bookmarks to sniffs on the page.
  139.      *
  140.      * @return void 
  141.      */
  142.     protected function printToc()
  143.     {
  144.         echo '  <h2>Table of Contents</h2>'.PHP_EOL;
  145.         echo '  <ul class="toc">'.PHP_EOL;
  146.  
  147.         foreach ($this->docFiles as $file{
  148.             $doc = new \DOMDocument();
  149.             $doc->load($file);
  150.             $documentation $doc->getElementsByTagName('documentation')->item(0);
  151.             $title         $this->getTitle($documentation);
  152.             echo '   <li><a href="#'.str_replace(' ''-'$title)."\">$title</a></li>".PHP_EOL;
  153.         }
  154.  
  155.         echo '  </ul>'.PHP_EOL;
  156.  
  157.     }//end printToc()
  158.  
  159.  
  160.     /**
  161.      * Print the footer of the HTML page.
  162.      *
  163.      * @return void 
  164.      */
  165.     protected function printFooter()
  166.     {
  167.         // Turn off errors so we don't get timezone warnings if people
  168.         // don't have their timezone set.
  169.         $errorLevel error_reporting(0);
  170.         echo '  <div class="tag-line">';
  171.         echo 'Documentation generated on '.date('r');
  172.         echo ' by <a href="https://github.com/squizlabs/PHP_CodeSniffer">PHP_CodeSniffer '.Config::VERSION.'</a>';
  173.         echo '</div>'.PHP_EOL;
  174.         error_reporting($errorLevel);
  175.  
  176.         echo ' </body>'.PHP_EOL;
  177.         echo '</html>'.PHP_EOL;
  178.  
  179.     }//end printFooter()
  180.  
  181.  
  182.     /**
  183.      * Process the documentation for a single sniff.
  184.      *
  185.      * @param \DOMNode $doc The DOMNode object for the sniff.
  186.      *                       It represents the "documentation" tag in the XML
  187.      *                       standard file.
  188.      *
  189.      * @return void 
  190.      */
  191.     public function processSniff(\DOMNode $doc)
  192.     {
  193.         $title $this->getTitle($doc);
  194.         echo '  <a name="'.str_replace(' ''-'$title).'" />'.PHP_EOL;
  195.         echo "  <h2>$title</h2>".PHP_EOL;
  196.  
  197.         foreach ($doc->childNodes as $node{
  198.             if ($node->nodeName === 'standard'{
  199.                 $this->printTextBlock($node);
  200.             else if ($node->nodeName === 'code_comparison'{
  201.                 $this->printCodeComparisonBlock($node);
  202.             }
  203.         }
  204.  
  205.     }//end processSniff()
  206.  
  207.  
  208.     /**
  209.      * Print a text block found in a standard.
  210.      *
  211.      * @param \DOMNode $node The DOMNode object for the text block.
  212.      *
  213.      * @return void 
  214.      */
  215.     protected function printTextBlock(\DOMNode $node)
  216.     {
  217.         $content trim($node->nodeValue);
  218.         $content htmlspecialchars($content);
  219.  
  220.         // Allow em tags only.
  221.         $content str_replace('&lt;em&gt;''<em>'$content);
  222.         $content str_replace('&lt;/em&gt;''</em>'$content);
  223.  
  224.         echo "  <p class=\"text\">$content</p>".PHP_EOL;
  225.  
  226.     }//end printTextBlock()
  227.  
  228.  
  229.     /**
  230.      * Print a code comparison block found in a standard.
  231.      *
  232.      * @param \DOMNode $node The DOMNode object for the code comparison block.
  233.      *
  234.      * @return void 
  235.      */
  236.     protected function printCodeComparisonBlock(\DOMNode $node)
  237.     {
  238.         $codeBlocks $node->getElementsByTagName('code');
  239.  
  240.         $firstTitle $codeBlocks->item(0)->getAttribute('title');
  241.         $first      trim($codeBlocks->item(0)->nodeValue);
  242.         $first      str_replace('<?php''&lt;?php'$first);
  243.         $first      str_replace("\n"'</br>'$first);
  244.         $first      str_replace(' ''&nbsp;'$first);
  245.         $first      str_replace('<em>''<span class="code-comparison-highlight">'$first);
  246.         $first      str_replace('</em>''</span>'$first);
  247.  
  248.         $secondTitle $codeBlocks->item(1)->getAttribute('title');
  249.         $second      trim($codeBlocks->item(1)->nodeValue);
  250.         $second      str_replace('<?php''&lt;?php'$second);
  251.         $second      str_replace("\n"'</br>'$second);
  252.         $second      str_replace(' ''&nbsp;'$second);
  253.         $second      str_replace('<em>''<span class="code-comparison-highlight">'$second);
  254.         $second      str_replace('</em>''</span>'$second);
  255.  
  256.         echo '  <table class="code-comparison">'.PHP_EOL;
  257.         echo '   <tr>'.PHP_EOL;
  258.         echo "    <td class=\"code-comparison-title\">$firstTitle</td>".PHP_EOL;
  259.         echo "    <td class=\"code-comparison-title\">$secondTitle</td>".PHP_EOL;
  260.         echo '   </tr>'.PHP_EOL;
  261.         echo '   <tr>'.PHP_EOL;
  262.         echo "    <td class=\"code-comparison-code\">$first</td>".PHP_EOL;
  263.         echo "    <td class=\"code-comparison-code\">$second</td>".PHP_EOL;
  264.         echo '   </tr>'.PHP_EOL;
  265.         echo '  </table>'.PHP_EOL;
  266.  
  267.     }//end printCodeComparisonBlock()
  268.  
  269.  
  270. }//end class

Documentation generated on Mon, 11 Mar 2019 15:27:33 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.