StringHelper   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 16
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 7
dl 0
loc 16
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A shortenNameToFirst() 0 6 2
A trim() 0 6 2
1
<?php
2
3
namespace Dpeuscher\Util\Strings;
4
5
/**
6
 * @category  util
7
 * @copyright Copyright (c) 2018 Dominik Peuscher
8
 */
9
class StringHelper
10
{
11
    public static function trim(string $text, int $limit): string
12
    {
13
        if (strlen($text) > $limit) {
14
            return substr($text, 0, $limit - 3) . '...';
15
        }
16
        return $text;
17
    }
18
19
    public static function shortenNameToFirst(string $name): string
20
    {
21
        if (preg_match('/^([a-zA-Z0-9]+)(?:\s+[a-zA-Z0-9]+)*\s+([a-zA-Z0-9])[a-zA-Z0-9]*$/', $name, $matches)) {
22
            return $matches[1] . ' ' . $matches[2];
23
        }
24
        return $name;
25
    }
26
}
27