Skip to content

Commit

Permalink
Add multi file type getter
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianfeldmann committed Oct 4, 2023
1 parent 38586be commit 4bbd267
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 9 deletions.
37 changes: 37 additions & 0 deletions src/Diff/FilterUtil.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/**
* This file is part of SebastianFeldmann\Git.
*
* (c) Sebastian Feldmann <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace SebastianFeldmann\Git\Diff;

/**
* Filter utility class
*
*
* @author Sebastian Feldmann <[email protected]>
* @link https://github.com/sebastianfeldmann/git
* @since Class available since Release 3.9.0
*/
abstract class FilterUtil
{
/**
* Remove all invalid filter options
*
* @param array<int, string> $filter
* @return array<int, string>
*/
public static function sanitize(array $filter): array
{
return array_filter($filter, fn($e) => in_array($e, ['A', 'C', 'D', 'M', 'R', 'T', 'U', 'X', 'B', '*']) );
}
}



44 changes: 35 additions & 9 deletions src/Operator/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use SebastianFeldmann\Git\Command\DiffIndex\GetStagedFiles\FilterByStatus;
use SebastianFeldmann\Git\Command\RevParse\GetCommitHash;
use SebastianFeldmann\Git\Command\Rm\RemoveFiles;
use SebastianFeldmann\Git\Diff\FilterUtil;

/**
* Index Operator
Expand Down Expand Up @@ -77,12 +78,39 @@ public function hasStagedFilesOfType(string $suffix): bool
*
* @param string $suffix
* @param array<string> $diffFilter
* @return array<string>
* @return array<int, string>
*/
public function getStagedFilesOfType(string $suffix, array $diffFilter = []): array
{
$filter = empty($diffFilter) ? $this->defaultDiffFilter : $diffFilter;
return $this->retrieveStagedFilesByType($suffix, $filter);
$suffix = strtolower($suffix);
$sanitized = FilterUtil::sanitize($diffFilter);
$filter = empty($sanitized) ? $this->defaultDiffFilter : $sanitized;
$filesByType = $this->retrieveStagedFilesByType($filter);

return $filesByType[$suffix] ?? [];
}

/**
* Return list of changed files of a given types
*
* @param array<string> $suffixes
* @param array<string> $diffFilter
* @return array<int, string>
*/
public function getStagedFilesOfTypes(array $suffixes, array $diffFilter = []): array
{
$suffixes = array_map('strtolower', $suffixes);
$sanitized = FilterUtil::sanitize($diffFilter);
$filter = empty($sanitized) ? $this->defaultDiffFilter : $sanitized;
$filesByType = $this->retrieveStagedFilesByType($filter);

$files = [];
foreach ($suffixes as $suffix) {
if (!empty($filesByType[$suffix])) {
$files = array_merge($files, $filesByType[$suffix]);
}
}
return $files;
}

/**
Expand Down Expand Up @@ -245,14 +273,12 @@ private function retrieveFromCache(array $diffFilter): array
/**
* Sort files by file suffix
*
* @param string $suffix
* @param array<string> $diffFilter
* @return array<string>
* @return array<string, <int, string>>
*/
private function retrieveStagedFilesByType(string $suffix, array $diffFilter): array
private function retrieveStagedFilesByType(array $diffFilter): array
{
$suffix = strtolower($suffix);
$key = implode($diffFilter);
$key = implode($diffFilter);

if (!isset($this->types[$key])) {
$this->types[$key] = [];
Expand All @@ -261,7 +287,7 @@ private function retrieveStagedFilesByType(string $suffix, array $diffFilter): a
$this->types[$key][$ext][] = $file;
}
}
return isset($this->types[$key][$suffix]) ? $this->types[$key][$suffix] : [];
return $this->types[$key];
}

/**
Expand Down
39 changes: 39 additions & 0 deletions tests/git/Diff/FilterUtilTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/**
* This file is part of SebastianFeldmann\Git.
*
* (c) Sebastian Feldmann <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace SebastianFeldmann\Git\Diff;

use PHPUnit\Framework\TestCase;

class FilterUtilTest extends TestCase
{
/**
* Tests FilterUtil::sanitize
*/
public function testSanitize(): void
{
$sanitized = FilterUtil::sanitize(['A', 'C', 'Z']);

$this->assertFalse(in_array('Z', $sanitized));
$this->assertTrue(in_array('A', $sanitized));
$this->assertTrue(in_array('C', $sanitized));
}

/**
* Tests FilterUtil::sanitize
*/
public function testSanitizeEmpty(): void
{
$sanitized = FilterUtil::sanitize(['Q', 'L', 'Z']);

$this->assertEmpty($sanitized);
}
}
29 changes: 29 additions & 0 deletions tests/git/Operator/IndexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,35 @@ public function testGetStagedFilesOfType()
$this->assertCount(2, $files);
}

/**
* Tests StagedFiles::getStagedFilesOfType
*/
public function testGetStagedFilesOfTypes()
{
$out = [
'/foo/bar.txt',
'/fiz/baz.txt',
'/foo/bar.php',
'/fiz/baz.php',
'/foo/bar.tpl',
'/fiz/baz.tpl',
];

$repo = $this->getRepoMock();
$runner = $this->getRunnerMock();
$cmd = new CommandResult('git ...', 0);
$result = new RunnerResult($cmd, $out);

$repo->method('getRoot')->willReturn((string) realpath(__FILE__ . '/../../..'));
$runner->method('run')->willReturn($result);

$operator = new Index($runner, $repo);
$files = $operator->getStagedFilesOfTypes(['php', 'tpl']);

$this->assertIsArray($files);
$this->assertCount(4, $files);
}

/**
* Tests StagedFiles::getStagedFilesOfType
*/
Expand Down

0 comments on commit 4bbd267

Please sign in to comment.