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.

Pad   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 1
dl 0
loc 124
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
B pad() 0 22 4
A padLeft() 0 4 1
A padRight() 0 4 1
A padBoth() 0 5 1
B applyPadding() 0 26 3
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 Pad
17
{
18
    /**
19
     * Pads the string to a given length with $padStr.
20
     *
21
     * If length is less than or equal to the length of the string, no padding
22
     * takes places. The default string used for padding is a space, and the
23
     * default type (one of 'left', 'right', 'both') is 'right'.
24
     *
25
     * @param  int    $length  Desired string length after padding.
26
     *
27
     * @param  string $padStr  String used to pad, defaults to space.
28
     *
29
     * @param  string $padType One of 'left', 'right', 'both'.
30
     *
31
     * @return static          String after being padded.
32
     *
33
     * @throws \InvalidArgumentException If $padType isn't one of 'right',
34
     *                                   'left' or 'both'.
35
     */
36
    public function pad($length, $padStr = ' ', $padType = 'right')
37
    {
38
        if (!in_array($padType, ['left', 'right', 'both'], true))
39
        {
40
            throw new \InvalidArgumentException
41
            (
42
                'Pad expects $padType '."to be one of 'left', 'right' or 'both'"
43
            );
44
        }
45
46
        switch ($padType)
47
        {
48
            case 'left':
49
                return $this->padLeft($length, $padStr);
50
51
            case 'right':
52
                return $this->padRight($length, $padStr);
53
54
            default:
55
                return $this->padBoth($length, $padStr);
56
        }
57
    }
58
59
    /**
60
     * Adds padding to the left of the string.
61
     *
62
     * @param  int    $length Desired string length after padding.
63
     *
64
     * @param  string $padStr String used to pad, defaults to space.
65
     *
66
     * @return static         String with left padding.
67
     */
68
    public function padLeft($length, $padStr = ' ')
69
    {
70
        return $this->applyPadding($length - $this->getLength(), 0, $padStr);
71
    }
72
73
    /**
74
     * Adds padding to the right of the string.
75
     *
76
     * @param  int    $length Desired string length after padding.
77
     *
78
     * @param  string $padStr String used to pad, defaults to space.
79
     *
80
     * @return static         String with right padding.
81
     */
82
    public function padRight($length, $padStr = ' ')
83
    {
84
        return $this->applyPadding(0, $length - $this->getLength(), $padStr);
85
    }
86
87
    /**
88
     * Adds padding to both sides of the string, equally.
89
     *
90
     * @param  int    $length Desired string length after padding.
91
     *
92
     * @param  string $padStr String used to pad, defaults to space.
93
     *
94
     * @return static         String with padding applied to both sides.
95
     */
96
    public function padBoth($length, $padStr = ' ')
97
    {
98
        $padding = $length - $this->getLength();
99
        return $this->applyPadding(floor($padding / 2), ceil($padding / 2), $padStr);
100
    }
101
102
    /**
103
     * Pad internal helper, adds padding to the left and right of the string.
104
     *
105
     * @param  int    $left   Length of left padding.
106
     *
107
     * @param  int    $right  Length of right padding.
108
     *
109
     * @param  string $padStr String used to pad, default is a space.
110
     *
111
     * @return static         String with padding applied.
112
     */
113
    protected function applyPadding($left = 0, $right = 0, $padStr = ' ')
114
    {
115
        $length = UTF8::strlen($padStr, $this->encoding);
116
        $strLength = $this->getLength();
117
        $paddedLength = $strLength + $left + $right;
118
119
        if (!$length || $paddedLength <= $strLength) return $this;
120
121
        $leftPadding = UTF8::substr
122
        (
123
            UTF8::str_repeat($padStr, ceil($left / $length)),
124
            0,
125
            $left,
126
            $this->encoding
127
        );
128
129
        $rightPadding = UTF8::substr
130
        (
131
            UTF8::str_repeat($padStr, ceil($right / $length)),
132
            0,
133
            $right,
134
            $this->encoding
135
        );
136
137
        return $this->newSelf($leftPadding.$this->scalarString.$rightPadding);
138
    }
139
}
140