Skip to content

Reducing complexity of execute method in SuggestCommand #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Jul 29, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/PHPSemVerCheckerGit/Console/Command/SuggestCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Gitter\Repository;
use PHPSemVerChecker\Analyzer\Analyzer;
use PHPSemVerChecker\Finder\Finder;
use PHPSemVerChecker\Report\Report;
use PHPSemVerChecker\Reporter\Reporter;
use PHPSemVerChecker\SemanticVersioning\Level;
use PHPSemVerCheckerGit\Filter\SourceFilter;
Expand All @@ -17,7 +18,6 @@
use vierbergenlars\SemVer\expression as SemanticExpression;
use vierbergenlars\SemVer\SemVerException as SemanticVersionException;
use vierbergenlars\SemVer\version as SemanticVersion;
use PHPSemVerChecker\Report\Report;

class SuggestCommand extends BaseCommand
{
Expand Down Expand Up @@ -50,8 +50,8 @@ private function getRepository($directory)
}

/**
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Symfony\Component\Console\Output\OutputInterface $output
* @param InputInterface $input
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Be careful, we still want the FQCN (fully qualified class name).

Copy link
Contributor Author

@Idrinth Idrinth Apr 14, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

having FQN in PHPdoc looks inconsistent to me when they aren't also used elsewhere, Will fix those.

* @param OutputInterface $output
* @return int
*/
protected function execute(InputInterface $input, OutputInterface $output)
Expand Down Expand Up @@ -127,7 +127,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$output->write(
array(
'',
'[Scanned files] Before: ' . count($before->getFiles()) . ' (' . $before->getOriginalAmount() . ' unfiltered), After: ' . count($after->getFiles()) . ' (' . $after->getOriginalAmount() . ' unfiltered)',
'[Scanned files] Before: ' . $before->getFilteredAmount() . ' (' . $before->getUnfilteredAmount() . ' unfiltered), After: ' . $after->getFilteredAmount() . ' (' . $after->getUnfilteredAmount() . ' unfiltered)',
'Time: ' . round($duration, 3) . ' seconds, Memory: ' . round(memory_get_peak_usage() / 1024 / 1024, 3) . ' MB'
),
true
Expand Down Expand Up @@ -173,7 +173,7 @@ private function getInitialTag(Repository $repository)
}

/**
* @param \Gitter\Repository $repository
* @param Repository $repository
* @return string|null
*/
protected function findLatestTag(Repository $repository)
Expand All @@ -182,7 +182,7 @@ protected function findLatestTag(Repository $repository)
}

/**
* @param \Gitter\Repository $repository
* @param Repository $repository
* @param string $tag
* @return string|null
*/
Expand Down Expand Up @@ -219,7 +219,7 @@ private function filterTags(array $tags)

/**
* @param string[] $tags
* @param \vierbergenlars\SemVer\version|string|null $versionTag
* @param SemanticVersion|string|null $versionTag
* @return string|null
*/
private function getMappedVersionTag(array $tags, $versionTag)
Expand Down
53 changes: 41 additions & 12 deletions src/PHPSemVerCheckerGit/ProcessedFileList.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,40 @@ class ProcessedFileList
/**
* @var int
*/
private $originalAmount;
private $unfilteredAmount;

/**
* @var int
*/
private $filteredAmount;

/**
* @var string[]
*/
private $filtered;

/**
* @var string[]
*/
private $files;
private $unfiltered;

/**
* @var Scanner
*/
private $scanner;

/**
* ProcessedFileList constructor.
* @param int $originalAmount
* @param string[] $files
* @param string[] $unfiltered
* @param string[] $filtered
* @param Scanner $scanner
*/
public function __construct($originalAmount, array &$files, Scanner &$scanner)
public function __construct(array $unfiltered, array $filtered, Scanner &$scanner)
{
$this->originalAmount = $originalAmount;
$this->files = $files;
$this->unfilteredAmount = count($unfiltered);
$this->filteredAmount = count($filtered);
$this->filtered = $filtered;
$this->unfiltered = $unfiltered;
$this->scanner = $scanner;
}

Expand All @@ -40,20 +54,35 @@ public function getScanner()
return $this->scanner;
}

/**
* @return string[]
*/
public function getFiltered()
{
return $this->filtered;
}

/**
* @return int
*/
public function getOriginalAmount()
public function getFilteredAmount()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very useful, thank you.

{
return $this->originalAmount;
return $this->filteredAmount;
}

/**
* @return string[]
* @return int
*/
public function getFiles()
public function getUnfilteredAmount()
{
return $this->files;
return $this->unfilteredAmount;
}

/**
* @return string[]
*/
public function getUnfiltered()
{
return $this->unfiltered;
}
}
17 changes: 6 additions & 11 deletions src/PHPSemVerCheckerGit/SourceFileProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,25 +66,21 @@ public function __construct(SourceFilter $filter, Repository $repository, Output
* @param $exclude
* @return ProcessedFileList
*/
public function processFileList(
$commitIdentifier,
$include,
$exclude
) {
public function processFileList($commitIdentifier, $include, $exclude)
{
$scanner = new Scanner();
$this->repository->checkout($commitIdentifier . ' --');
$source = $this->finder->findFromString($this->directory, $include, $exclude);
$count = count($source);
$source = $this->filter->filter($source, $this->modifiedFiles);
$unfiltered = $this->finder->findFromString($this->directory, $include, $exclude);
$source = $this->filter->filter($unfiltered, $this->modifiedFiles);
$this->scanFileList($scanner, $source);
return new ProcessedFileList($count, $source, $scanner);
return new ProcessedFileList($unfiltered, $source, $scanner);
}

/**
* @param Scanner $scanner
* @param array $files
*/
private function scanFileList(Scanner &$scanner, array &$files)
private function scanFileList(Scanner $scanner, array $files)
{
$progress = new ProgressBar($this->output, count($files));
foreach ($files as $file) {
Expand All @@ -93,5 +89,4 @@ private function scanFileList(Scanner &$scanner, array &$files)
}
$progress->clear();
}

}
29 changes: 21 additions & 8 deletions test/Console/Command/SuggestCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

namespace PHPSemVerCheckerGit\Test;

use PHPSemVerChecker\SemanticVersioning\Level;
use PHPSemVerCheckerGit\Console\Command\SuggestCommand;
use PHPUnit\Framework\TestCase;
use PHPSemVerChecker\SemanticVersioning\Level;
use ReflectionClass;
use ReflectionException;
use ReflectionMethod;
use vierbergenlars\SemVer\version;

class SuggestCommandTest extends TestCase
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 for the added tests.

Expand All @@ -21,6 +24,18 @@ private function getMockedVersion($major, $minor, $patch)
return new version("$major.$minor.$patch", true);
}

/**
* @return ReflectionMethod
* @throws ReflectionException
*/
private function getNextTagMethod()
{
$class = new ReflectionClass('PHPSemVerCheckerGit\Console\Command\SuggestCommand');
$method = $class->getMethod('getNextTag');
$method->setAccessible(true);
return $method;
}

/**
* Provides a few test cases
* @return array
Expand Down Expand Up @@ -54,18 +69,16 @@ public function provideGetNextTag() {
* @param $level
* @param $version
* @param $expected
* @throws \ReflectionException
* @throws ReflectionException
* @test
* @dataProvider provideGetNextTag
*/
public function testGetNextTag($level, version $version, $expected) {
public function testGetNextTag($level, version $version, $expected)
{
$report = $this->getMockBuilder('PHPSemVerChecker\Report\Report')->disableOriginalConstructor()->getMock();
$report->expects($this->once())->method('getSuggestedLevel')->willReturn($level);
$instance = new SuggestCommand();
$rc = new \ReflectionClass('PHPSemVerCheckerGit\Console\Command\SuggestCommand');
$method = $rc->getMethod('getNextTag');
$method->setAccessible(true);
$result = $method->invoke($instance, $report, $version);

$result = $this->getNextTagMethod()->invoke(new SuggestCommand(), $report, $version);
$this->assertInstanceOf('vierbergenlars\SemVer\version', $result);
$this->assertEquals($expected, $result->getVersion());
}
Expand Down