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

File tree

18 files changed

+123
-57
lines changed

18 files changed

+123
-57
lines changed

src/Gitonomy/Git/Blame.php

Lines changed: 1 addition & 3 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 12 additions & 6 deletions
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

Lines changed: 3 additions & 0 deletions
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

Lines changed: 2 additions & 10 deletions
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

Lines changed: 7 additions & 1 deletion
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

Lines changed: 6 additions & 2 deletions
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

Lines changed: 5 additions & 1 deletion
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

Lines changed: 6 additions & 4 deletions
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

Lines changed: 1 addition & 1 deletion
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]);

0 commit comments

Comments
 (0)