Completed
Push — master ( cb8ad2...cd16c8 )
by Cristiano
02:41
created

InsertPart::insert()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 4
eloc 7
nc 5
nop 2
dl 0
loc 14
ccs 8
cts 8
cp 1
crap 4
rs 10
c 1
b 0
f 1
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
trait InsertPart {
13
	abstract public function add(...$elements);
14
15
	/**
16
	 * Insert one element at the given index
17
	 *
18
	 * @param mixed $element
19
	 * @param int|null|string $index
20
	 *
21
	 * @return $this
22
	 */
23 5
	public function insert($element, $index): self {
24 5
		if (null === $index) {
25 2
			return $this->add($element);
26
		}
27
28 5
		if (is_int($index)) {
29 3
			array_splice($this->array, $index, 0, $element);
30
		}
31
32 5
		if (is_string($index)) {
33 2
			$this->array[$index] = $element;
0 ignored issues
show
Bug Best Practice introduced by
The property array does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
34
		}
35
36 5
		return $this;
37
	}
38
}
39