summaryrefslogtreecommitdiff
path: root/www/wiki/tests/phan/bin/postprocess-phan.php
blob: 3e8059867757b02c08cd783e54b807e93d1b477e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php

abstract class Suppressor {
	/**
	 * @param string $input
	 * @return bool do errors remain
	 */
	abstract public function suppress( $input );

	/**
	 * @param string[] $source
	 * @param string $type
	 * @param int $lineno
	 * @return bool
	 */
	protected function isSuppressed( array $source, $type, $lineno ) {
		return $lineno > 0 && preg_match(
			"|/\*\* @suppress {$type} |",
			$source[$lineno - 1]
		);
	}
}

class TextSuppressor extends Suppressor {
	/**
	 * @param string $input
	 * @return bool do errors remain
	 */
	public function suppress( $input ) {
		$hasErrors = false;
		$errors = [];
		foreach ( explode( "\n", $input ) as $error ) {
			if ( empty( $error ) ) {
				continue;
			}
			if ( !preg_match( '/^(.*):(\d+) (Phan\w+) (.*)$/', $error, $matches ) ) {
				echo "Failed to parse line: $error\n";
				continue;
			}
			list( $source, $file, $lineno, $type, $message ) = $matches;
			$errors[$file][] = [
				'orig' => $error,
				// convert from 1 indexed to 0 indexed
				'lineno' => $lineno - 1,
				'type' => $type,
			];
		}
		foreach ( $errors  as $file => $fileErrors ) {
			$source = file( $file );
			foreach ( $fileErrors as $error ) {
				if ( !$this->isSuppressed( $source, $error['type'], $error['lineno'] ) ) {
					echo $error['orig'], "\n";
					$hasErrors = true;
				}
			}
		}

		return $hasErrors;
	}
}

class CheckStyleSuppressor extends Suppressor {
	/**
	 * @param string $input
	 * @return bool True do errors remain
	 */
	public function suppress( $input ) {
		$dom = new DOMDocument();
		$dom->loadXML( $input );
		$hasErrors = false;
		// DOMNodeList's are "live", convert to an array so it works as expected
		$files = [];
		foreach ( $dom->getElementsByTagName( 'file' ) as $file ) {
			$files[] = $file;
		}
		foreach ( $files as $file ) {
			$errors = [];
			foreach ( $file->getElementsByTagName( 'error' ) as $error ) {
				$errors[] = $error;
			}
			$source = file( $file->getAttribute( 'name' ) );
			$fileHasErrors = false;
			foreach ( $errors as $error ) {
				$lineno = $error->getAttribute( 'line' ) - 1;
				$type = $error->getAttribute( 'source' );
				if ( $this->isSuppressed( $source, $type, $lineno ) ) {
					$error->parentNode->removeChild( $error );
				} else {
					$fileHasErrors = true;
					$hasErrors = true;
				}
			}
			if ( !$fileHasErrors ) {
				$file->parentNode->removeChild( $file );
			}
		}
		echo $dom->saveXML();

		return $hasErrors;
	}
}

class NoopSuppressor extends Suppressor {
	private $mode;

	public function __construct( $mode ) {
		$this->mode = $mode;
	}
	public function suppress( $input ) {
		echo "Unsupported output mode: {$this->mode}\n$input";
		return true;
	}
}

$opt = getopt( "m:", [ "output-mode:" ] );
// if provided multiple times getopt returns an array
if ( isset( $opt['m'] ) ) {
	$mode = $opt['m'];
} elseif ( isset( $mode['output-mode'] ) ) {
	$mode = $opt['output-mode'];
} else {
	$mode = 'text';
}
if ( is_array( $mode ) ) {
	// If an option is passed multiple times getopt returns an
	// array. Just take the last one.
	$mode = end( $mode );
}

switch ( $mode ) {
case 'text':
	$suppressor = new TextSuppressor();
	break;
case 'checkstyle':
	$suppressor = new CheckStyleSuppressor();
	break;
default:
	$suppressor = new NoopSuppressor( $mode );
}

$input = file_get_contents( 'php://stdin' );
$hasErrors = $suppressor->suppress( $input );

if ( $hasErrors ) {
	exit( 1 );
}