Skip to content

Commit 33ae0a2

Browse files
authored
Added more type hinting & Fixed bug when parsing renamed files with spaces and/or non english-symbols (#194)
* changed repo name in the composer * added path to the symlink RuntimeException exception-message * added more and better typehints into PHPDocs above methods * Regex didn't count for the rare case when the diff has quotation marks around the filenames (file names with non-english symbols for example) * replaced uninitialized $entry variable with $element * removed getRevisions() since revisions isn't a part of the Diff object * removed unused $fullname in the getCommit() method * - added more type hinting - introduced data property to Tag - added .idea to .gitignore * - changes that were requested by " continuous-integration/styleci/pr" - fixed composer.json * upped the number of characters that are shown to get a more verbose exception * added type annotation for file in the Diff class * - fixed parser to properly work with files - with umlauts added a test - made changes @lyrixx suggested * - made fixes for @continuous-integration/styleci/pr * - made one more fix for @continuous-integration/styleci/pr * - switches quotes for @continuous-integration/styleci/pr * added trailing \n to the gitignore
1 parent 793ffe5 commit 33ae0a2

18 files changed

+123
-57
lines changed

src/Gitonomy/Git/Blame.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,7 @@ public function getGroupedLines()
108108
}
109109

110110
/**
111-
* Returns all lines of the blame.
112-
*
113-
* @return array
111+
* @return Line[] All lines of the blame.
114112
*/
115113
public function getLines()
116114
{

src/Gitonomy/Git/Blob.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ public function getHash()
5858
}
5959

6060
/**
61-
* Returns content of the blob.
62-
*
6361
* @throws ProcessException Error occurred while getting content of blob
62+
*
63+
* @return string Content of the blob.
6464
*/
6565
public function getContent()
6666
{

src/Gitonomy/Git/Commit.php

+12-6
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Gitonomy\Git\Exception\InvalidArgumentException;
1717
use Gitonomy\Git\Exception\ProcessException;
1818
use Gitonomy\Git\Exception\ReferenceNotFoundException;
19+
use Gitonomy\Git\Reference\Branch;
1920
use Gitonomy\Git\Util\StringHelper;
2021

2122
/**
@@ -35,8 +36,8 @@ class Commit extends Revision
3536
/**
3637
* Constructor.
3738
*
38-
* @param Gitonomy\Git\Repository $repository Repository of the commit
39-
* @param string $hash Hash of the commit
39+
* @param Repository $repository Repository of the commit
40+
* @param string $hash Hash of the commit
4041
*/
4142
public function __construct(Repository $repository, $hash, array $data = [])
4243
{
@@ -91,6 +92,8 @@ public function getShortHash()
9192

9293
/**
9394
* Returns a fixed-with short hash.
95+
*
96+
* @return string Short hash
9497
*/
9598
public function getFixedShortHash($length = 6)
9699
{
@@ -100,7 +103,7 @@ public function getFixedShortHash($length = 6)
100103
/**
101104
* Returns parent hashes.
102105
*
103-
* @return array An array of SHA1 hashes
106+
* @return string[] An array of SHA1 hashes
104107
*/
105108
public function getParentHashes()
106109
{
@@ -110,7 +113,7 @@ public function getParentHashes()
110113
/**
111114
* Returns the parent commits.
112115
*
113-
* @return array An array of Commit objects
116+
* @return Commit[] An array of Commit objects
114117
*/
115118
public function getParents()
116119
{
@@ -132,6 +135,9 @@ public function getTreeHash()
132135
return $this->getData('treeHash');
133136
}
134137

138+
/**
139+
* @return Tree
140+
*/
135141
public function getTree()
136142
{
137143
return $this->getData('tree');
@@ -184,7 +190,7 @@ public function getShortMessage($length = 50, $preserve = false, $separator = '.
184190
/**
185191
* Resolves all references associated to this commit.
186192
*
187-
* @return array An array of references (Branch, Tag, Squash)
193+
* @return Reference[] An array of references (Branch, Tag, Squash)
188194
*/
189195
public function resolveReferences()
190196
{
@@ -197,7 +203,7 @@ public function resolveReferences()
197203
* @param bool $local set true to try to locate a commit on local repository
198204
* @param bool $remote set true to try to locate a commit on remote repository
199205
*
200-
* @return array An array of Reference\Branch
206+
* @return Reference[]|Branch[] An array of Reference\Branch
201207
*/
202208
public function getIncludingBranches($local = true, $remote = true)
203209
{

src/Gitonomy/Git/CommitReference.php

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

1515
class CommitReference
1616
{
17+
/**
18+
* @var string
19+
*/
1720
private $hash;
1821

1922
public function __construct($hash)

src/Gitonomy/Git/Diff/Diff.php

+2-10
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
class Diff
2424
{
2525
/**
26-
* @var array
26+
* @var File[]
2727
*/
2828
protected $files;
2929

@@ -62,18 +62,10 @@ public function setRepository(Repository $repository)
6262
}
6363
}
6464

65-
/**
66-
* @return array
67-
*/
68-
public function getRevisions()
69-
{
70-
return $this->revisions;
71-
}
72-
7365
/**
7466
* Get list of files modified in the diff's revision.
7567
*
76-
* @return array An array of Diff\File objects
68+
* @return File[] An array of Diff\File objects
7769
*/
7870
public function getFiles()
7971
{

src/Gitonomy/Git/Diff/File.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class File
5555
protected $isBinary;
5656

5757
/**
58-
* @var array An array of FileChange objects
58+
* @var FileChange[] An array of FileChange objects
5959
*/
6060
protected $changes;
6161

@@ -215,6 +215,9 @@ public function isBinary()
215215
return $this->isBinary;
216216
}
217217

218+
/**
219+
* @return FileChange[]
220+
*/
218221
public function getChanges()
219222
{
220223
return $this->changes;
@@ -236,6 +239,9 @@ public function toArray()
236239
];
237240
}
238241

242+
/**
243+
* @return File
244+
*/
239245
public static function fromArray(array $array)
240246
{
241247
$file = new self($array['old_name'], $array['new_name'], $array['old_mode'], $array['new_mode'], $array['old_index'], $array['new_index'], $array['is_binary']);

src/Gitonomy/Git/Hooks.php

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

1515
use Gitonomy\Git\Exception\InvalidArgumentException;
1616
use Gitonomy\Git\Exception\LogicException;
17+
use Gitonomy\Git\Exception\RuntimeException;
1718

1819
/**
1920
* Hooks collection, aggregated by repository.
@@ -23,7 +24,7 @@
2324
class Hooks
2425
{
2526
/**
26-
* @var Gitonomy\Git\Repository
27+
* @var \Gitonomy\Git\Repository
2728
*/
2829
protected $repository;
2930

@@ -82,7 +83,7 @@ public function setSymlink($name, $file)
8283

8384
$path = $this->getPath($name);
8485
if (false === symlink($file, $path)) {
85-
throw new RuntimeException(sprintf('Unable to create hook "%s"', $name, $path));
86+
throw new RuntimeException(sprintf('Unable to create hook "%s" (%s)', $name, $path));
8687
}
8788
}
8889

@@ -121,6 +122,9 @@ public function remove($name)
121122
unlink($this->getPath($name));
122123
}
123124

125+
/**
126+
* @return string
127+
*/
124128
protected function getPath($name)
125129
{
126130
return $this->repository->getGitDir().'/hooks/'.$name;

src/Gitonomy/Git/Log.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
namespace Gitonomy\Git;
1414

15+
use Gitonomy\Git\Diff\Diff;
1516
use Gitonomy\Git\Exception\ProcessException;
1617
use Gitonomy\Git\Exception\ReferenceNotFoundException;
1718
use Gitonomy\Git\Util\StringHelper;
@@ -136,6 +137,9 @@ public function setLimit($limit)
136137
return $this;
137138
}
138139

140+
/**
141+
* @return Commit
142+
*/
139143
public function getSingleCommit()
140144
{
141145
$limit = $this->limit;
@@ -151,7 +155,7 @@ public function getSingleCommit()
151155
}
152156

153157
/**
154-
* @return array
158+
* @return Commit[]
155159
*/
156160
public function getCommits()
157161
{

src/Gitonomy/Git/Parser/DiffParser.php

+6-4
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ protected function doParse()
2525

2626
while (!$this->isFinished()) {
2727
// 1. title
28-
$vars = $this->consumeRegexp('/diff --git (a\/.*) (b\/.*)\n/');
28+
$vars = $this->consumeRegexp("/diff --git \"?(a\/.*?)\"? \"?(b\/.*?)\"?\n/");
2929
$oldName = $vars[1];
3030
$newName = $vars[2];
3131
$oldIndex = null;
@@ -74,14 +74,15 @@ protected function doParse()
7474
}
7575
$this->consumeNewLine();
7676

77+
//verifying if the file was deleted or created
7778
if ($this->expects('--- ')) {
78-
$oldName = $this->consumeTo("\n");
79+
$oldName = $this->consumeTo("\n") === '/dev/null' ? '/dev/null' : $oldName;
7980
$this->consumeNewLine();
8081
$this->consume('+++ ');
81-
$newName = $this->consumeTo("\n");
82+
$newName = $this->consumeTo("\n") === '/dev/null' ? '/dev/null' : $newName;
8283
$this->consumeNewLine();
8384
} elseif ($this->expects('Binary files ')) {
84-
$vars = $this->consumeRegexp('/(.*) and (.*) differ\n/');
85+
$vars = $this->consumeRegexp('/"?(.*?)"? and "?(.*?)"? differ\n/');
8586
$isBinary = true;
8687
$oldName = $vars[1];
8788
$newName = $vars[2];
@@ -90,6 +91,7 @@ protected function doParse()
9091

9192
$oldName = $oldName === '/dev/null' ? null : substr($oldName, 2);
9293
$newName = $newName === '/dev/null' ? null : substr($newName, 2);
94+
9395
$oldIndex = $oldIndex !== null ?: '';
9496
$newIndex = $newIndex !== null ?: '';
9597
$oldIndex = preg_match('/^0+$/', $oldIndex) ? null : $oldIndex;

src/Gitonomy/Git/Parser/ParserBase.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ protected function consumeHash()
8282
protected function consumeRegexp($regexp)
8383
{
8484
if (!preg_match($regexp.'A', $this->content, $vars, 0, $this->cursor)) {
85-
throw new RuntimeException('No match for regexp '.$regexp.' Upcoming: '.substr($this->content, $this->cursor, 30));
85+
throw new RuntimeException('No match for regexp '.$regexp.' Upcoming: '.substr($this->content, $this->cursor, 500));
8686
}
8787

8888
$this->cursor += strlen($vars[0]);

src/Gitonomy/Git/PushReference.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ class PushReference
2424
{
2525
const ZERO = '0000000000000000000000000000000000000000';
2626

27+
/**
28+
* @var Repository
29+
*/
30+
protected $repository;
2731
/**
2832
* @var string
2933
*/
@@ -86,7 +90,7 @@ public function getAfter()
8690
}
8791

8892
/**
89-
* @return array
93+
* @return Log
9094
*/
9195
public function getLog($excludes = [])
9296
{
@@ -98,6 +102,9 @@ public function getLog($excludes = [])
98102
));
99103
}
100104

105+
/**
106+
* @return string
107+
*/
101108
public function getRevision()
102109
{
103110
if ($this->isDelete()) {

src/Gitonomy/Git/Reference.php

+16-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212

1313
namespace Gitonomy\Git;
1414

15+
use Gitonomy\Git\Exception\ProcessException;
16+
use Gitonomy\Git\Exception\ReferenceNotFoundException;
17+
1518
/**
1619
* Reference in a Git repository.
1720
*
@@ -29,16 +32,25 @@ public function __construct(Repository $repository, $revision, $commitHash = nul
2932
$this->commitHash = $commitHash;
3033
}
3134

35+
/**
36+
* @return string
37+
*/
3238
public function getFullname()
3339
{
3440
return $this->revision;
3541
}
3642

43+
/**
44+
* @return void
45+
*/
3746
public function delete()
3847
{
3948
$this->repository->getReferences()->delete($this->getFullname());
4049
}
4150

51+
/**
52+
* @return string
53+
*/
4254
public function getCommitHash()
4355
{
4456
if (null !== $this->commitHash) {
@@ -55,15 +67,16 @@ public function getCommitHash()
5567
}
5668

5769
/**
58-
* Returns the commit associated to the reference.
59-
*
60-
* @return Commit
70+
* @return Commit Commit associated to the reference.
6171
*/
6272
public function getCommit()
6373
{
6474
return $this->repository->getCommit($this->getCommitHash());
6575
}
6676

77+
/**
78+
* @return Commit
79+
*/
6780
public function getLastModification($path = null)
6881
{
6982
return $this->getCommit()->getLastModification($path);

src/Gitonomy/Git/Reference/Tag.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
*/
2828
class Tag extends Reference
2929
{
30+
protected $data;
31+
3032
public function getName()
3133
{
3234
if (!preg_match('#^refs/tags/(.*)$#', $this->revision, $vars)) {
@@ -65,8 +67,8 @@ public function getCommit()
6567
$parser = new ReferenceParser();
6668
$parser->parse($output);
6769

68-
foreach ($parser->references as $row) {
69-
list($commitHash, $fullname) = $row;
70+
foreach ($parser->references as list($row)) {
71+
$commitHash = $row;
7072
}
7173

7274
return $this->repository->getCommit($commitHash);
@@ -101,7 +103,7 @@ public function getTaggerEmail()
101103
/**
102104
* Returns the authoring date.
103105
*
104-
* @return DateTime A time object
106+
* @return \DateTime A time object
105107
*/
106108
public function getTaggerDate()
107109
{

0 commit comments

Comments
 (0)