MediaWiki  REL1_31
postprocess-phan.php
Go to the documentation of this file.
1 <?php
2 
3 abstract 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 
24 class TextSuppressor extends Suppressor {
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 
107 class NoopSuppressor extends Suppressor {
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
121 if ( isset( $opt['m'] ) ) {
122  $mode = $opt['m'];
123 } elseif ( isset( $mode['output-mode'] ) ) {
124  $mode = $opt['output-mode'];
125 } else {
126  $mode = 'text';
127 }
128 if ( 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 
134 switch ( $mode ) {
135 case 'text':
136  $suppressor = new TextSuppressor();
137  break;
138 case 'checkstyle':
139  $suppressor = new CheckStyleSuppressor();
140  break;
141 default:
142  $suppressor = new NoopSuppressor( $mode );
143 }
144 
145 $input = file_get_contents( 'php://stdin' );
146 $hasErrors = $suppressor->suppress( $input );
147 
148 if ( $hasErrors ) {
149  exit( 1 );
150 }
array
the array() calling protocol came about after MediaWiki 1.4rc1.
$opt
$opt
Definition: postprocess-phan.php:119
CheckStyleSuppressor
Definition: postprocess-phan.php:62
CheckStyleSuppressor\suppress
suppress( $input)
Definition: postprocess-phan.php:67
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:37
NoopSuppressor\suppress
suppress( $input)
Definition: postprocess-phan.php:113
file
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
$hasErrors
$hasErrors
Definition: postprocess-phan.php:146
NoopSuppressor\__construct
__construct( $mode)
Definition: postprocess-phan.php:110
$input
if(is_array( $mode)) switch( $mode) $input
Definition: postprocess-phan.php:145
$matches
$matches
Definition: NoLocalSettings.php:24
NoopSuppressor\$mode
$mode
Definition: postprocess-phan.php:108
list
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
Suppressor\isSuppressed
isSuppressed(array $source, $type, $lineno)
Definition: postprocess-phan.php:16
NoopSuppressor
Definition: postprocess-phan.php:107
TextSuppressor\suppress
suppress( $input)
Definition: postprocess-phan.php:29
Suppressor\suppress
suppress( $input)
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:22
$source
$source
Definition: mwdoc-filter.php:46
TextSuppressor
Definition: postprocess-phan.php:24
Suppressor
Definition: postprocess-phan.php:3
$type
$type
Definition: testCompression.php:48