Repository: bmitch/churn-php (php)

Issue:
Make FileManager return an object instead of an array?



Discussion:

Patch:
diff --git a/src/Collections/FileCollection.php b/src/Collections/FileCollection.php
new file mode 100644
--- /dev/null
+++ b/src/Collections/FileCollection.php
@@ -0,0 +1,27 @@
+<?php declare(strict_types = 1);
+
+namespace Churn\Collections;
+
+use Illuminate\Support\Collection;
+use Churn\Values\File;
+
+class FileCollection extends Collection
+{
+    /**
+     * Determines if the collection has any files left.
+     * @return boolean
+     */
+    public function hasFiles(): bool
+    {
+        return $this->count() > 0;
+    }
+
+    /**
+     * Pops off the next file from the collection.
+     * @return File
+     */
+    public function getNextFile(): File
+    {
+        return $this->shift();
+    }
+}
diff --git a/src/Commands/ChurnCommand.php b/src/Commands/ChurnCommand.php
--- a/src/Commands/ChurnCommand.php
+++ b/src/Commands/ChurnCommand.php
@@ -32,7 +32,7 @@ class ChurnCommand extends Command
      * Collection of files to run the processes on.
      * @var Collection
      */
-    private $files;
+    private $filesCollection;
 
     /**
      * Collection of processes currently running.
@@ -77,11 +77,11 @@ protected function configure()
     protected function execute(InputInterface $input, OutputInterface $output)
     {
         $path = $input->getArgument('path');
-        $this->files = $this->fileManager->getPhpFiles($path);
+        $this->filesCollection = $this->fileManager->getPhpFiles($path);
 
         $this->runningProcesses = new Collection;
         $this->completedProcessesArray = [];
-        while ($this->files->count() || $this->runningProcesses->count()) {
+        while ($this->filesCollection->hasFiles() || $this->runningProcesses->count()) {
             $this->getProcessResults();
         }
         $completedProcesses = new Collection($this->completedProcessesArray);
@@ -96,8 +96,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
      */
     private function getProcessResults()
     {
-        for ($index = $this->runningProcesses->count(); $this->files->count() > 0 && $index < 10; $index++) {
-            $file = $this->files->shift();
+        for ($index = $this->runningProcesses->count(); $this->filesCollection->hasFiles() > 0 && $index < 10; $index++) {
+            $file = $this->filesCollection->getNextFile();
 
             $process = new GitCommitCountProcess($file);
             $process->start();
diff --git a/src/Managers/FileManager.php b/src/Managers/FileManager.php
--- a/src/Managers/FileManager.php
+++ b/src/Managers/FileManager.php
@@ -2,7 +2,7 @@
 
 namespace Churn\Managers;
 
-use Illuminate\Support\Collection;
+use Churn\Collections\FileCollection;
 use RecursiveDirectoryIterator;
 use RecursiveIteratorIterator;
 use Churn\Values\File;
@@ -13,12 +13,12 @@ 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 Collection
+     * @return FileCollection
      */
-    public function getPhpFiles(string $path): Collection
+    public function getPhpFiles(string $path): FileCollection
     {
         $directoryIterator = new RecursiveDirectoryIterator($path);
-        $files = new Collection;
+        $files = new FileCollection;
         foreach (new RecursiveIteratorIterator($directoryIterator) as $file) {
             if ($file->getExtension() !== 'php') {
                 continue;
diff --git a/src/Results/ResultsParser.php b/src/Results/ResultsParser.php
--- a/src/Results/ResultsParser.php
+++ b/src/Results/ResultsParser.php
@@ -60,6 +60,9 @@ private function parseCommits(GitCommitCountProcess $process): int
     {
         $output = $process->getOutput();
         preg_match("/([0-9]{1,})/", $output, $matches);
-        return (integer) $matches[1] ?? 0;
+        if (! isset($matches[1])) {
+            return 0;
+        }
+        return (integer) $matches[1];
     }
 }


Test patch:
diff --git a/tests/Unit/Collections/FileCollectionTest.php b/tests/Unit/Collections/FileCollectionTest.php
new file mode 100644
--- /dev/null
+++ b/tests/Unit/Collections/FileCollectionTest.php
@@ -0,0 +1,37 @@
+<?php
+
+namespace Churn\Tests\Unit\Collections;
+
+use Churn\Collections\FileCollection;
+use Churn\Values\File;
+
+class FileCollectionTest extends \Churn\Tests\BaseTestCase
+{
+    /** @test **/
+    public function it_can_be_instantiated()
+    {
+        $this->assertInstanceOf(FileCollection::class, new FileCollection());
+    }
+
+    /** @test **/
+    public function it_can_pop_off_the_next_file()
+    {
+        $fileCollection = new FileCollection;
+        $fileCollection->push(new File(['fullPath' => 'foo.php', 'displayPath' => 'foo.php']));
+        $fileCollection->push(new File(['fullPath' => 'bar.php', 'displayPath' => 'bar.php']));
+        $fileCollection->push(new File(['fullPath' => 'baz.php', 'displayPath' => 'baz.php']));
+        $this->assertCount(3, $fileCollection);
+        $poppedFile = $fileCollection->getNextFile();
+        $this->assertCount(2, $fileCollection);
+    }
+
+    /** @test **/
+    public function it_can_determine_if_it_has_files_or_not()
+    {
+        $fileCollection = new FileCollection;
+        $this->assertFalse($fileCollection->hasFiles());
+        $fileCollection->push(new File(['fullPath' => 'foo.php', 'displayPath' => 'foo.php']));
+        $this->assertTrue($fileCollection->hasFiles());
+    }
+    
+}
\ No newline at end of file