Skip to content
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

Reduce the amount of data stored in memory #248

Merged
merged 2 commits into from
Oct 22, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion churn.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# The minimum score a file need to display in the results table.
# Default: 0
# Default: 0.1
minScoreToShow: 0
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
"symfony/console": "^3.4 || ^4.0 || ^5.0",
"symfony/process": "^3.4 || ^4.0 || ^5.0",
"symfony/yaml": "^3.4 || ^4.0 || ^5.0",
"tightenco/collect": "^5.4",
"webmozart/assert": "^1.2"
},
"require-dev": {
Expand Down
27 changes: 0 additions & 27 deletions src/Collections/FileCollection.php

This file was deleted.

61 changes: 25 additions & 36 deletions src/Command/RunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
namespace Churn\Command;

use Churn\Configuration\Config;
use Churn\Factories\ResultsRendererFactory;
use Churn\Logic\ResultsLogic;
use Churn\Managers\FileManager;
use Churn\File\FileFinder;
use Churn\Process\Observer\OnSuccess;
use Churn\Process\Observer\OnSuccessNull;
use Churn\Process\Observer\OnSuccessAccumulate;
use Churn\Process\Observer\OnSuccessCollection;
use Churn\Process\Observer\OnSuccessProgress;
use Churn\Process\ProcessFactory;
use Churn\Process\ProcessHandlerFactory;
use Churn\Results\ResultCollection;
use Churn\Result\ResultAccumulator;
use Churn\Result\ResultsRendererFactory;
use function count;
use function file_get_contents;
use function fopen;
Expand All @@ -37,12 +37,6 @@ class RunCommand extends Command
\___)(_) (_)(______)(_)\_)(_)\_) (__) (_) (_)(__)
";

/**
* The results logic.
* @var ResultsLogic
*/
private $resultsLogic;

/**
* The process handler factory.
* @var ProcessHandlerFactory
Expand All @@ -57,17 +51,14 @@ class RunCommand extends Command

/**
* ChurnCommand constructor.
* @param ResultsLogic $resultsLogic The results logic.
* @param ProcessHandlerFactory $processHandlerFactory The process handler factory.
* @param ResultsRendererFactory $renderFactory The Results Renderer Factory.
*/
public function __construct(
ResultsLogic $resultsLogic,
ProcessHandlerFactory $processHandlerFactory,
ResultsRendererFactory $renderFactory
) {
parent::__construct();
$this->resultsLogic = $resultsLogic;
$this->processHandlerFactory = $processHandlerFactory;
$this->renderFactory = $renderFactory;
}
Expand Down Expand Up @@ -99,19 +90,15 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$this->displayLogo($input, $output);
$content = (string) @file_get_contents($input->getOption('configuration'));
$config = Config::create(Yaml::parse($content) ?? []);
$filesCollection = (new FileManager($config->getFileExtensions(), $config->getFilesToIgnore()))
$filesFinder = (new FileFinder($config->getFileExtensions(), $config->getFilesToIgnore()))
->getPhpFiles($this->getDirectoriesToScan($input, $config->getDirectoriesToScan()));
$completedProcesses = $this->processHandlerFactory->getProcessHandler($config)->process(
$filesCollection,
$accumulator = new ResultAccumulator($config->getFilesToShow(), $config->getMinScoreToShow());
$this->processHandlerFactory->getProcessHandler($config)->process(
$filesFinder,
new ProcessFactory($config->getCommitsSince()),
$this->getOnSuccessObserver($input, $output, $filesCollection->count())
$this->getOnSuccessObserver($input, $output, $accumulator)
);
$resultCollection = $this->resultsLogic->process(
$completedProcesses,
$config->getMinScoreToShow(),
$config->getFilesToShow()
);
$this->writeResult($input, $output, $resultCollection);
$this->writeResult($input, $output, $accumulator);
return 0;
}

Expand Down Expand Up @@ -140,20 +127,22 @@ private function getDirectoriesToScan(InputInterface $input, array $dirsConfigur
}

/**
* @param InputInterface $input Input.
* @param OutputInterface $output Output.
* @param integer $totalFiles Total number of files to process.
* @param InputInterface $input Input.
* @param OutputInterface $output Output.
* @param ResultAccumulator $accumulator The object accumulating the results.
* @return OnSuccess
*/
private function getOnSuccessObserver(InputInterface $input, OutputInterface $output, int $totalFiles): OnSuccess
private function getOnSuccessObserver(InputInterface $input, OutputInterface $output, ResultAccumulator $accumulator): OnSuccess
{
$observer = new OnSuccessAccumulate($accumulator);

if ((bool)$input->getOption('progress')) {
$progressBar = new ProgressBar($output, $totalFiles);
$progressBar = new ProgressBar($output);
$progressBar->start();
return new OnSuccessProgress($progressBar);
$observer = new OnSuccessCollection($observer, new OnSuccessProgress($progressBar));
}

return new OnSuccessNull();
return $observer;
}

/**
Expand All @@ -171,12 +160,12 @@ private function displayLogo(InputInterface $input, OutputInterface $output): vo
}

/**
* @param InputInterface $input Input.
* @param OutputInterface $output Output.
* @param ResultCollection $resultCollection The result to write.
* @param InputInterface $input Input.
* @param OutputInterface $output Output.
* @param ResultAccumulator $accumulator The results to write.
* @return void
*/
private function writeResult(InputInterface $input, OutputInterface $output, ResultCollection $resultCollection): void
private function writeResult(InputInterface $input, OutputInterface $output, ResultAccumulator $accumulator): void
{
if ((bool)$input->getOption('progress')) {
$output->writeln("\n");
Expand All @@ -186,6 +175,6 @@ private function writeResult(InputInterface $input, OutputInterface $output, Res
}

$renderer = $this->renderFactory->getRenderer($input->getOption('format'));
$renderer->render($output, $resultCollection);
$renderer->render($output, $accumulator->toArray());
}
}
11 changes: 6 additions & 5 deletions src/Values/File.php → src/File/File.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php declare(strict_types = 1);

namespace Churn\Values;
namespace Churn\File;

class File
{
Expand All @@ -18,12 +18,13 @@ class File

/**
* File constructor.
* @param array $fileData Raw file data.
* @param string $fullPath The full path of the file.
* @param string $displayPath The display path of the file.
*/
public function __construct(array $fileData)
public function __construct(string $fullPath, string $displayPath)
{
$this->fullPath = $fileData['fullPath'];
$this->displayPath = $fileData['displayPath'];
$this->fullPath = $fullPath;
$this->displayPath = $displayPath;
}

/**
Expand Down
55 changes: 26 additions & 29 deletions src/Managers/FileManager.php → src/File/FileFinder.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
<?php declare(strict_types = 1);

namespace Churn\Managers;
namespace Churn\File;

use Churn\Collections\FileCollection;
use Churn\Values\File;
use const DIRECTORY_SEPARATOR;
use Generator;
use function in_array;
use function preg_match;
use function preg_replace;
Expand All @@ -13,14 +12,8 @@
use SplFileInfo;
use function str_replace;

class FileManager
class FileFinder
{
/**
* Collection of File objects.
* @var FileCollection;
*/
private $files;

/**
* List of file extensions to look for.
* @var array
Expand All @@ -34,7 +27,7 @@ class FileManager
private $filesToIgnore;

/**
* FileManager constructor.
* Class constructor.
* @param array $fileExtensions List of file extensions to look for.
* @param array $filesToIgnore List of files to ignore.
*/
Expand All @@ -47,41 +40,45 @@ public function __construct(array $fileExtensions, array $filesToIgnore)
/**
* Recursively finds all files with the .php extension in the provided
* $paths and returns list as array.
* @param array $paths Paths in which to look for .php files.
* @return FileCollection
* @param array $paths Paths in which to look for .php files.
* @return Generator
*/
public function getPhpFiles(array $paths): FileCollection
public function getPhpFiles(array $paths): Generator
{
$this->files = new FileCollection;
foreach ($paths as $path) {
$this->getPhpFilesFromPath($path);
yield from $this->getPhpFilesFromPath($path);
}

return $this->files;
}

/**
* Recursively finds all files with the .php extension in the provided
* $path adds them to $this->files.
* @param string $path Path in which to look for .php files.
* @return void
* @param string $path Path in which to look for .php files.
* @return Generator
*/
private function getPhpFilesFromPath(string $path): void
private function getPhpFilesFromPath(string $path): Generator
{
$directoryIterator = new RecursiveDirectoryIterator($path);
foreach (new RecursiveIteratorIterator($directoryIterator) as $file) {
if (! in_array($file->getExtension(), $this->fileExtensions)) {
continue;
}

if ($this->fileShouldBeIgnored($file)) {
foreach ($this->getPathIterator($path) as $file) {
if (!in_array($file->getExtension(), $this->fileExtensions)
|| $this->fileShouldBeIgnored($file)) {
continue;
}

$this->files->push(new File(['displayPath' => $file->getPathName(), 'fullPath' => $file->getRealPath()]));
yield new File($file->getRealPath(), $file->getPathName());
}
}

/**
* Returns a recursive iterator for a given directory.
* @param string $path Path in which to look for .php files.
* @return RecursiveIteratorIterator
*/
private function getPathIterator(string $path): RecursiveIteratorIterator
{
$directoryIterator = new RecursiveDirectoryIterator($path);
return new RecursiveIteratorIterator($directoryIterator);
}

/**
* Determines if a file should be ignored.
* @param \SplFileInfo $file File.
Expand Down
40 changes: 0 additions & 40 deletions src/Logic/ResultsLogic.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/Process/ChurnProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Churn\Process;

use Churn\Values\File;
use Churn\File\File;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;

Expand Down
Loading