Skip to content

Commit

Permalink
[#45] Replace File_Iterator with Symfony Finder
Browse files Browse the repository at this point in the history
[#23] Add command line input option to support includes/excludes
  • Loading branch information
tomzx committed May 3, 2015
1 parent 567562b commit 56f95cb
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 8 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"herrera-io/phar-update": "~2.0",
"nikic/php-parser": "dev-features/php-semver-checker",
"symfony/console": "2.7.*@dev",
"phpunit/php-file-iterator": "~1.3"
"tomzx/finder": "~0.1@dev"
},
"require-dev": {
"mockery/mockery": "~0.9",
Expand Down
23 changes: 16 additions & 7 deletions src/PHPSemVerChecker/Console/Command/CompareCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace PHPSemVerChecker\Console\Command;

use File_Iterator_Facade;
use PHPSemVerChecker\Analyzer\Analyzer;
use PHPSemVerChecker\Configuration\Configuration;
use PHPSemVerChecker\Configuration\LevelMapping;
use PHPSemVerChecker\Filter\SourceFilter;
use PHPSemVerChecker\Finder\Finder;
use PHPSemVerChecker\Reporter\JsonReporter;
use PHPSemVerChecker\Reporter\Reporter;
use PHPSemVerChecker\Scanner\Scanner;
Expand All @@ -24,10 +24,14 @@ protected function configure()
->setName('compare')
->setDescription('Compare a set of files to determine what semantic versioning change needs to be done')
->setDefinition([
new InputArgument('source-before', InputArgument::REQUIRED, 'A directory to check'),
new InputArgument('source-after', InputArgument::REQUIRED, 'A directory to check against'),
new InputArgument('source-before', InputArgument::REQUIRED, 'A base directory to check (ex my-test)'),
new InputArgument('source-after', InputArgument::REQUIRED, 'A base directory to check against (ex my-test)'),
new InputOption('include-before', null, InputOption::VALUE_OPTIONAL, 'List of paths to include <info>(comma separated)</info>'),
new InputOption('include-after', null, InputOption::VALUE_OPTIONAL, 'List of paths to include <info>(comma separated)</info>'),
new InputOption('exclude-before', null, InputOption::VALUE_REQUIRED, 'List of paths to exclude <info>(comma separated)</info>'),
new InputOption('exclude-after', null, InputOption::VALUE_REQUIRED, 'List of paths to exclude <info>(comma separated)</info>'),
new InputOption('full-path', null, InputOption::VALUE_NONE, 'Display the full path to the file instead of the relative path'),
new InputOption('config', 'c', InputOption::VALUE_REQUIRED, 'A configuration file to configure php-semver-checker'),
new InputOption('config', null, InputOption::VALUE_REQUIRED, 'A configuration file to configure php-semver-checker'),
new InputOption('to-json', null, InputOption::VALUE_REQUIRED, 'Output the result to a JSON file')
]);
}
Expand All @@ -42,15 +46,20 @@ protected function execute(InputInterface $input, OutputInterface $output)
// Set overrides
LevelMapping::setOverrides($configuration->getLevelMapping());

$fileIterator = new File_Iterator_Facade;
$finder = new Finder();
$scannerBefore = new Scanner();
$scannerAfter = new Scanner();

$sourceBefore = $input->getArgument('source-before');
$sourceBefore = $fileIterator->getFilesAsArray($sourceBefore, '.php');
$includeBefore = $input->getOption('include-before');
$excludeBefore = $input->getOption('exclude-before');

$sourceAfter = $input->getArgument('source-after');
$sourceAfter = $fileIterator->getFilesAsArray($sourceAfter, '.php');
$includeAfter = $input->getOption('include-after');
$excludeAfter = $input->getOption('exclude-after');

$sourceBefore = $finder->findFromString($sourceBefore, $includeBefore, $excludeBefore);
$sourceAfter = $finder->findFromString($sourceAfter, $includeAfter, $excludeAfter);

$sourceFilter = new SourceFilter();
$identicalCount = $sourceFilter->filter($sourceBefore, $sourceAfter);
Expand Down
58 changes: 58 additions & 0 deletions src/PHPSemVerChecker/Finder/Finder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace PHPSemVerChecker\Finder;

use Finder\Adapter\SymfonyFinder as BaseFinder;

class Finder
{
/**
* @param string $path
* @param array $includes
* @param array $excludes
* @return array
*/
public function find($path, array $includes, array $excludes = [])
{
$finder = new BaseFinder();
$finder->ignoreDotFiles(true)
->files()
->name('*.php')
->in($path);

foreach ($includes as $include) {
$finder->path($include);
}

foreach ($excludes as $exclude) {
$finder->notPath($exclude);
}

$files = iterator_to_array($finder->getIterator());

return $files;
}

/**
* @param string $path
* @param string $includes
* @param string $excludes
* @return array
*/
public function findFromString($path, $includes, $excludes)
{
if ($includes === '*') {
$includes = [];
} else {
$includes = preg_split('@(?:\s*,\s*|^\s*|\s*$)@', $includes, NULL, PREG_SPLIT_NO_EMPTY);
}

if ($excludes === '*') {
$excludes = [];
} else {
$excludes = preg_split('@(?:\s*,\s*|^\s*|\s*$)@', $excludes, NULL, PREG_SPLIT_NO_EMPTY);
}

return $this->find($path, $includes, $excludes);
}
}

0 comments on commit 56f95cb

Please sign in to comment.