From 8183b4bc5e5cb6eeaaee5e982ec7d475708b48a0 Mon Sep 17 00:00:00 2001 From: Bill Mitchell Date: Mon, 19 Jun 2017 16:00:55 -0700 Subject: [PATCH] Fixes #9 --- src/Commands/ChurnCommand.php | 57 +++++---------------- src/Managers/FileManager.php | 28 +++++++++++ src/Results/Result.php | 85 ++++++++++++++++++++++++++++++++ src/Results/ResultCollection.php | 19 +++++++ src/Results/ResultsGenerator.php | 63 +++++++++++++++++++++++ 5 files changed, 208 insertions(+), 44 deletions(-) create mode 100644 src/Managers/FileManager.php create mode 100644 src/Results/Result.php create mode 100644 src/Results/ResultCollection.php create mode 100644 src/Results/ResultsGenerator.php diff --git a/src/Commands/ChurnCommand.php b/src/Commands/ChurnCommand.php index f49ff163..4176c656 100644 --- a/src/Commands/ChurnCommand.php +++ b/src/Commands/ChurnCommand.php @@ -6,14 +6,10 @@ use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Input\InputArgument; -use RecursiveDirectoryIterator; -use RecursiveIteratorIterator; -use Churn\Assessors\GitCommitCount\GitCommitCountAssessor; -use Churn\Assessors\CyclomaticComplexity\CyclomaticComplexityAssessor; -use Churn\Services\CommandService; use Symfony\Component\Console\Helper\Table; -use Illuminate\Support\Collection; -use SplFileInfo; +use Churn\Results\ResultsGenerator; +use Churn\Managers\FileManager; +use Churn\Results\ResultCollection; class ChurnCommand extends Command { @@ -23,8 +19,8 @@ class ChurnCommand extends Command public function __construct() { parent::__construct(); - $this->commitCountAssessor = new GitCommitCountAssessor(new CommandService); - $this->complexityAssessor = new CyclomaticComplexityAssessor(); + $this->resultsGenerator = new ResultsGenerator; + $this->fileManager = new FileManager; } /** @@ -47,51 +43,24 @@ protected function configure() */ protected function execute(InputInterface $input, OutputInterface $output) { - $path = $input->getArgument('path'); - - $directoryIterator = new RecursiveDirectoryIterator($path); - - $results = Collection::make(); - foreach (new RecursiveIteratorIterator($directoryIterator) as $file) { - if ($file->getExtension() !== 'php') { - continue; - } - $results->push($this->getResults($file)); - } - $results = $results->sortByDesc('score'); - $this->displayTable($output, $results); - } - - /** - * Calculate the results for a file. - * @param \SplFileInfo $file File. - * @return array - */ - protected function getResults(SplFileInfo $file): array - { - $commits = $this->commitCountAssessor->assess($file->getRealPath()); - $complexity = $this->complexityAssessor->assess($file->getRealPath()); - - return [ - 'file' => $file->getRealPath(), - 'commits' => $commits, - 'complexity' => $complexity, - 'score' => ($commits/ 10) * ($complexity * 8.75), - ]; + $path = $input->getArgument('path'); + $phpFiles = $this->fileManager->getPhpFiles($path); + $results = $this->resultsGenerator->getResults($phpFiles); + $this->displayResults($output, $results); } /** * Displays the results in a table. * @param OutputInterface $output Output. - * @param \Illuminate\Support\Collection $results Results. + * @param Churn\Results\ResultCollection $results Results Collection. * @return void */ - protected function displayTable(OutputInterface $output, Collection $results) + protected function displayResults(OutputInterface $output, ResultCollection $results) { $table = new Table($output); $table->setHeaders(['File', 'Times Changed', 'Complexity', 'Score']); - foreach ($results as $resultsRow) { - $table->addRow($resultsRow); + foreach ($results->orderByScoreDesc() as $result) { + $table->addRow($result->toArray()); } $table->render(); } diff --git a/src/Managers/FileManager.php b/src/Managers/FileManager.php new file mode 100644 index 00000000..d900a9d3 --- /dev/null +++ b/src/Managers/FileManager.php @@ -0,0 +1,28 @@ +getExtension() !== 'php') { + continue; + } + $files[] = $file->getRealPath(); + } + return $files; + } +} diff --git a/src/Results/Result.php b/src/Results/Result.php new file mode 100644 index 00000000..a9b08181 --- /dev/null +++ b/src/Results/Result.php @@ -0,0 +1,85 @@ +file = $data['file']; + $this->commits = $data['commits']; + $this->complexity = $data['complexity']; + } + + /** + * Get the file property. + * @return string + */ + public function getFile(): string + { + return $this->file; + } + + /** + * Get the file property. + * @return string + */ + public function getCommits(): string + { + return $this->commits; + } + + /** + * Get the file property. + * @return string + */ + public function getComplexity(): string + { + return $this->complexity; + } + + /** + * Calculate the score. + * @return string + */ + public function getScore(): string + { + return $this->getCommits() + $this->getComplexity(); + } + + /** + * Output results to an array. + * @return string + */ + public function toArray(): string + { + return [ + $this->getFile(), + $this->getCommits(), + $this->getComplexity(), + $this->getScore(), + ]; + } +} diff --git a/src/Results/ResultCollection.php b/src/Results/ResultCollection.php new file mode 100644 index 00000000..81078ea5 --- /dev/null +++ b/src/Results/ResultCollection.php @@ -0,0 +1,19 @@ +sortByDesc(function ($result) { + return $result->getScore(); + }); + } +} diff --git a/src/Results/ResultsGenerator.php b/src/Results/ResultsGenerator.php new file mode 100644 index 00000000..e41cef08 --- /dev/null +++ b/src/Results/ResultsGenerator.php @@ -0,0 +1,63 @@ +commitCountAssessor = new GitCommitCountAssessor(new CommandService); + $this->complexityAssessor = new CyclomaticComplexityAssessor(); + } + + /** + * Generates a ResultCollection for the provided $fils. + * @param array $files List of files. + * @return ResultCollection + */ + public function getResults(array $files): ResultCollection + { + $results = new ResultCollection; + foreach ($files as $file) { + $results->push($this->getResultsForFile($file)); + } + return $results; + } + + /** + * Returns a Result object for the provided $file. + * @param string $file File. + * @return Result + */ + protected function getResultsForFile(string $file): Result + { + $commits = $this->commitCountAssessor->assess($file); + $complexity = $this->complexityAssessor->assess($file); + + return new Result([ + 'file' => $file, + 'commits' => $commits, + 'complexity' => $complexity, + ]); + } +}