Skip to content

Commit 7459fc2

Browse files
author
alexandresalome
committed
convert all string methods to StringHelper
1 parent aef6efe commit 7459fc2

File tree

5 files changed

+30
-21
lines changed

5 files changed

+30
-21
lines changed

src/Gitonomy/Git/Commit.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ public function getShortHash()
190190
*/
191191
public function getFixedShortHash($length = 6)
192192
{
193-
return substr($this->hash, 0, $length);
193+
return StringHelper::substr($this->hash, 0, $length);
194194
}
195195

196196
/**
@@ -252,7 +252,7 @@ public function getTree()
252252
public function getLastModification($path, $lastHash = null)
253253
{
254254
if (preg_match('#^/#', $path)) {
255-
$path = substr($path, 1);
255+
$path = StringHelper::substr($path, 1);
256256
}
257257

258258
$result = $this->repository->run('log', array('--format=%H', '-n', 1, $this->hash, '--', $path));
@@ -319,7 +319,7 @@ public function getIncludingBranches($local = true, $remote = true)
319319
}
320320

321321
$branchesName = explode("\n", trim(str_replace('*', '', $result)));
322-
$branchesName = array_filter($branchesName, function($v) { return false === strpos($v, '->');});
322+
$branchesName = array_filter($branchesName, function($v) { return false === StringHelper::strpos($v, '->');});
323323
$branchesName = array_map('trim', $branchesName);
324324

325325
$references = $this->repository->getReferences();
@@ -328,7 +328,7 @@ public function getIncludingBranches($local = true, $remote = true)
328328
foreach ($branchesName as $branchName) {
329329
if (false === $local) {
330330
$branches[] = $references->getRemoteBranch($branchName);
331-
} elseif (0 === strrpos($branchName, 'remotes/')) {
331+
} elseif (0 === StringHelper::strrpos($branchName, 'remotes/')) {
332332
$branches[] = $references->getRemoteBranch(str_replace('remotes/', '', $branchName));
333333
} else {
334334
$branches[] = $references->getBranch($branchName);

src/Gitonomy/Git/Parser/CommitParser.php

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

1313
namespace Gitonomy\Git\Parser;
1414

15+
use Gitonomy\Git\Util\StringHelper;
16+
1517
class CommitParser extends ParserBase
1618
{
1719
public $tree;
@@ -56,7 +58,7 @@ protected function consumeNameEmailDate()
5658
throw new \RuntimeException('Unable to parse name, email and date');
5759
}
5860

59-
$this->cursor += mb_strlen($vars[1]);
61+
$this->cursor += StringHelper::strlen($vars[1]);
6062

6163
return array($vars[2], $vars[3], $vars[4]);
6264
}

src/Gitonomy/Git/Parser/DiffParser.php

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

1515
use Gitonomy\Git\Diff\File;
1616
use Gitonomy\Git\Diff\FileChange;
17+
use Gitonomy\Git\Util\StringHelper;
1718
use Gitonomy\Git\Repository;
1819

1920
class DiffParser extends ParserBase
@@ -99,8 +100,8 @@ protected function doParse()
99100
}
100101
}
101102

102-
$oldName = $oldName === '/dev/null' ? null : substr($oldName, 2);
103-
$newName = $newName === '/dev/null' ? null : substr($newName, 2);
103+
$oldName = $oldName === '/dev/null' ? null : StringHelper::substr($oldName, 2);
104+
$newName = $newName === '/dev/null' ? null : StringHelper::substr($newName, 2);
104105
$oldIndex = preg_match('/^0+$/', $oldIndex) ? null : $oldIndex;
105106
$newIndex = preg_match('/^0+$/', $newIndex) ? null : $newIndex;
106107
$file = new File($this->repository, $oldName, $newName, $oldMode, $newMode, $oldIndex, $newIndex, $isBinary);

src/Gitonomy/Git/Parser/ParserBase.php

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

1313
namespace Gitonomy\Git\Parser;
1414

15+
use Gitonomy\Git\Util\StringHelper;
16+
1517
abstract class ParserBase
1618
{
1719
protected $cursor;
@@ -24,7 +26,7 @@ public function parse($content)
2426
{
2527
$this->cursor = 0;
2628
$this->content = $content;
27-
$this->length = strlen($this->content);
29+
$this->length = StringHelper::strlen($this->content);
2830

2931
$this->doParse();
3032
}
@@ -36,16 +38,16 @@ protected function isFinished()
3638

3739
protected function consumeAll()
3840
{
39-
$rest = substr($this->content, $this->cursor);
40-
$this->cursor += strlen($rest);
41+
$rest = StringHelper::substr($this->content, $this->cursor);
42+
$this->cursor += StringHelper::strlen($rest);
4143

4244
return $rest;
4345
}
4446

4547
protected function expects($expected)
4648
{
47-
$length = strlen($expected);
48-
$actual = substr($this->content, $this->cursor, $length);
49+
$length = StringHelper::strlen($expected);
50+
$actual = StringHelper::substr($this->content, $this->cursor, $length);
4951
if ($actual !== $expected) {
5052
return false;
5153
}
@@ -58,18 +60,18 @@ protected function expects($expected)
5860
protected function consumeShortHash()
5961
{
6062
if (!preg_match('/([A-Za-z0-9]{7,40})/A', $this->content, $vars, null, $this->cursor)) {
61-
throw new \RuntimeException('No short hash found: '.substr($this->content, $this->cursor, 7));
63+
throw new \RuntimeException('No short hash found: '.StringHelper::substr($this->content, $this->cursor, 7));
6264
}
6365

64-
$this->cursor += strlen($vars[1]);
66+
$this->cursor += StringHelper::strlen($vars[1]);
6567

6668
return $vars[1];
6769
}
6870

6971
protected function consumeHash()
7072
{
7173
if (!preg_match('/([A-Za-z0-9]{40})/A', $this->content, $vars, null, $this->cursor)) {
72-
throw new \RuntimeException('No hash found: '.substr($this->content, $this->cursor, 40));
74+
throw new \RuntimeException('No hash found: '.StringHelper::substr($this->content, $this->cursor, 40));
7375
}
7476

7577
$this->cursor += 40;
@@ -83,29 +85,29 @@ protected function consumeRegexp($regexp)
8385
throw new \RuntimeException('No match for regexp '.$regexp);
8486
}
8587

86-
$this->cursor += strlen($vars[0]);
88+
$this->cursor += StringHelper::strlen($vars[0]);
8789

8890
return $vars;
8991
}
9092

9193
protected function consumeTo($text)
9294
{
93-
$pos = strpos($this->content, $text, $this->cursor);
95+
$pos = StringHelper::strpos($this->content, $text, $this->cursor);
9496

9597
if (false === $pos) {
9698
throw new \RuntimeException(sprintf('Unable to find "%s"', $text));
9799
}
98100

99-
$result = substr($this->content, $this->cursor, $pos - $this->cursor);
101+
$result = StringHelper::substr($this->content, $this->cursor, $pos - $this->cursor);
100102
$this->cursor = $pos;
101103

102104
return $result;
103105
}
104106

105107
protected function consume($expected)
106108
{
107-
$length = strlen($expected);
108-
$actual = substr($this->content, $this->cursor, $length);
109+
$length = StringHelper::strlen($expected);
110+
$actual = StringHelper::substr($this->content, $this->cursor, $length);
109111
if ($actual !== $expected) {
110112
throw new \RuntimeException(sprintf('Expected "%s", but got "%s"', $expected, $actual));
111113
}

src/Gitonomy/Git/Util/StringHelper.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,12 @@ public static function strlen($string)
3131
return function_exists('mb_strlen') ? mb_strlen($string, self::$encoding) : strlen($string);
3232
}
3333

34-
public static function substr($string, $start, $length = null)
34+
public static function substr($string, $start, $length = false)
3535
{
36+
if (false === $length) {
37+
$length = self::strlen($string);
38+
}
39+
3640
return function_exists('mb_substr') ? mb_substr($string, $start, $length, self::$encoding) : substr($string, $start, $length);
3741
}
3842

0 commit comments

Comments
 (0)