Skip to content

Commit

Permalink
Merge pull request #26 from MacFJA/add-exclusion
Browse files Browse the repository at this point in the history
Add an option to ignore paths
  • Loading branch information
rskuipers authored Mar 1, 2018
2 parents 7f17d7a + d83212e commit 74e6877
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 13 deletions.
18 changes: 15 additions & 3 deletions src/PhpAssumptions/Analyser.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use PhpParser\Node;
use PhpParser\NodeTraverserInterface;
use PhpParser\Parser\Multiple;
use PhpParser\ParserAbstract;

class Analyser
{
Expand Down Expand Up @@ -35,16 +36,24 @@ class Analyser
private $result;

/**
* @param ParserAbstract $parser
* @param NodeTraverserInterface $nodeTraverser
* @var array|\string[]
*/
private $excludes = [];

/**
* @param ParserAbstract|Multiple $parser
* @param NodeTraverserInterface $nodeTraverser
* @param string[] $excludes
*/
public function __construct(
Multiple $parser,
NodeTraverserInterface $nodeTraverser
NodeTraverserInterface $nodeTraverser,
$excludes = []
) {
$this->parser = $parser;
$this->traverser = $nodeTraverser;
$this->result = new Result();
$this->excludes = $excludes;
}

/**
Expand All @@ -54,6 +63,9 @@ public function __construct(
public function analyse(array $files)
{
foreach ($files as $file) {
if (in_array($file, $this->excludes, true)) {
continue;
}
$this->currentFilePath = $file;
$this->currentFile = [];
$statements = $this->parser->parse(file_get_contents($file));
Expand Down
54 changes: 45 additions & 9 deletions src/PhpAssumptions/Cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ public function __construct(CLImate $cli)
'description' => 'Format (pretty, xml)',
'defaultValue' => 'pretty',
],
'exclude' => [
'prefix' => 'e',
'longPrefix' => 'exclude',
'description' => 'List of files/directories (separate by ",") to exclude from the analyse',
'defaultValue' => ''
],
'output' => [
'prefix' => 'o',
'longPrefix' => 'output',
Expand Down Expand Up @@ -73,32 +79,62 @@ public function handle(array $args)
break;
}

$excludes = $this->getPathsFromList($this->cli->arguments->get('exclude'));

$nodeTraverser = new NodeTraverser();

$analyser = new Analyser(
$this->parser,
$nodeTraverser
$nodeTraverser,
$excludes
);

$nodeTraverser->addVisitor(new NodeVisitor($analyser, new Detector()));

$target = $this->cli->arguments->get('path');
$targets = [];
$targets = $this->getPaths($target);

$result = $analyser->analyse($targets);

if (is_file($target)) {
$targets[] = $target;
$output->output($result);
}

/**
* @param string $list
* @return array
*/
private function getPathsFromList($list)
{
$paths = [];
if (strlen($list) > 0) {
$items = explode(',', $list);
foreach ($items as $item) {
$paths = array_merge($paths, $this->getPaths($item));
}
}

return $paths;
}

/**
* @param string $fromPath
* @return array
*/
private function getPaths($fromPath)
{
$paths = [];
if (is_file($fromPath)) {
$paths[] = $fromPath;
} else {
$directory = new \RecursiveDirectoryIterator($target);
$directory = new \RecursiveDirectoryIterator($fromPath);
$iterator = new \RecursiveIteratorIterator($directory);
$regex = new \RegexIterator($iterator, '/^.+\.php$/i', \RecursiveRegexIterator::GET_MATCH);

foreach ($regex as $file) {
$targets[] = $file[0];
$paths[] = $file[0];
}
}

$result = $analyser->analyse($targets);

$output->output($result);
return $paths;
}
}
18 changes: 17 additions & 1 deletion tests/PhpAssumptions/AnalyserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function setUp()
$this->analyser = new Analyser(
$this->parser->reveal(),
$this->nodeTraverser->reveal(),
$this->output->reveal()
[fixture('MyOtherClass.php')]
);
}

Expand All @@ -64,4 +64,20 @@ public function itShouldAnalyseAllFiles()

$this->analyser->analyse($files);
}

/**
* @test
*/
public function itShouldIgnoreExcludeFiles()
{
$files = [fixture('MyClass.php'), fixture('MyOtherClass.php')];
$nodes = [$this->node];

$parseRes = $this->parser->parse(Argument::type('string'));
$this->parser->parse(Argument::type('string'))->shouldBeCalled()->willReturn($nodes);

$this->nodeTraverser->traverse($nodes)->shouldBeCalled();

$this->analyser->analyse($files);
}
}
62 changes: 62 additions & 0 deletions tests/PhpAssumptions/CliTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public function itShouldAnalyseTargetFile()

$this->argumentManager->get('format')->shouldBeCalled()->willReturn('pretty');
$this->argumentManager->get('path')->shouldBeCalled()->willReturn($path);
$this->argumentManager->get('exclude')->shouldBeCalled()->willReturn('');

$this->climate->table([
[
Expand All @@ -73,6 +74,7 @@ public function itShouldAnalyseTargetDirectory()

$this->argumentManager->get('format')->shouldBeCalled()->willReturn('pretty');
$this->argumentManager->get('path')->shouldBeCalled()->willReturn(FIXTURES_DIR);
$this->argumentManager->get('exclude')->shouldBeCalled()->willReturn('');

// Assert that all files show up in the table
$this->climate->table(Argument::that(function ($table) use ($files) {
Expand All @@ -89,6 +91,65 @@ public function itShouldAnalyseTargetDirectory()
$this->cli->handle(['phpa', FIXTURES_DIR]);
}

/**
* @test
*/
public function itShouldIgnoreExcludeFile()
{
$path = fixture('MyClass.php');

$this->argumentManager->get('format')->shouldBeCalled()->willReturn('pretty');
$this->argumentManager->get('path')->shouldBeCalled()->willReturn($path);
$this->argumentManager->get('exclude')->shouldBeCalled()->willReturn(fixture('MyClass.php'));

$this->climate->table()->shouldNotBeCalled();
$this->climate->out('0 out of 0 boolean expressions are assumptions (0%)')->shouldBeCalled();

$this->cli->handle(['phpa', $path]);
}

/**
* @test
*/
public function itShouldIgnoreExcludeFileFromDirectory()
{
$path = fixture('MyClass.php');

$this->argumentManager->get('format')->shouldBeCalled()->willReturn('pretty');
$this->argumentManager->get('path')->shouldBeCalled()->willReturn(FIXTURES_DIR);
$this->argumentManager->get('exclude')->shouldBeCalled()->willReturn(fixture('MyOtherClass.php').','.fixture('Example.php'));

$this->climate->table([
[
'file' => $path,
'line' => 9,
'message' => 'if ($dog !== null) {',
]
])->shouldBeCalled()->willReturn($this->climate);
$this->climate->out('1 out of 2 boolean expressions are assumptions (50%)')->shouldBeCalled();

$this->cli->handle(['phpa', FIXTURES_DIR]);
}

/**
* @test
*/
public function itShouldIgnoreExcludeDirectory()
{
$files = [fixture('MyClass.php'), fixture('MyOtherClass.php'), fixture('Example.php')];

$this->argumentManager->get('format')->shouldBeCalled()->willReturn('pretty');
$this->argumentManager->get('path')->shouldBeCalled()->willReturn(FIXTURES_DIR);
$this->argumentManager->get('exclude')->shouldBeCalled()->willReturn(fixture(''));

// Assert that all files show up in the table
$this->climate->table()->shouldNotBeCalled();

$this->climate->out(Argument::containingString('boolean expressions are assumptions'))->shouldBeCalled();

$this->cli->handle(['phpa', FIXTURES_DIR]);
}

/**
* @test
*/
Expand All @@ -100,6 +161,7 @@ public function itShouldAnalyseTargetFileAndOutputXml()
$this->argumentManager->get('format')->shouldBeCalled()->willReturn('xml');
$this->argumentManager->get('path')->shouldBeCalled()->willReturn($path);
$this->argumentManager->get('output')->shouldBeCalled()->willReturn($output);
$this->argumentManager->get('exclude')->shouldBeCalled()->willReturn('');

$this->climate->out('Written 1 assumption(s) to file ' . $output)->shouldBeCalled();

Expand Down

0 comments on commit 74e6877

Please sign in to comment.