ArrayConversionsPart   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Test Coverage

Coverage 88.24%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 80
ccs 15
cts 17
cp 0.8824
rs 10
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A split() 0 6 2
A chunk() 0 6 2
A join() 0 11 4
1
<?php declare(strict_types=1);
2
/**
3
 * This file is part of the Phootwork package.
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 *
7
 * @license MIT License
8
 * @copyright Thomas Gossmann
9
 */
10
namespace phootwork\lang\parts;
11
12
use InvalidArgumentException;
13
use phootwork\lang\ArrayObject;
14
use phootwork\lang\Text;
15
16
/**
17
 * Text methods for array/ArrayObject conversions
18
 *
19
 * @author Thomas Gossmann
20
 * @author Cristiano Cinotti
21
 */
22
trait ArrayConversionsPart {
23
	abstract protected function getString(): string;
24
25
	/**
26
	 * Splits the string by string
27
	 *
28
	 * @param string $delimiter The boundary string.
29
	 * @param int $limit
30
	 * 		If limit is set and positive, the returned array will contain a maximum of
31
	 * 		limit elements with the last element containing the rest of string.
32
	 *
33
	 * 		If the limit parameter is negative, all components except the last
34
	 * 		-limit are returned.
35
	 *
36
	 * 		If the limit parameter is zero, then this is treated as 1.
37
	 *
38
	 * @throws InvalidArgumentException If the delimiter is an empty string.
39
	 *
40
	 * @return ArrayObject
41
	 * 		Returns an array of strings created by splitting the string parameter on boundaries
42
	 * 		formed by the delimiter.
43
	 *
44
	 *        If delimiter contains a value that is not contained in string and a negative limit is used,
45
	 *        then an empty array will be returned, otherwise an array containing string will be returned.
46
	 *
47
	 */
48 22
	public function split(string $delimiter, int $limit = PHP_INT_MAX): ArrayObject {
49 22
		if ('' === $delimiter) {
50 1
			throw new InvalidArgumentException("The delimiter can't be an empty string");
51
		}
52
53 21
		return new ArrayObject(explode($delimiter, $this->getString(), $limit));
54
	}
55
56
	/**
57
	 * Join array elements with a string
58
	 *
59
	 * @param array       $pieces   The array of strings to join.
60
	 * @param string      $glue     Defaults to an empty string.
61
	 * @param string|null $encoding the desired encoding
62
	 *
63
	 * @return Text
64
	 *        Returns a string containing a string representation of all the array elements in the
65
	 *        same order, with the glue string between each element.
66
	 *
67
	 * @psalm-suppress MixedArgumentTypeCoercion
68
	 */
69 1
	public static function join(array $pieces, string $glue = '', ?string $encoding = null): Text {
70 1
		array_map(
71 1
			function (mixed $element): void {
72 1
				if (!($element === null || is_scalar($element) || $element instanceof \Stringable)) {
73
					throw new \TypeError('Can join elements only if scalar, null or \\Stringable');
74
				}
75 1
			},
76 1
			$pieces
77 1
		);
78
79 1
		return new Text(implode($glue, $pieces), $encoding);
80
	}
81
82
	/**
83
	 * Convert the string to an array
84
	 *
85
	 * @param int $splitLength Maximum length of the chunk.
86
	 *
87
	 * @throws InvalidArgumentException If splitLength is less than 1.
88
	 *
89
	 * @return ArrayObject
90
	 * 		If the optional splitLength parameter is specified, the returned array will be
91
	 * 		broken down into chunks with each being splitLength in length, otherwise each chunk
92
	 * 		will be one character in length.
93
	 *      If the split_length length exceeds the length of string, the entire string is returned
94
	 *      as the first (and only) array element.
95
	 */
96 3
	public function chunk(int $splitLength = 1): ArrayObject {
97 3
		if (false === $array = str_split($this->getString(), $splitLength)) {
98
			throw new InvalidArgumentException('The chunk length has to be positive');
99
		}
100
101 1
		return new ArrayObject($array);
0 ignored issues
show
Bug introduced by
It seems like $array can also be of type true; however, parameter $contents of phootwork\lang\ArrayObject::__construct() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

101
		return new ArrayObject(/** @scrutinizer ignore-type */ $array);
Loading history...
102
	}
103
}
104