MediaWiki REL1_31
postprocess-phan.php
Go to the documentation of this file.
1<?php
2
3abstract class Suppressor {
8 abstract public function suppress( $input );
9
16 protected function isSuppressed( array $source, $type, $lineno ) {
17 return $lineno > 0 && preg_match(
18 "|/\*\* @suppress {$type} |",
19 $source[$lineno - 1]
20 );
21 }
22}
23
29 public function suppress( $input ) {
30 $hasErrors = false;
31 $errors = [];
32 foreach ( explode( "\n", $input ) as $error ) {
33 if ( empty( $error ) ) {
34 continue;
35 }
36 if ( !preg_match( '/^(.*):(\d+) (Phan\w+) (.*)$/', $error, $matches ) ) {
37 echo "Failed to parse line: $error\n";
38 continue;
39 }
40 list( $source, $file, $lineno, $type, $message ) = $matches;
41 $errors[$file][] = [
42 'orig' => $error,
43 // convert from 1 indexed to 0 indexed
44 'lineno' => $lineno - 1,
45 'type' => $type,
46 ];
47 }
48 foreach ( $errors as $file => $fileErrors ) {
49 $source = file( $file );
50 foreach ( $fileErrors as $error ) {
51 if ( !$this->isSuppressed( $source, $error['type'], $error['lineno'] ) ) {
52 echo $error['orig'], "\n";
53 $hasErrors = true;
54 }
55 }
56 }
57
58 return $hasErrors;
59 }
60}
61
67 public function suppress( $input ) {
68 $dom = new DOMDocument();
69
70 $oldDisable = libxml_disable_entity_loader( true );
71 $dom->loadXML( $input );
72
73 $hasErrors = false;
74 // DOMNodeList's are "live", convert to an array so it works as expected
75 $files = [];
76 foreach ( $dom->getElementsByTagName( 'file' ) as $file ) {
77 $files[] = $file;
78 }
79 foreach ( $files as $file ) {
80 $errors = [];
81 foreach ( $file->getElementsByTagName( 'error' ) as $error ) {
82 $errors[] = $error;
83 }
84 $source = file( $file->getAttribute( 'name' ) );
85 $fileHasErrors = false;
86 foreach ( $errors as $error ) {
87 $lineno = $error->getAttribute( 'line' ) - 1;
88 $type = $error->getAttribute( 'source' );
89 if ( $this->isSuppressed( $source, $type, $lineno ) ) {
90 $error->parentNode->removeChild( $error );
91 } else {
92 $fileHasErrors = true;
93 $hasErrors = true;
94 }
95 }
96 if ( !$fileHasErrors ) {
97 $file->parentNode->removeChild( $file );
98 }
99 }
100 echo $dom->saveXML();
101 libxml_disable_entity_loader( $oldDisable );
102
103 return $hasErrors;
104 }
105}
106
108 private $mode;
109
110 public function __construct( $mode ) {
111 $this->mode = $mode;
112 }
113 public function suppress( $input ) {
114 echo "Unsupported output mode: {$this->mode}\n$input";
115 return true;
116 }
117}
118
119$opt = getopt( "m:", [ "output-mode:" ] );
120// if provided multiple times getopt returns an array
121if ( isset( $opt['m'] ) ) {
122 $mode = $opt['m'];
123} elseif ( isset( $mode['output-mode'] ) ) {
124 $mode = $opt['output-mode'];
125} else {
126 $mode = 'text';
127}
128if ( is_array( $mode ) ) {
129 // If an option is passed multiple times getopt returns an
130 // array. Just take the last one.
131 $mode = end( $mode );
132}
133
134switch ( $mode ) {
135case 'text':
136 $suppressor = new TextSuppressor();
137 break;
138case 'checkstyle':
139 $suppressor = new CheckStyleSuppressor();
140 break;
141default:
142 $suppressor = new NoopSuppressor( $mode );
143}
144
145$input = file_get_contents( 'php://stdin' );
146$hasErrors = $suppressor->suppress( $input );
147
148if ( $hasErrors ) {
149 exit( 1 );
150}
suppress( $input)
isSuppressed(array $source, $type, $lineno)
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition deferred.txt:11
We ve cleaned up the code here by removing clumps of infrequently used code and moving them off somewhere else It s much easier for someone working with this code to see what s _really_ going and make changes or fix bugs In we can take all the code that deals with the little used title reversing we can concentrate it all in an extension file
Definition hooks.txt:106
$source
if(is_array($mode)) switch( $mode) $input