From d17ecc9ee3d25fa4dcd7d1acaf159ad631c851c9 Mon Sep 17 00:00:00 2001 From: Kore Nordmann Date: Mon, 17 May 2010 11:24:56 +0000 Subject: [PATCH] # Refactored linting to use a string stream and moved out reusable methods --- src/classes/check.php | 51 ++++++++++++++++++++++++++++++++++ src/classes/check/lint.php | 19 ++----------- src/classes/check/lint/php.php | 2 +- 3 files changed, 55 insertions(+), 17 deletions(-) diff --git a/src/classes/check.php b/src/classes/check.php index 8140e67..61842d1 100644 --- a/src/classes/check.php +++ b/src/classes/check.php @@ -51,5 +51,56 @@ abstract class pchCheck * @return void */ abstract public function validate( pchRepository $repository ); + + /** + * Returns an array of chanegd files + * + * Returns an array with the names of all files wich have been changed in + * the specified transaction / revision. + * + * @param pchRepository $repository + * @return array + */ + protected function getChangedFiles( pchRepository $repository ) + { + $process = $repository->buildSvnLookCommand( 'changed' ); + $process->execute(); + + $files = preg_split( '(\r\n|\r|\n)', trim( $process->stdoutOutput ) ); + + $filtered = array(); + foreach ( $files as $file ) + { + if ( !preg_match( '(^[AM]\s+(?P.*)$)', $file, $match ) ) + { + continue; + } + + $filtered[] = $match['filename']; + } + + return $filtered; + } + + /** + * Get file contents as stream + * + * Return the contents of the specified file as a PHP stream + * + * @param pchRepository $repository + * @param string $file + * @return resource + */ + protected function getFileContents( pchRepository $repository, $file ) + { + $fileContents = $repository->buildSvnLookCommand( 'cat' ); + $fileContents->argument( $file ); + $fileContents->execute(); + + $stream = fopen( 'string://', 'w' ); + fwrite( $stream, $fileContents->stdoutOutput ); + fseek( $stream, 0 ); + return $stream; + } } diff --git a/src/classes/check/lint.php b/src/classes/check/lint.php index 73c2d1f..a35e907 100644 --- a/src/classes/check/lint.php +++ b/src/classes/check/lint.php @@ -85,11 +85,7 @@ public function lint( pchRepository $repository, $file ) $type = strtolower( pathinfo( $file, PATHINFO_EXTENSION ) ); if ( isset( $this->linters[$type] ) ) { - $fileContents = $repository->buildSvnLookCommand( 'cat' ); - $fileContents->argument( $file ); - $fileContents->execute(); - - return $this->linters[$type]->lint( $file, $fileContents->stdoutOutput ); + return $this->linters[$type]->lint( $file, $this->getFileContents( $repository, $file ) ); } return array(); @@ -106,21 +102,12 @@ public function lint( pchRepository $repository, $file ) */ public function validate( pchRepository $repository ) { - $process = $repository->buildSvnLookCommand( 'changed' ); - $process->execute(); - $files = preg_split( '(\r\n|\r|\n)', trim( $process->stdoutOutput ) ); - $issues = array(); - foreach ( $files as $file ) + foreach ( $this->getChangedFiles( $repository ) as $file ) { - if ( !preg_match( '(^[AM]\s+(?P.*)$)', $file, $match ) ) - { - continue; - } - $issues = array_merge( $issues, - $this->lint( $repository, $match['filename'] ) + $this->lint( $repository, $file ) ); } diff --git a/src/classes/check/lint/php.php b/src/classes/check/lint/php.php index 49913c1..ac81c6d 100644 --- a/src/classes/check/lint/php.php +++ b/src/classes/check/lint/php.php @@ -60,7 +60,7 @@ public function lint( $file, $contents ) // Run process asynchronously to pipe file contents into it $pipes = $check->execute( true ); - fwrite( $pipes[0], $contents ); + fwrite( $pipes[0], stream_get_contents( $contents ) ); fclose( $pipes[0] ); $output = stream_get_contents( $pipes[1] );