GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( ac9b53...c7154a )
by Brad
02:28
created

Truncate::safeTruncate()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 29
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 29
rs 8.5806
cc 4
eloc 12
nc 4
nop 2
1
<?php namespace Gears\String\Methods;
2
////////////////////////////////////////////////////////////////////////////////
3
// __________ __             ________                   __________
4
// \______   \  |__ ______  /  _____/  ____ _____ ______\______   \ _______  ___
5
//  |     ___/  |  \\____ \/   \  ____/ __ \\__  \\_  __ \    |  _//  _ \  \/  /
6
//  |    |   |   Y  \  |_> >    \_\  \  ___/ / __ \|  | \/    |   (  <_> >    <
7
//  |____|   |___|  /   __/ \______  /\___  >____  /__|  |______  /\____/__/\_ \
8
//                \/|__|           \/     \/     \/             \/            \/
9
// -----------------------------------------------------------------------------
10
//          Designed and Developed by Brad Jones <brad @="bjc.id.au" />
11
// -----------------------------------------------------------------------------
12
////////////////////////////////////////////////////////////////////////////////
13
14
use voku\helper\UTF8;
15
16
trait Truncate
17
{
18
    /**
19
     * Truncates the string to a given length.
20
     *
21
     * If $substring is provided, and truncating occurs, the string is further
22
     * truncated so that the substring may be appended without exceeding the
23
     * desired length.
24
     *
25
     * @param  int    $length    Desired length of the truncated string.
26
     *
27
     * @param  string $substring The substring to append if it can fit.
28
     *
29
     * @return static            String after truncating.
30
     */
31
    public function truncate($length, $substring = '')
32
    {
33
        if ($length >= $this->getLength()) return $this;
34
35
        // Need to further trim the string so we can append the substring
36
        $substringLength = UTF8::strlen($substring, $this->encoding);
37
        $length -= $substringLength;
38
39
        $truncated = UTF8::substr
40
        (
41
            $this->scalarString,
42
            0,
43
            $length,
44
            $this->encoding
45
        );
46
47
        return $this->newSelf($truncated . $substring);
48
    }
49
50
    /**
51
     * Truncates the string to a given length,
52
     * while ensuring that it does not split words.
53
     *
54
     * If $substring is provided, and truncating occurs, the string is further
55
     * truncated so that the substring may be appended without exceeding the
56
     * desired length.
57
     *
58
     * @param  int    $length    Desired length of the truncated string.
59
     *
60
     * @param  string $substring The substring to append if it can fit.
61
     *
62
     * @return static
63
     */
64
    public function safeTruncate($length, $substring = '')
65
    {
66
        if ($length >= $this->getLength()) return $this;
67
68
        // Need to further trim the string so we can append the substring
69
        $substringLength = UTF8::strlen($substring, $this->encoding);
70
        $length -= $substringLength;
71
72
        $truncated = UTF8::substr($this->scalarString, 0, $length, $this->encoding);
73
74
        // If the last word was truncated
75
        if (UTF8::strpos($this->scalarString, ' ', $length - 1, $this->encoding) != $length)
76
        {
77
            // Find pos of the last occurrence of a space, get up to that
78
            $lastPos = UTF8::strrpos($truncated, ' ', 0, $this->encoding);
79
80
            if (!is_integer($lastPos))
81
            {
82
                throw new \UnexpectedValueException
83
                (
84
                    'Expecting $lastPos to be integer!'
85
                );
86
            }
87
88
            $truncated = UTF8::substr($truncated, 0, $lastPos, $this->encoding);
89
        }
90
91
        return $this->newSelf($truncated . $substring);
92
    }
93
}
94