Skip to content

Commit 574ad0c

Browse files
committed
Make each entry type a different function
1 parent 343dc13 commit 574ad0c

File tree

3 files changed

+64
-37
lines changed

3 files changed

+64
-37
lines changed

src/Gitonomy/Git/Tree.php

+38-8
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,16 @@ protected function initialize()
4949

5050
$this->entries = [];
5151
$this->entriesByType = [
52-
TreeType::BLOB->value => [],
53-
TreeType::TREE->value => [],
54-
TreeType::COMMIT->value => [],
52+
'blob' => [],
53+
'tree' => [],
54+
'commit' => [],
5555
];
5656

5757
foreach ($parser->entries as $entry) {
5858
list($mode, $type, $hash, $name) = $entry;
59-
if ($type == TreeType::BLOB->value) {
59+
if ($type == 'blob') {
6060
$treeEntry = [$mode, $this->repository->getBlob($hash)];
61-
} elseif ($type == TreeType::TREE->value) {
61+
} elseif ($type == 'tree') {
6262
$treeEntry = [$mode, $this->repository->getTree($hash)];
6363
} else {
6464
$treeEntry = [$mode, new CommitReference($hash)];
@@ -71,13 +71,43 @@ protected function initialize()
7171
}
7272

7373
/**
74-
* @return array An associative array name => $object
74+
* @return array<string, array{string, CommitReference|Tree|Blob}> An associative array name => $object
7575
*/
76-
public function getEntries(?TreeType $type = null)
76+
public function getEntries(): array
7777
{
7878
$this->initialize();
7979

80-
return $type ? $this->entriesByType[$type->value] : $this->entries;
80+
return $this->entries;
81+
}
82+
83+
/**
84+
* @return array<string, array{string, CommitReference}> An associative array of name => [mode, commit reference]
85+
*/
86+
public function getCommitReferenceEntries(): array
87+
{
88+
$this->initialize();
89+
90+
return $this->entriesByType['commit'];
91+
}
92+
93+
/**
94+
* @return array<string, array{string, Tree}> An associative array of name => [mode, tree]
95+
*/
96+
public function getTreeEntries(): array
97+
{
98+
$this->initialize();
99+
100+
return $this->entriesByType['tree'];
101+
}
102+
103+
/**
104+
* @return array<string, array{string, Blob}> An associative array of name => [mode, blob]
105+
*/
106+
public function getBlobEntries(): array
107+
{
108+
$this->initialize();
109+
110+
return $this->entriesByType['blob'];
81111
}
82112

83113
public function getEntry($name)

src/Gitonomy/Git/TreeType.php

-20
This file was deleted.

tests/Gitonomy/Git/Tests/TreeTest.php

+26-9
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
use Gitonomy\Git\Blob;
1616
use Gitonomy\Git\CommitReference;
17-
use Gitonomy\Git\TreeType;
1817

1918
class TreeTest extends AbstractTest
2019
{
@@ -39,21 +38,39 @@ public function testGetEntries($repository)
3938
/**
4039
* @dataProvider provideFooBar
4140
*/
42-
public function testGetEntriesByType($repository)
41+
public function testGetCommitReferenceEntries($repository)
4342
{
4443
$tree = $repository->getCommit(self::NO_MESSAGE_COMMIT)->getTree();
4544

46-
$blobs = $tree->getEntries(TreeType::BLOB);
45+
$commits = $tree->getCommitReferenceEntries();
4746

48-
$this->assertNotEmpty($blobs['README.md'], 'README.md is present');
49-
$this->assertTrue($blobs['README.md'][1] instanceof Blob, 'README.md is a blob');
47+
$this->assertNotEmpty($commits['barbaz'], 'barbaz is present');
48+
$this->assertTrue($commits['barbaz'][1] instanceof CommitReference, 'barbaz is a Commit');
49+
}
50+
51+
/**
52+
* @dataProvider provideFooBar
53+
*/
54+
public function testGetTreeEntries($repository)
55+
{
56+
$tree = $repository->getCommit(self::NO_MESSAGE_COMMIT)->getTree();
57+
58+
$trees = $tree->getTreeEntries();
5059

51-
$trees = $tree->getEntries(TreeType::TREE);
5260
$this->assertEmpty($trees);
61+
}
5362

54-
$commits = $tree->getEntries(TreeType::COMMIT);
55-
$this->assertNotEmpty($commits['barbaz'], 'barbaz is present');
56-
$this->assertTrue($commits['barbaz'][1] instanceof CommitReference, 'barbaz is a Commit');
63+
/**
64+
* @dataProvider provideFooBar
65+
*/
66+
public function testGetBlobEntries($repository)
67+
{
68+
$tree = $repository->getCommit(self::NO_MESSAGE_COMMIT)->getTree();
69+
70+
$blobs = $tree->getBlobEntries();
71+
72+
$this->assertNotEmpty($blobs['README.md'], 'README.md is present');
73+
$this->assertTrue($blobs['README.md'][1] instanceof Blob, 'README.md is a blob');
5774
}
5875

5976
/**

0 commit comments

Comments
 (0)