• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

PHP-Alchemist / coreFiles / 15423302022

03 Jun 2025 04:58PM UTC coverage: 92.411%. First build
15423302022

Pull #9

github

druid628
CS Fixes
Pull Request #9: [Release] v3.0.0

144 of 175 new or added lines in 16 files covered. (82.29%)

548 of 593 relevant lines covered (92.41%)

4.95 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/Abstract/AbstractString.php
1
<?php
2

3
namespace PHPAlchemist\Abstract;
4

5
use PHPAlchemist\Contract\IndexedArrayInterface;
6
use PHPAlchemist\Contract\StringInterface;
7
use PHPAlchemist\Type\Collection;
8
use PHPAlchemist\Type\Twine;
9

10
/**
11
 * Abstract class for String.
12
 */
13
abstract class AbstractString implements StringInterface
14
{
15
    const BEGINNING_OF_STRING_POSITION = 0;
16

17
    public function __construct(protected ?string $value = null)
29✔
18
    {
19
    }
29✔
20

21
    public function __toString() : string
14✔
22
    {
23
        return ($this->getValue()) ?: '';
14✔
24
    }
25

26
    public function getValue() : ?string
20✔
27
    {
28
        return $this->value;
20✔
29
    }
30

31
    public function setValue($value) : StringInterface
2✔
32
    {
33
        $this->value = $value;
2✔
34

35
        return $this;
2✔
36
    }
37

38
    public function contains(mixed $needle, bool $caseInsensitive = false) : bool
1✔
39
    {
40
        if ($caseInsensitive) {
1✔
41
            return str_contains(mb_strtolower($this->value), mb_strtolower($needle));
1✔
42
        }
43

44
        return str_contains($this->value, $needle);
1✔
45
    }
46

47
    /** @inheritDoc */
48
    public function endsWith(mixed $needle) : bool
1✔
49
    {
50
        return str_ends_with($this->value, $needle);
1✔
51
    }
52

53
    /** @inheritDoc */
54
    public function equals(string|StringInterface $comparitive) : bool
3✔
55
    {
56
        return (string) $comparitive === $this->value;
3✔
57
    }
58

59
    /**
60
     * @inheritDoc
61
     */
62
    public function hasValue() : bool
1✔
63
    {
64
        return !is_null($this->value);
1✔
65
    }
66

67
    /** @inheritDoc */
68
    public function indexOf(string $needle, int $startIndex = 0) : int|false
2✔
69
    {
70
        return strpos($this->value, $needle, $startIndex);
2✔
71
    }
72

73
    /** @inheritDoc */
74
    public function insert(string $insertion, int $offset) : void
2✔
75
    {
76
        $explosion[0] = substr($this->value, 0, $offset);
2✔
77
        $explosion[1] = $insertion;
2✔
78
        $explosion[2] = substr($this->value, $offset);
2✔
79

80
        $this->value = implode('', $explosion);
2✔
81
    }
82

83
    /** @inheritDoc */
84
    public function isNullOrEmpty() : bool
1✔
85
    {
86
        return is_null($this->value) || empty($this->value);
1✔
87
    }
88

89
    /** @inheritDoc */
90
    public function lastIndexOf(string $needle, int $startIndex = 0) : int|false
1✔
91
    {
92
        return strrpos($this->value, $needle, $startIndex);
1✔
93
    }
94

95
    /**
96
     * @inheritDoc
97
     */
98
    public function length() : int
2✔
99
    {
100
        return strlen($this->value);
2✔
101
    }
102

103
    /**
104
     * Convert string to lower case.
105
     *
106
     * @return string
107
     */
108
    public function lower() : string
1✔
109
    {
110
        return mb_strtolower($this->getValue());
1✔
111
    }
112

113
    /** @inheritDoc */
114
    public function padBoth(int $length, string $padValue = ' ') : void
1✔
115
    {
116
        $this->value = str_pad($this->value, $length, $padValue, STR_PAD_BOTH);
1✔
117
    }
118

119
    /** @inheritDoc */
120
    public function padLeft(int $length, string $padValue = ' ') : void
1✔
121
    {
122
        $this->value = str_pad($this->value, $length, $padValue, STR_PAD_LEFT);
1✔
123
    }
124

125
    /** @inheritDoc */
126
    public function padRight(int $length, string $padValue = ' ') : void
1✔
127
    {
128
        $this->value = str_pad($this->value, $length, $padValue, STR_PAD_RIGHT);
1✔
129
    }
130

131
    /** @inheritDoc */
132
    public function remove(int $offset, int $length) : void
1✔
133
    {
134
        $temp[] = substr($this->value, 0, $offset);
1✔
135
        $temp[] = substr($this->value, $offset + $length);
1✔
136

137
        $this->value = implode('', $temp);
1✔
138
    }
139

140
    /**
141
     * Convenience function for explode.
142
     *
143
     * @param $delimiter
144
     * @param $limit
145
     *
146
     * @return IndexedArrayInterface
147
     */
148
    public function splitOn($delimiter = '', $limit = PHP_INT_MAX) : IndexedArrayInterface
1✔
149
    {
150
        return $this->explode($delimiter, $limit);
1✔
151
    }
152

153
    public function splitAt(int $position) : IndexedArrayInterface
1✔
154
    {
155
        $split1 = substr($this->value, self::BEGINNING_OF_STRING_POSITION, $position);
1✔
156
        $split2 = substr($this->value, $position);
1✔
157

158
        return new Collection([$split1, $split2]);
1✔
159
    }
160

161
    /**
162
     * @inheritDoc
163
     */
164
    public function explode($delimiter = '', $limit = PHP_INT_MAX) : IndexedArrayInterface
1✔
165
    {
166
        return new Collection(explode($delimiter, $this->getValue(), $limit));
1✔
167
    }
168

169
    /** @inheritDoc */
170
    public function startsWith(mixed $needle) : bool
1✔
171
    {
172
        return str_starts_with($this->value, $needle);
1✔
173
    }
174

175
    /** @inheritDoc */
176
    public function substring(int $offset, ?int $length = null) : StringInterface
1✔
177
    {
178
        return new Twine(substr($this->value, $offset, $length));
1✔
179
    }
180

181
    /**
182
     * Convert string to UPPER CASE.
183
     *
184
     * @return string
185
     */
186
    public function upper() : string
1✔
187
    {
188
        return mb_strtoupper($this->getValue());
1✔
189
    }
190

191
    /**
192
     * @param string|array $needle
193
     * @param string|array $replacement
194
     *
195
     * @return void
196
     */
197
    public function replace(string|array $needle, string|array $replacement) : void
1✔
198
    {
199
        if (is_string($needle)) {
1✔
200
            $needle = "/{$needle}/";
1✔
201
        }
202
        $this->value = preg_replace($needle, $replacement, $this->value);
1✔
203
    }
204

205
    public function isEmpty() : bool
1✔
206
    {
207
        return is_null($this->value) || $this->value === '';
1✔
208
    }
209
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc