Skip to content

Commit de59c78

Browse files
author
Alexandre Salomé
committed
Merge pull request #37 from gitonomy/feat-exception
Used only gitonomy exceptions
2 parents 211aeef + b190e4c commit de59c78

20 files changed

+130
-81
lines changed

src/Gitonomy/Git/Admin.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
namespace Gitonomy\Git;
1414

15-
use Psr\Log\LoggerInterface;
15+
use Gitonomy\Git\Exception\RuntimeException;
1616
use Symfony\Component\Process\ProcessBuilder;
1717

1818
/**
@@ -54,7 +54,7 @@ public static function init($path, $bare = true, array $options = array())
5454
$process->run();
5555

5656
if (!$process->isSuccessFul()) {
57-
throw new \RuntimeException(sprintf("Error on repository initialization, command wasn't successful (%s). Error output:\n%s", $process->getCommandLine(), $process->getErrorOutput()));
57+
throw new RuntimeException(sprintf("Error on repository initialization, command wasn't successful (%s). Error output:\n%s", $process->getCommandLine(), $process->getErrorOutput()));
5858
}
5959

6060
return new Repository($path, $options);
@@ -63,9 +63,9 @@ public static function init($path, $bare = true, array $options = array())
6363
/**
6464
* Clone a repository to a local path.
6565
*
66-
* @param string $path indicates where to clone repository
67-
* @param string $url url of repository to clone
68-
* @param boolean $bare indicates if repository should be bare or have a working copy
66+
* @param string $path indicates where to clone repository
67+
* @param string $url url of repository to clone
68+
* @param boolean $bare indicates if repository should be bare or have a working copy
6969
* @param array $options options for Repository creation
7070
*
7171
* @return Repository
@@ -80,9 +80,9 @@ public static function cloneTo($path, $url, $bare = true, array $options = array
8080
/**
8181
* Mirrors a repository (fetch all revisions, not only branches).
8282
*
83-
* @param string $path indicates where to clone repository
84-
* @param string $url url of repository to clone
85-
* @param array $options options for Repository creation
83+
* @param string $path indicates where to clone repository
84+
* @param string $url url of repository to clone
85+
* @param array $options options for Repository creation
8686
*
8787
* @return Repository
8888
*/
@@ -94,10 +94,10 @@ public static function mirrorTo($path, $url, array $options = array())
9494
/**
9595
* Internal method to launch effective ``git clone`` command.
9696
*
97-
* @param string $path indicates where to clone repository
98-
* @param string $url url of repository to clone
99-
* @param array $args arguments to be added to the command-line
100-
* @param array $options options for Repository creation
97+
* @param string $path indicates where to clone repository
98+
* @param string $url url of repository to clone
99+
* @param array $args arguments to be added to the command-line
100+
* @param array $options options for Repository creation
101101
*
102102
* @return Repository
103103
*/
@@ -120,7 +120,7 @@ private static function cloneRepository($path, $url, array $args = array(), arra
120120
$process->run();
121121

122122
if (!$process->isSuccessFul()) {
123-
throw new \RuntimeException(sprintf('Error while initializing repository: %s', $process->getErrorOutput()));
123+
throw new RuntimeException(sprintf('Error while initializing repository: %s', $process->getErrorOutput()));
124124
}
125125

126126
return new Repository($path, $options);

src/Gitonomy/Git/Blame.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
namespace Gitonomy\Git;
1414

15+
use Gitonomy\Git\Exception\InvalidArgumentException;
1516
use Gitonomy\Git\Parser\BlameParser;
1617

1718
/**
@@ -42,13 +43,13 @@ public function __construct(Repository $repository, $revision, $file, $lineRange
4243
public function getLine($number)
4344
{
4445
if ($number < 1) {
45-
throw new \InvalidArgumentException('Line number should be at least 1');
46+
throw new InvalidArgumentException('Line number should be at least 1');
4647
}
4748

4849
$lines = $this->getLines();
4950

5051
if (!isset($lines[$number])) {
51-
throw new \InvalidArgumentException('Line does not exist');
52+
throw new InvalidArgumentException('Line does not exist');
5253
}
5354

5455
return $lines[$number];

src/Gitonomy/Git/Blob.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function getHash()
6060
/**
6161
* Returns content of the blob.
6262
*
63-
* @throws RuntimeException Error occurred while getting content of blob
63+
* @throws ProcessException Error occurred while getting content of blob
6464
*/
6565
public function getContent()
6666
{

src/Gitonomy/Git/Commit.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
use Gitonomy\Git\Util\StringHelper;
1616
use Gitonomy\Git\Diff\Diff;
1717
use Gitonomy\Git\Exception\ReferenceNotFoundException;
18+
use Gitonomy\Git\Exception\InvalidArgumentException;
19+
use Gitonomy\Git\Exception\ProcessException;
1820

1921
/**
2022
* Representation of a Git commit.
@@ -131,7 +133,7 @@ public function __construct(Repository $repository, $hash)
131133
/**
132134
* Initializes the commit, which means read data about it and fill object.
133135
*
134-
* @throws RuntimeException An error occurred during read of data.
136+
* @throws ReferenceNotFoundException An error occurred during read of data.
135137
*/
136138
private function initialize()
137139
{
@@ -142,7 +144,7 @@ private function initialize()
142144
$parser = new Parser\CommitParser();
143145
try {
144146
$result = $this->repository->run('cat-file', array('commit', $this->hash));
145-
} catch (\RuntimeException $e) {
147+
} catch (ProcessException $e) {
146148
throw new ReferenceNotFoundException(sprintf('Can not find reference "%s"', $this->hash));
147149
}
148150

@@ -328,12 +330,12 @@ public function getIncludingBranches($local = true, $remote = true)
328330
} elseif (!$local && $remote) {
329331
$arguments[] = '-r';
330332
} elseif (!$local && !$remote) {
331-
throw new \InvalidArgumentException('You should a least set one argument to true');
333+
throw new InvalidArgumentException('You should a least set one argument to true');
332334
}
333335

334336
try {
335337
$result = $this->repository->run('branch', $arguments);
336-
} catch (\Exception $e) {
338+
} catch (ProcessException $e) {
337339
return array();
338340
}
339341

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Gitonomy\Git\Exception;
4+
5+
class InvalidArgumentException extends \InvalidArgumentException implements GitExceptionInterface
6+
{
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Gitonomy\Git\Exception;
4+
5+
class LogicException extends \LogicException implements GitExceptionInterface
6+
{
7+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Gitonomy\Git\Exception;
4+
5+
use Symfony\Component\Process\Process;
6+
7+
class ProcessException extends RuntimeException implements GitExceptionInterface
8+
{
9+
protected $process;
10+
11+
public function __construct(Process $process)
12+
{
13+
parent::__construct("Error while running git command:\n".
14+
$process->getCommandLine()."\n".
15+
"\n".
16+
$process->getErrorOutput()
17+
);
18+
19+
$this->process = $process;
20+
}
21+
22+
public function getErrorOutput()
23+
{
24+
return $this->process->getErrorOutput();
25+
}
26+
27+
public function getOutput()
28+
{
29+
return $this->process->getOutput();
30+
}
31+
}

src/Gitonomy/Git/Exception/RuntimeException.php

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,6 @@
22

33
namespace Gitonomy\Git\Exception;
44

5-
use Symfony\Component\Process\Process;
6-
75
class RuntimeException extends \RuntimeException implements GitExceptionInterface
86
{
9-
protected $process;
10-
11-
public function __construct(Process $process)
12-
{
13-
parent::__construct("Error while running git command:\n".
14-
$process->getCommandLine()."\n".
15-
"\n".
16-
$process->getErrorOutput()
17-
);
18-
19-
$this->process = $process;
20-
}
21-
22-
public function getErrorOutput()
23-
{
24-
return $this->process->getErrorOutput();
25-
}
26-
27-
public function getOutput()
28-
{
29-
return $this->process->getOutput();
30-
}
317
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Gitonomy\Git\Exception;
4+
5+
class UnexpectedValueException extends \UnexpectedValueException implements GitExceptionInterface
6+
{
7+
}

src/Gitonomy/Git/Hooks.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212

1313
namespace Gitonomy\Git;
1414

15+
use Gitonomy\Git\Exception\InvalidArgumentException;
16+
use Gitonomy\Git\Exception\LogicException;
17+
1518
/**
1619
* Hooks collection, aggregated by repository.
1720
*
@@ -56,7 +59,7 @@ public function has($name)
5659
public function get($name)
5760
{
5861
if (!$this->has($name)) {
59-
throw new \InvalidArgumentException(sprintf('Hook named "%s" is not present', $name));
62+
throw new InvalidArgumentException(sprintf('Hook named "%s" is not present', $name));
6063
}
6164

6265
return file_get_contents($this->getPath($name));
@@ -74,12 +77,12 @@ public function get($name)
7477
public function setSymlink($name, $file)
7578
{
7679
if ($this->has($name)) {
77-
throw new \LogicException(sprintf('A hook "%s" is already defined', $name));
80+
throw new LogicException(sprintf('A hook "%s" is already defined', $name));
7881
}
7982

8083
$path = $this->getPath($name);
8184
if (false === symlink($file, $path)) {
82-
throw new \RuntimeException(sprintf('Unable to create hook "%s"', $name, $path));
85+
throw new RuntimeException(sprintf('Unable to create hook "%s"', $name, $path));
8386
}
8487
}
8588

@@ -94,7 +97,7 @@ public function setSymlink($name, $file)
9497
public function set($name, $content)
9598
{
9699
if ($this->has($name)) {
97-
throw new \LogicException(sprintf('A hook "%s" is already defined', $name));
100+
throw new LogicException(sprintf('A hook "%s" is already defined', $name));
98101
}
99102

100103
$path = $this->getPath($name);
@@ -112,7 +115,7 @@ public function set($name, $content)
112115
public function remove($name)
113116
{
114117
if (!$this->has($name)) {
115-
throw new \LogicException(sprintf('The hook "%s" was not found', $name));
118+
throw new LogicException(sprintf('The hook "%s" was not found', $name));
116119
}
117120

118121
unlink($this->getPath($name));

src/Gitonomy/Git/Parser/CommitParser.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
namespace Gitonomy\Git\Parser;
1414

15+
use Gitonomy\Git\Exception\RuntimeException;
16+
1517
class CommitParser extends ParserBase
1618
{
1719
public $tree;
@@ -53,7 +55,7 @@ protected function doParse()
5355
protected function consumeNameEmailDate()
5456
{
5557
if (!preg_match('/(([^\n]*) <([^\n]*)> (\d+ [+-]\d{4}))/A', $this->content, $vars, 0, $this->cursor)) {
56-
throw new \RuntimeException('Unable to parse name, email and date');
58+
throw new RuntimeException('Unable to parse name, email and date');
5759
}
5860

5961
$this->cursor += strlen($vars[1]);
@@ -66,7 +68,7 @@ protected function parseDate($text)
6668
$date = \DateTime::createFromFormat('U e O', $text.' UTC');
6769

6870
if (!$date instanceof \DateTime) {
69-
throw new \RuntimeException(sprintf('Unable to convert "%s" to datetime', $text));
71+
throw new RuntimeException(sprintf('Unable to convert "%s" to datetime', $text));
7072
}
7173

7274
return $date;

src/Gitonomy/Git/Parser/ParserBase.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
namespace Gitonomy\Git\Parser;
1414

15-
use Gitonomy\Git\Util\StringHelper;
15+
use Gitonomy\Git\Exception\RuntimeException;
1616

1717
abstract class ParserBase
1818
{
@@ -60,7 +60,7 @@ protected function expects($expected)
6060
protected function consumeShortHash()
6161
{
6262
if (!preg_match('/([A-Za-z0-9]{7,40})/A', $this->content, $vars, null, $this->cursor)) {
63-
throw new \RuntimeException('No short hash found: '.substr($this->content, $this->cursor, 7));
63+
throw new RuntimeException('No short hash found: '.substr($this->content, $this->cursor, 7));
6464
}
6565

6666
$this->cursor += strlen($vars[1]);
@@ -71,7 +71,7 @@ protected function consumeShortHash()
7171
protected function consumeHash()
7272
{
7373
if (!preg_match('/([A-Za-z0-9]{40})/A', $this->content, $vars, null, $this->cursor)) {
74-
throw new \RuntimeException('No hash found: '.substr($this->content, $this->cursor, 40));
74+
throw new RuntimeException('No hash found: '.substr($this->content, $this->cursor, 40));
7575
}
7676

7777
$this->cursor += 40;
@@ -82,7 +82,7 @@ protected function consumeHash()
8282
protected function consumeRegexp($regexp)
8383
{
8484
if (!preg_match($regexp.'A', $this->content, $vars, null, $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, 30));
8686
}
8787

8888
$this->cursor += strlen($vars[0]);
@@ -95,7 +95,7 @@ protected function consumeTo($text)
9595
$pos = strpos($this->content, $text, $this->cursor);
9696

9797
if (false === $pos) {
98-
throw new \RuntimeException(sprintf('Unable to find "%s"', $text));
98+
throw new RuntimeException(sprintf('Unable to find "%s"', $text));
9999
}
100100

101101
$result = substr($this->content, $this->cursor, $pos - $this->cursor);
@@ -109,7 +109,7 @@ protected function consume($expected)
109109
$length = strlen($expected);
110110
$actual = substr($this->content, $this->cursor, $length);
111111
if ($actual !== $expected) {
112-
throw new \RuntimeException(sprintf('Expected "%s", but got "%s" (%s)', $expected, $actual, substr($this->content, $this->cursor, 10)));
112+
throw new RuntimeException(sprintf('Expected "%s", but got "%s" (%s)', $expected, $actual, substr($this->content, $this->cursor, 10)));
113113
}
114114
$this->cursor += $length;
115115

src/Gitonomy/Git/PushReference.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
namespace Gitonomy\Git;
1414

15+
use Gitonomy\Git\Exception\LogicException;
16+
1517
/**
1618
* Push reference contains a commit interval. This object aggregates methods
1719
* for this interval.
@@ -99,7 +101,7 @@ public function getLog($excludes = array())
99101
public function getRevision()
100102
{
101103
if ($this->isDelete()) {
102-
throw new \LogicException('No log on deletion');
104+
throw new LogicException('No log on deletion');
103105
}
104106

105107
if ($this->isCreate()) {

src/Gitonomy/Git/Reference/Branch.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
namespace Gitonomy\Git\Reference;
1414

15+
use Gitonomy\Git\Exception\RuntimeException;
1516
use Gitonomy\Git\Reference;
1617

1718
/**
@@ -36,7 +37,7 @@ public function getName()
3637
return $vars['remote'].'/'.$vars['name'];
3738
}
3839

39-
throw new \RuntimeException(sprintf('Cannot extract branch name from "%s"', $this->fullname));
40+
throw new RuntimeException(sprintf('Cannot extract branch name from "%s"', $this->fullname));
4041
}
4142

4243
public function isRemote()

0 commit comments

Comments
 (0)