Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ Changes since [v1.6.0](https://github.com/gitonomy/gitlib/releases/tag/v1.6.0).

### Minor

- Replace `Gitonomy\Git\Util\StringHelper` with `symfony/string`'s `CodePointString`, which
covers the same UTF-8-aware `strlen`/`substr`/`strpos`/`strrpos` operations.
**Breaking:** `StringHelper` is removed, along with its configurable encoding
(`getEncoding()`/`setEncoding()`); the library now consistently targets `UTF-8`.
([#252](https://github.com/gitonomy/gitlib/pull/252))
- Modernize for PHP 8.4+, Symfony 6.4/7.4/8.1+ and PHPUnit 12: full property/parameter/return
type coverage, `readonly`/`final` where applicable, PHPUnit 12 attributes, and new
php-cs-fixer/PHPStan/Castor tooling.
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
"require": {
"php": "^8.4",
"ext-pcre": "*",
"symfony/polyfill-mbstring": "^1.7",
"symfony/process": "^6.4 || ^7.4 || ^8.1"
"symfony/process": "^6.4 || ^7.4 || ^8.1",
"symfony/string": "^7.4.6 || ^8.0.6"
},
"require-dev": {
"ext-fileinfo": "*",
Expand Down
18 changes: 10 additions & 8 deletions src/Gitonomy/Git/Commit.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use Gitonomy\Git\Exception\ProcessException;
use Gitonomy\Git\Exception\ReferenceNotFoundException;
use Gitonomy\Git\Reference\Branch;
use Gitonomy\Git\Util\StringHelper;
use Symfony\Component\String\CodePointString;

/**
* Representation of a Git commit.
Expand Down Expand Up @@ -139,7 +139,7 @@ public function getShortHash(): string
*/
public function getFixedShortHash(int $length = 6): string
{
return StringHelper::substr($this->revision, 0, $length);
return new CodePointString($this->revision)->slice(0, $length)->toString();
}

/**
Expand Down Expand Up @@ -191,7 +191,7 @@ public function getTree(): Tree
public function getLastModification(?string $path = null): self
{
if (null !== $path && str_starts_with($path, '/')) {
$path = StringHelper::substr($path, 1);
$path = new CodePointString($path)->slice(1)->toString();
}

if ($getWorkingDir = $this->repository->getWorkingDir()) {
Expand All @@ -216,12 +216,14 @@ public function getShortMessage(int $length = 50, bool $preserve = false, string
{
$message = $this->getSubjectMessage();

if (StringHelper::strlen($message) > $length) {
if ($preserve && false !== ($breakpoint = StringHelper::strpos($message, ' ', $length))) {
$codePointMessage = new CodePointString($message);

if ($codePointMessage->length() > $length) {
if ($preserve && null !== ($breakpoint = $codePointMessage->indexOf(' ', $length))) {
$length = $breakpoint;
}

return rtrim(StringHelper::substr($message, 0, $length)).$separator;
return rtrim($codePointMessage->slice(0, $length)->toString()).$separator;
}

return $message;
Expand Down Expand Up @@ -269,7 +271,7 @@ public function getIncludingBranches(bool $local = true, bool $remote = true): a

$branchesName = explode("\n", trim(str_replace('*', '', $result)));
$branchesName = array_filter($branchesName, static function ($v) {
return false === StringHelper::strpos($v, '->');
return null === new CodePointString($v)->indexOf('->');
});
$branchesName = array_map('trim', $branchesName);

Expand All @@ -279,7 +281,7 @@ public function getIncludingBranches(bool $local = true, bool $remote = true): a
foreach ($branchesName as $branchName) {
if (false === $local) {
$branches[] = $references->getRemoteBranch($branchName);
} elseif (0 === StringHelper::strrpos($branchName, 'remotes/')) {
} elseif (0 === new CodePointString($branchName)->indexOfLast('remotes/')) {
$branches[] = $references->getRemoteBranch(str_replace('remotes/', '', $branchName));
} else {
$branches[] = $references->getBranch($branchName);
Expand Down
3 changes: 1 addition & 2 deletions src/Gitonomy/Git/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use Gitonomy\Git\Diff\Diff;
use Gitonomy\Git\Exception\ProcessException;
use Gitonomy\Git\Exception\ReferenceNotFoundException;
use Gitonomy\Git\Util\StringHelper;

/**
* @author Alexandre Salomé <alexandre.salome@gmail.com>
Expand Down Expand Up @@ -120,7 +119,7 @@ public function getSingleCommit(): Commit
*/
public function getCommits(): array
{
$args = ['--encoding='.StringHelper::getEncoding(), '--format=raw'];
$args = ['--encoding=UTF-8', '--format=raw'];

if (null !== $this->offset) {
$args[] = '--skip='.$this->offset;
Expand Down
4 changes: 2 additions & 2 deletions src/Gitonomy/Git/Reference/Branch.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use Gitonomy\Git\Exception\ProcessException;
use Gitonomy\Git\Exception\RuntimeException;
use Gitonomy\Git\Reference;
use Gitonomy\Git\Util\StringHelper;
use Symfony\Component\String\CodePointString;

/**
* Representation of a branch reference.
Expand Down Expand Up @@ -82,7 +82,7 @@ public function isMergedTo(string $destinationBranchName = 'master', bool $compa

$output = explode("\n", trim(str_replace(['*', 'remotes/'], '', $result)));
$filtered_output = array_filter($output, static function ($v) {
return false === StringHelper::strpos($v, '->');
return null === new CodePointString($v)->indexOf('->');
});
$trimmed_output = array_map('trim', $filtered_output);

Expand Down
57 changes: 0 additions & 57 deletions src/Gitonomy/Git/Util/StringHelper.php

This file was deleted.