Skip to content

Commit

Permalink
New rules can be added by XML #14
Browse files Browse the repository at this point in the history
  • Loading branch information
ScreamingDev committed Mar 14, 2015
1 parent 6453185 commit 188c3b0
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 35 deletions.
62 changes: 52 additions & 10 deletions lib/PHPSemVer/Console/CompareCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ protected function configure()
'R',
InputArgument::OPTIONAL,
'A ruleset (eg. "semver2.0")',
'semver2.0'
'SemVer2'
);

$this->addArgument(
Expand Down Expand Up @@ -87,8 +87,47 @@ protected function execute(
$previousWrapper = new $wrapper( $input->getArgument( 'previous' ) );
$latestWrapper = new $wrapper( $input->getArgument( 'latest' ) );

$rule = new NoneDeletedRule( $previousWrapper->getBuilder(), $latestWrapper->getBuilder() );
$rule->process();
$xmlFile = PHPSEMVER_LIB_PATH . '/PHPSemVer/Rules/Semver2.xml';
$xmlFileShort = trim( str_replace( getcwd(), '', $xmlFile ), DIRECTORY_SEPARATOR );

$xml = simplexml_load_file( $xmlFile );

$ruleSets = $xml->xpath( '//ruleset' );

$appliedRules = array();
foreach ( $ruleSets as $ruleSet )
{
foreach ( $ruleSet->xpath( '//rule' ) as $rule )
{
if ( ! $rule->attributes() || ! $rule->attributes()->ref )
{
continue;
}

$rule = (string) $rule->attributes()->ref;

$segments = explode( '.', $rule );
$class = '\\PHPSemVer\\Rules\\' . implode( '\\', $segments ) . 'Rule';

if ( ! class_exists( $class ) )
{
throw new \Exception(
sprintf(
'Invalid rule "%s" in "%s" (class "%s" not found).',
$rule,
$xmlFileShort,
$class
)
);

continue;
}

$singleRule = new NoneDeletedRule( $previousWrapper->getBuilder(), $latestWrapper->getBuilder() );
$singleRule->process();
$appliedRules[] = $singleRule;
}
}

$table = new Table( $output );
$table->setHeaders(
Expand All @@ -98,14 +137,17 @@ protected function execute(
)
);

foreach ( $rule->getErrors() as $error )
foreach ( $appliedRules as $rule )
{
$table->addRow(
array(
$error->getRule(),
$error->getMessage()
)
);
foreach ( $rule->getErrors() as $error )
{
$table->addRow(
array(
$error->getRule(),
$error->getMessage()
)
);
}
}

$table->render();
Expand Down
24 changes: 0 additions & 24 deletions lib/PHPSemVer/Rules/Semver2Rule.php

This file was deleted.

Empty file added lib/PHPSemVer/Rules/semver2.xml
Empty file.
2 changes: 2 additions & 0 deletions lib/PHPSemVer/Wrapper/AbstractWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ public function getBuilder()
continue;
}

$sourceFile = realpath($sourceFile);

$tokenizer->setSourceFile( $sourceFile );

$parser = $this->getParser( $tokenizer, $builder, $cache );
Expand Down
9 changes: 8 additions & 1 deletion lib/PHPSemVer/Wrapper/Git.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,15 @@ public function getAllFileNames()
);

$allPrevious = explode( PHP_EOL, $result->getOutput() );
$allPrevious = array_filter( $allPrevious );

return array_filter( $allPrevious );
$allFileNames = array();
foreach ( $allPrevious as $singleFile )
{
$allFileNames[ $singleFile ] = $this->getPath( $singleFile );
}

return $allFileNames;
}

/**
Expand Down

0 comments on commit 188c3b0

Please sign in to comment.