Skip to content

Commit

Permalink
# Refactored linting to use a string stream and moved out reusable me…
Browse files Browse the repository at this point in the history
…thods
  • Loading branch information
kore committed May 17, 2010
1 parent c6d8d68 commit d17ecc9
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 17 deletions.
51 changes: 51 additions & 0 deletions src/classes/check.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<filename>.*)$)', $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;
}
}

19 changes: 3 additions & 16 deletions src/classes/check/lint.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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<filename>.*)$)', $file, $match ) )
{
continue;
}

$issues = array_merge(
$issues,
$this->lint( $repository, $match['filename'] )
$this->lint( $repository, $file )
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/classes/check/lint/php.php
Original file line number Diff line number Diff line change
Expand Up @@ -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] );
Expand Down

0 comments on commit d17ecc9

Please sign in to comment.