| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * PHPPgAdmin 6.1.3 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | namespace PHPPgAdmin; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use PHPMD\PHPMD; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use PHPMD\Report; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use PHPMD\Renderer\XMLRenderer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * This class will render a Java-checkstyle compatible xml-report. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * for use with cs2pr and others | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 15 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  | class CheckStyleRenderer extends XMLRenderer | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |      * Temporary property that holds the name of the last rendered file, it is | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |      * used to detect the next processed file. | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |      * @var string | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |     private $fileName; | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |      * Get a violation severity level according to the priority | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |      * of the rule that's being broken | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |      * @see https://checkstyle.sourceforge.io/version/4.4/property_types.html#severity | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |      * - priority 1 maps to error level severity | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |      * - priority 2 maps to warning level severity | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |      * - priority > 2 maps to info level severity | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |      * @param integer $priority priority of the broken rule | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |      * @return string either error, warning or info | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |     protected function mapPriorityToSeverity($priority) | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |         if ($priority>2) { | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |             return 'info'; | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |         return $priority===2? 'warning':'error'; | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |      * This method will be called when the engine has finished the source analysis | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |      * phase. | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |      * @param \PHPMD\Report $report | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |     public function renderReport(Report $report) | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |         $writer = $this->getWriter(); | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |         $writer->write('<checkstyle>'); | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |         $writer->write(\PHP_EOL); | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |         foreach ($report->getRuleViolations() as $violation) { | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |             $fileName = $violation->getFileName(); | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |             if ($this->fileName !== $fileName) { | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |                 // Not first file | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |                 if (null !== $this->fileName) { | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |                     $writer->write('  </file>' . \PHP_EOL); | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |                 // Store current file name | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |                 $this->fileName = $fileName; | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |                 $writer->write('  <file name="' . $fileName . '">' . \PHP_EOL); | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |             $rule = $violation->getRule(); | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |             $writer->write('    <error'); | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |             $writer->write(' line="' . $violation->getBeginLine() . '"'); | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |             $writer->write(' endline="' . $violation->getEndLine() . '"'); | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |             $writer->write(\sprintf(' severity="%s"', $this->mapPriorityToSeverity($rule->getPriority()))); | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |             $writer->write(\sprintf( | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |                 ' message="%s (%s, %s) "', | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |                 \htmlspecialchars($violation->getDescription()), | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |                 $rule->getName(), | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |                 $rule->getRuleSetName() | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |             )); | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |             $this->maybeAdd('package', $violation->getNamespaceName()); | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |             $this->maybeAdd('externalInfoUrl', $rule->getExternalInfoUrl()); | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |             $this->maybeAdd('function', $violation->getFunctionName()); | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |             $this->maybeAdd('class', $violation->getClassName()); | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |             $this->maybeAdd('method', $violation->getMethodName()); | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |             //$this->_maybeAdd('variable', $violation->getVariableName()); | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |             $writer->write(' />' . \PHP_EOL); | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |         // Last file and at least one violation | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |         if (null !== $this->fileName) { | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |             $writer->write('  </file>' . \PHP_EOL); | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |         foreach ($report->getErrors() as $error) { | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |             $writer->write('  <file name="' . $error->getFile() . '">'); | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |             $writer->write($error->getFile()); | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |             $writer->write('<error  msg="'); | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |             $writer->write(\htmlspecialchars($error->getMessage())); | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |             $writer->write(' severity="error" />' . \PHP_EOL); | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |         $writer->write('</checkstyle>' . \PHP_EOL); | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |         /** | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |      * This method will write a xml attribute named <b>$attr</b> to the output | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |      * when the given <b>$value</b> is not an empty string and is not <b>null</b>. | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |      * @param string $attr The xml attribute name. | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |      * @param string $value The attribute value. | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |      * @return void | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     protected function maybeAdd($attr, $value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         if ($value === null || trim($value) === '') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 121 |  |  |         $this->getWriter()->write(' ' . $attr . '="' . $value . '"'); | 
            
                                                                        
                                                                
            
                                    
            
            
                | 122 |  |  |     } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 123 |  |  | } |