Skip to content

Commit 6ba4c35

Browse files
author
alexandresalome
committed
add and use StringHelper - #26
1 parent 72b5672 commit 6ba4c35

File tree

3 files changed

+65
-31
lines changed

3 files changed

+65
-31
lines changed

src/Gitonomy/Git/Commit.php

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

1313
namespace Gitonomy\Git;
1414

15+
use Gitonomy\Git\Util\StringHelper;
16+
1517
/**
1618
* Representation of a Git commit.
1719
*
@@ -275,31 +277,15 @@ public function getShortMessage($length = 50, $preserve = false, $separator = '.
275277

276278
$message = $this->getSubjectMessage();
277279

278-
if (function_exists('mb_substr')) {
279-
if (mb_strlen($message) > $length) {
280-
if ($preserve) {
281-
if (false !== ($breakpoint = mb_strpos($message, ' ', $length))) {
282-
$length = $breakpoint;
283-
}
284-
}
285-
286-
return rtrim(mb_substr($message, 0, $length)) . $separator;
287-
}
288-
289-
return $message;
290-
} else {
291-
if (strlen($message) > $length) {
292-
if ($preserve) {
293-
if (false !== ($breakpoint = strpos($message, ' ', $length))) {
294-
$length = $breakpoint;
295-
}
296-
}
297-
298-
return rtrim(substr($message, 0, $length)) . $separator;
280+
if (StringHelper::strlen($message) > $length) {
281+
if ($preserve && false !== ($breakpoint = StringHelper::strpos($message, ' ', $length))) {
282+
$length = $breakpoint;
299283
}
300284

301-
return $message;
285+
return rtrim(StringHelper::substr($message, 0, $length)).$separator;
302286
}
287+
288+
return $message;
303289
}
304290

305291
/**
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
/**
4+
* This file is part of Gitonomy.
5+
*
6+
* (c) Alexandre Salomé <[email protected]>
7+
* (c) Julien DIDIER <[email protected]>
8+
*
9+
* This source file is subject to the GPL license that is bundled
10+
* with this source code in the file LICENSE.
11+
*/
12+
13+
namespace Gitonomy\Git\Util;
14+
15+
/**
16+
* Helper class to support language particularity.
17+
*
18+
* @author Alexandre Salomé <[email protected]>
19+
*/
20+
class StringHelper
21+
{
22+
private static $encoding = 'utf-8';
23+
24+
public static function setEncoding($encoding)
25+
{
26+
self::$encoding = $encoding;
27+
}
28+
29+
public static function strlen($string)
30+
{
31+
return function_exists('mb_strlen') ? mb_strlen($string, self::$encoding) : strlen($string);
32+
}
33+
34+
public static function substr($string, $start, $length = null)
35+
{
36+
return function_exists('mb_substr') ? mb_substr($string, $start, $length, self::$encoding) : substr($string, $start, $length);
37+
}
38+
39+
public static function strpos($haystack, $needle, $offset = 0)
40+
{
41+
return function_exists('mb_strpos') ? mb_strpos($haystack, $needle, $offset, self::$encoding) : strpos($haystace, $needle, $offset);
42+
}
43+
44+
public static function strrpos($haystack, $needle, $offset = 0)
45+
{
46+
return function_exists('mb_strrpos') ? mb_strrpos($haystack, $needle, $offset, self::$encoding) : strrpos($haystace, $needle, $offset);
47+
}
48+
}

tests/Gitonomy/Git/Tests/CommitTest.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -170,20 +170,20 @@ public function testGetMessage($repository)
170170
*/
171171
public function testGetShortMessage($repository)
172172
{
173+
// tests with a multi-line message
173174
$commit = $repository->getCommit(self::LONGMESSAGE_COMMIT);
174175

175176
$this->assertEquals('Fixed perm...', $commit->getShortMessage(10));
176-
}
177-
178-
/**
179-
* @dataProvider provideFoobar
180-
*/
181-
public function testGetSubjectMessage($repository)
182-
{
183-
$commit = $repository->getCommit(self::LONGMESSAGE_COMMIT);
184-
185177
$this->assertEquals('Fixed perm!!!', $commit->getShortMessage(10, false, '!!!'));
186178
$this->assertEquals('Fixed permissions!!!', $commit->getShortMessage(10, true, '!!!'));
179+
180+
// tests with a single-line message
181+
$commit = $repository->getCommit(self::INITIAL_COMMIT);
182+
183+
$this->assertEquals('Add README', $commit->getShortMessage(20));
184+
$this->assertEquals('A', $commit->getShortMessage(1, false, ''));
185+
$this->assertEquals('Add!!!', $commit->getShortMessage(1, true, '!!!'));
186+
187187
}
188188

189189
/**

0 commit comments

Comments
 (0)