Krinkle has uploaded a new change for review.
https://gerrit.wikimedia.org/r/57830
Change subject: lint.php: Support variadic arguments and implement --verbose
flag
......................................................................
lint.php: Support variadic arguments and implement --verbose flag
Change-Id: Ieed3ef385cc846c9c722f39aed5014d24c314479
---
M includes/Args.php
M lint.php
2 files changed, 79 insertions(+), 14 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/tools/code-utils
refs/changes/30/57830/1
diff --git a/includes/Args.php b/includes/Args.php
index d47d630..dd18f80 100644
--- a/includes/Args.php
+++ b/includes/Args.php
@@ -61,6 +61,9 @@
}
// Any arguments after the last - or --
+ // FIXME: This ignores arguments before and between options
+ // So in "$ php file.php foo bar --verbose baz --force quux qux"
+ // it will silently ignore foo, bar and baz.
for ( $i = count( $argv ) - 1; $i >= 0; $i-- ) {
if ( preg_match( '/^--?.+/', $argv[$i] ) == 0 )
$this->args[] = $argv[$i];
diff --git a/lint.php b/lint.php
index 3e16a86..3479843 100755
--- a/lint.php
+++ b/lint.php
@@ -5,9 +5,58 @@
* Recursive directory crawling PHP syntax checker
* Uses parsekit, which is much faster than php -l for lots of files due to the
* PHP startup overhead.
+ *
+ * @author Tim Starling
+ * @author Timo Tijhof
+ * @file
*/
-function check_dir( $dir ) {
+require_once( __DIR__ . '/includes/Args.php' );
+
+if ( php_sapi_name() != 'cli' ) {
+ echo "This script must be run from the command line\n";
+ exit( 1 );
+}
+
+$self = array_shift( $argv );
+
+$verbose = false;
+$paths = array();
+
+if ( count( $argv ) ) {
+ $args = new Args( $argv );
+ $unknownArgs = array_diff( array_keys( $args->flags ), array( 'h',
'help', 'v', 'verbose' ) );
+ if ( count( $unknownArgs ) ) {
+ echo "error: unknown option '{$unknownArgs[0]}'\n\n";
+ $args->flags['help'] = true;
+ }
+
+ if ( $args->flag( 'help' ) || $args->flag( 'h' ) ) {
+ echo "usage: php $self [options] [<files/directories>..]
+
+ -v, --verbose Be verbose in output
+ -h, --help Show this message
+";
+ exit( 0 );
+ }
+
+ $verbose = $args->flag( 'verbose' ) || $args->flag( 'v' );
+ $paths = $args->args;
+}
+
+if ( !count( $paths ) ) {
+ $paths = array( '.' );
+}
+
+/**
+ * @param string $dir
+ * @param bool [$verbose=false]
+ * @return bool
+ */
+function check_dir( $dir, $verbose = false ) {
+ if ( $verbose ) {
+ print "... checking $dir\n";
+ }
$handle = opendir( $dir );
if ( !$handle ) {
return true;
@@ -18,9 +67,9 @@
continue;
}
if ( is_dir( "$dir/$fileName" ) ) {
- $ret = check_dir( "$dir/$fileName" );
+ $ret = check_dir( "$dir/$fileName", $verbose );
} elseif ( substr( $fileName, -4 ) == '.php' ) {
- $ret = check_file( "$dir/$fileName" );
+ $ret = check_file( "$dir/$fileName", $verbose );
} else {
$ret = true;
}
@@ -30,7 +79,15 @@
return $success;
}
-function check_file( $file ) {
+/**
+ * @param string $file
+ * @param bool [$verbose=false]
+ * @return bool
+ */
+function check_file( $file, $verbose = false ) {
+ if ( $verbose ) {
+ print "... checking $file\n";
+ }
static $okErrors = array(
'Redefining already defined constructor',
'Assigning the return value of new by reference is deprecated',
@@ -55,19 +112,24 @@
return $ret;
}
-if ( isset( $argv[1] ) ) {
- $path = $argv[1];
+$allOK = true;
+foreach ( $paths as $path ) {
if ( !file_exists( $path ) ) {
echo "Path not found: $path\n";
- exit( 1 );
+ $allOK = false;
+ continue;
}
-} else {
- $dir = '.';
+ $ret = is_dir( $path ) ? check_dir( $path, $verbose ) : check_file(
$path, $verbose );
+ $allOK = $allOK && $ret;
}
-
-$ret = is_dir( $path ) ? check_dir( $path ) : check_file( $path );
-if ( !$ret ) {
- exit( 1 );
-} else {
+if ( $allOK ) {
+ if ( $verbose ) {
+ echo "\nAll OK!\n";
+ }
exit( 0 );
+} else {
+ if ( $verbose ) {
+ echo "\nOne or more errors.\n";
+ }
+ exit( 1 );
}
--
To view, visit https://gerrit.wikimedia.org/r/57830
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Ieed3ef385cc846c9c722f39aed5014d24c314479
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/tools/code-utils
Gerrit-Branch: master
Gerrit-Owner: Krinkle <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits