-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
208 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Churn\Managers; | ||
|
||
use RecursiveDirectoryIterator; | ||
use RecursiveIteratorIterator; | ||
|
||
class FileManager | ||
{ | ||
/** | ||
* Recursively finds all files with the .php extension in the provided | ||
* $path and returns list as array. | ||
* @param string $path Path to look for .php files. | ||
* @return array | ||
*/ | ||
public function getPhpFiles(string $path): array | ||
{ | ||
$directoryIterator = new RecursiveDirectoryIterator($path); | ||
$files = []; | ||
foreach (new RecursiveIteratorIterator($directoryIterator) as $file) { | ||
if ($file->getExtension() !== 'php') { | ||
continue; | ||
} | ||
$files[] = $file->getRealPath(); | ||
} | ||
return $files; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Churn\Results; | ||
|
||
class Result | ||
{ | ||
/** | ||
* The file property. | ||
* @var string | ||
*/ | ||
private $file; | ||
|
||
/** | ||
* The commits property. | ||
* @var string | ||
*/ | ||
private $commits; | ||
|
||
/** | ||
* The complexity property. | ||
* @var string | ||
*/ | ||
private $complexity; | ||
|
||
/** | ||
* Class constructor. | ||
* @param array $data Data to store in the object. | ||
*/ | ||
public function __construct(array $data) | ||
{ | ||
$this->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(), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Churn\Results; | ||
|
||
use Illuminate\Support\Collection; | ||
|
||
class ResultCollection extends Collection | ||
{ | ||
/** | ||
* Order results by their score in descending order. | ||
* @return self | ||
*/ | ||
public function orderByScoreDesc(): self | ||
{ | ||
return $this->sortByDesc(function ($result) { | ||
return $result->getScore(); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Churn\Results; | ||
|
||
use Churn\Services\CommandService; | ||
use Churn\Assessors\GitCommitCount\GitCommitCountAssessor; | ||
use Churn\Assessors\CyclomaticComplexity\CyclomaticComplexityAssessor; | ||
|
||
class ResultsGenerator | ||
{ | ||
|
||
/** | ||
* The commit count assesor. | ||
* @var GitCommitCountAssessor | ||
*/ | ||
protected $commitCountAssessor; | ||
|
||
/** | ||
* The complexity assesor. | ||
* @var CyclomaticComplexityAssessor | ||
*/ | ||
protected $complexityAssessor; | ||
|
||
/** | ||
* Class constructor. | ||
*/ | ||
public function __construct() | ||
{ | ||
$this->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, | ||
]); | ||
} | ||
} |