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

Source for file Text.php

Documentation is available at Text.php

  1. <?php
  2. /**
  3.  * A doc generator that outputs text-based documentation.
  4.  *
  5.  * Output is designed to be displayed in a terminal and is wrapped to 100 characters.
  6.  *
  7.  * @author    Greg Sherwood <gsherwood@squiz.net>
  8.  * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
  9.  * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  10.  */
  11.  
  12. namespace PHP_CodeSniffer\Generators;
  13.  
  14. class Text extends Generator
  15. {
  16.  
  17.  
  18.     /**
  19.      * Process the documentation for a single sniff.
  20.      *
  21.      * @param \DOMNode $doc The DOMNode object for the sniff.
  22.      *                       It represents the "documentation" tag in the XML
  23.      *                       standard file.
  24.      *
  25.      * @return void 
  26.      */
  27.     public function processSniff(\DOMNode $doc)
  28.     {
  29.         $this->printTitle($doc);
  30.  
  31.         foreach ($doc->childNodes as $node) {
  32.             if ($node->nodeName === 'standard') {
  33.                 $this->printTextBlock($node);
  34.             } else if ($node->nodeName === 'code_comparison') {
  35.                 $this->printCodeComparisonBlock($node);
  36.             }
  37.         }
  38.  
  39.     }//end processSniff()
  40.  
  41.  
  42.     /**
  43.      * Prints the title area for a single sniff.
  44.      *
  45.      * @param \DOMNode $doc The DOMNode object for the sniff.
  46.      *                       It represents the "documentation" tag in the XML
  47.      *                       standard file.
  48.      *
  49.      * @return void 
  50.      */
  51.     protected function printTitle(\DOMNode $doc)
  52.     {
  53.         $title    = $this->getTitle($doc);
  54.         $standard = $this->ruleset->name;
  55.  
  56.         echo PHP_EOL;
  57.         echo str_repeat('-', (strlen("$standard CODING STANDARD: $title") + 4));
  58.         echo strtoupper(PHP_EOL."| $standard CODING STANDARD: $title |".PHP_EOL);
  59.         echo str_repeat('-', (strlen("$standard CODING STANDARD: $title") + 4));
  60.         echo PHP_EOL.PHP_EOL;
  61.  
  62.     }//end printTitle()
  63.  
  64.  
  65.     /**
  66.      * Print a text block found in a standard.
  67.      *
  68.      * @param \DOMNode $node The DOMNode object for the text block.
  69.      *
  70.      * @return void 
  71.      */
  72.     protected function printTextBlock(\DOMNode $node)
  73.     {
  74.         $text = trim($node->nodeValue);
  75.         $text = str_replace('<em>', '*', $text);
  76.         $text = str_replace('</em>', '*', $text);
  77.  
  78.         $lines    = array();
  79.         $tempLine = '';
  80.         $words    = explode(' ', $text);
  81.  
  82.         foreach ($words as $word) {
  83.             if (strlen($tempLine.$word) >= 99) {
  84.                 if (strlen($tempLine.$word) === 99) {
  85.                     // Adding the extra space will push us to the edge
  86.                     // so we are done.
  87.                     $lines[]  = $tempLine.$word;
  88.                     $tempLine = '';
  89.                 } else if (strlen($tempLine.$word) === 100) {
  90.                     // We are already at the edge, so we are done.
  91.                     $lines[]  = $tempLine.$word;
  92.                     $tempLine = '';
  93.                 } else {
  94.                     $lines[]  = rtrim($tempLine);
  95.                     $tempLine = $word.' ';
  96.                 }
  97.             } else {
  98.                 $tempLine .= $word.' ';
  99.             }
  100.         }//end foreach
  101.  
  102.         if ($tempLine !== '') {
  103.             $lines[] = rtrim($tempLine);
  104.         }
  105.  
  106.         echo implode(PHP_EOL, $lines).PHP_EOL.PHP_EOL;
  107.  
  108.     }//end printTextBlock()
  109.  
  110.  
  111.     /**
  112.      * Print a code comparison block found in a standard.
  113.      *
  114.      * @param \DOMNode $node The DOMNode object for the code comparison block.
  115.      *
  116.      * @return void 
  117.      */
  118.     protected function printCodeComparisonBlock(\DOMNode $node)
  119.     {
  120.         $codeBlocks = $node->getElementsByTagName('code');
  121.         $first      = trim($codeBlocks->item(0)->nodeValue);
  122.         $firstTitle = $codeBlocks->item(0)->getAttribute('title');
  123.  
  124.         $firstTitleLines = array();
  125.         $tempTitle       = '';
  126.         $words           = explode(' ', $firstTitle);
  127.  
  128.         foreach ($words as $word) {
  129.             if (strlen($tempTitle.$word) >= 45) {
  130.                 if (strlen($tempTitle.$word) === 45) {
  131.                     // Adding the extra space will push us to the edge
  132.                     // so we are done.
  133.                     $firstTitleLines[] = $tempTitle.$word;
  134.                     $tempTitle         = '';
  135.                 } else if (strlen($tempTitle.$word) === 46) {
  136.                     // We are already at the edge, so we are done.
  137.                     $firstTitleLines[] = $tempTitle.$word;
  138.                     $tempTitle         = '';
  139.                 } else {
  140.                     $firstTitleLines[] = $tempTitle;
  141.                     $tempTitle         = $word;
  142.                 }
  143.             } else {
  144.                 $tempTitle .= $word.' ';
  145.             }
  146.         }//end foreach
  147.  
  148.         if ($tempTitle !== '') {
  149.             $firstTitleLines[] = $tempTitle;
  150.         }
  151.  
  152.         $first      = str_replace('<em>', '', $first);
  153.         $first      = str_replace('</em>', '', $first);
  154.         $firstLines = explode("\n", $first);
  155.  
  156.         $second      = trim($codeBlocks->item(1)->nodeValue);
  157.         $secondTitle = $codeBlocks->item(1)->getAttribute('title');
  158.  
  159.         $secondTitleLines = array();
  160.         $tempTitle        = '';
  161.         $words            = explode(' ', $secondTitle);
  162.  
  163.         foreach ($words as $word) {
  164.             if (strlen($tempTitle.$word) >= 45) {
  165.                 if (strlen($tempTitle.$word) === 45) {
  166.                     // Adding the extra space will push us to the edge
  167.                     // so we are done.
  168.                     $secondTitleLines[] = $tempTitle.$word;
  169.                     $tempTitle          = '';
  170.                 } else if (strlen($tempTitle.$word) === 46) {
  171.                     // We are already at the edge, so we are done.
  172.                     $secondTitleLines[] = $tempTitle.$word;
  173.                     $tempTitle          = '';
  174.                 } else {
  175.                     $secondTitleLines[] = $tempTitle;
  176.                     $tempTitle          = $word;
  177.                 }
  178.             } else {
  179.                 $tempTitle .= $word.' ';
  180.             }
  181.         }//end foreach
  182.  
  183.         if ($tempTitle !== '') {
  184.             $secondTitleLines[] = $tempTitle;
  185.         }
  186.  
  187.         $second      = str_replace('<em>', '', $second);
  188.         $second      = str_replace('</em>', '', $second);
  189.         $secondLines = explode("\n", $second);
  190.  
  191.         $maxCodeLines  = max(count($firstLines), count($secondLines));
  192.         $maxTitleLines = max(count($firstTitleLines), count($secondTitleLines));
  193.  
  194.         echo str_repeat('-', 41);
  195.         echo ' CODE COMPARISON ';
  196.         echo str_repeat('-', 42).PHP_EOL;
  197.  
  198.         for ($i = 0; $i < $maxTitleLines; $i++) {
  199.             if (isset($firstTitleLines[$i]) === true) {
  200.                 $firstLineText = $firstTitleLines[$i];
  201.             } else {
  202.                 $firstLineText = '';
  203.             }
  204.  
  205.             if (isset($secondTitleLines[$i]) === true) {
  206.                 $secondLineText = $secondTitleLines[$i];
  207.             } else {
  208.                 $secondLineText = '';
  209.             }
  210.  
  211.             echo '| ';
  212.             echo $firstLineText.str_repeat(' ', (46 - strlen($firstLineText)));
  213.             echo ' | ';
  214.             echo $secondLineText.str_repeat(' ', (47 - strlen($secondLineText)));
  215.             echo ' |'.PHP_EOL;
  216.         }//end for
  217.  
  218.         echo str_repeat('-', 100).PHP_EOL;
  219.  
  220.         for ($i = 0; $i < $maxCodeLines; $i++) {
  221.             if (isset($firstLines[$i]) === true) {
  222.                 $firstLineText = $firstLines[$i];
  223.             } else {
  224.                 $firstLineText = '';
  225.             }
  226.  
  227.             if (isset($secondLines[$i]) === true) {
  228.                 $secondLineText = $secondLines[$i];
  229.             } else {
  230.                 $secondLineText = '';
  231.             }
  232.  
  233.             echo '| ';
  234.             echo $firstLineText.str_repeat(' ', (47 - strlen($firstLineText)));
  235.             echo '| ';
  236.             echo $secondLineText.str_repeat(' ', (48 - strlen($secondLineText)));
  237.             echo '|'.PHP_EOL;
  238.         }//end for
  239.  
  240.         echo str_repeat('-', 100).PHP_EOL.PHP_EOL;
  241.  
  242.     }//end printCodeComparisonBlock()
  243.  
  244.  
  245. }//end class

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