r69452 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r69451‎ | r69452 | r69453 >
Date:23:53, 16 July 2010
Author:mah
Status:deferred
Tags:
Comment:
* Fix EOL whitespace fixer in stylize.php
* Add she-bang header
* Run stylize on stylize
Modified paths:
  • /trunk/tools/code-utils/stylize.php (modified) (history)

Diff [purge]

Index: trunk/tools/code-utils/stylize.php
@@ -1,3 +1,4 @@
 2+#!/usr/bin/php
23 <?php
34
45 /**
@@ -3,10 +4,9 @@
45 * A PHP code beautifier aimed at adding lots of spaces to files that lack them,
56 * in keeping with MediaWiki's spacey site style.
6 - *
 7+ *
78 * @author Tim Starling
89 * @author Jeroen De Dauw
910 */
1011
11 -
1212 if ( php_sapi_name() != 'cli' ) {
1313 echo "This script must be run from the command line\n";
@@ -45,18 +45,18 @@
4646
4747 function stylize_file( $filename, $backup = true ) {
4848 echo "Stylizing file $filename\n";
49 -
 49+
5050 $s = ( $filename == '-' )
5151 ? file_get_contents( '/dev/stdin' )
5252 : file_get_contents( $filename );
53 -
 53+
5454 if ( $s === false ) {
5555 return;
5656 }
57 -
 57+
5858 $stylizer = new Stylizer( $s );
5959 $s = $stylizer->stylize();
60 -
 60+
6161 if ( $filename == '-' ) {
6262 echo $s;
6363 } else {
@@ -133,7 +133,7 @@
134134 );
135135 static $spaceBefore = array(
136136 ')',
137 - '-', // $foo = -1; shouldn't change to $foo = - 1;
 137+ '-', // $foo = -1; shouldn't change to $foo = - 1;
138138 );
139139 static $spaceAfter = array(
140140 '(',
@@ -192,7 +192,7 @@
193193 function getPrev() {
194194 return $this->get( $this->p - 1 );
195195 }
196 -
 196+
197197 function getNext() {
198198 return $this->get( $this->p + 1 );
199199 }
@@ -264,12 +264,14 @@
265265 // Add the token contents
266266 if ( $curType == T_COMMENT ) {
267267 $curText = $this->fixComment( $curText );
268 - } elseif ( $curType == T_WHITESPACE ) {
269 - $curText = $this->fixWhitespace( $curText );
270268 }
271269
272270 $out .= $curText;
273271
 272+ if ( substr( $out, -1 ) === "\n" ) {
 273+ $out = $this->fixWhitespace( $out );
 274+ }
 275+
274276 $wantSpaceAfter = $this->isSpaceAfter( $curToken );
275277 // Special case: space after =&
276278 if ( $prevType == '=' && $curType == '&' ) {
@@ -299,6 +301,6 @@
300302
301303 function fixWhitespace( $s ) {
302304 // Fix whitespace at the line end
303 - return preg_replace( '!^([\t ]+)(\n.*)$!s', '\2', $s, 1 );
 305+ return preg_replace( "#[\t ]*\n#", "\n", $s );
304306 }
305307 }

Status & tagging log