IndexCollection::add()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 9
Ratio 100 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 5
Bugs 0 Features 1
Metric Value
c 5
b 0
f 1
dl 9
loc 9
ccs 6
cts 6
cp 1
rs 9.2
cc 4
eloc 5
nc 3
nop 1
crap 4
1
<?php
2
3
namespace Zortje\MySQLKeeper\Database\Table;
4
5
use Zortje\MySQLKeeper\Common\Collection;
6
7
/**
8
 * Class IndexCollection
9
 *
10
 * @package Zortje\MySQLKeeper\Database\Table
11
 */
12 View Code Duplication
class IndexCollection extends Collection {
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
13
14
	/**
15
	 * @var Index[] Table indices
16
	 */
17
	protected $collection = [];
18
19
	/**
20
	 * Add index to collection
21
	 *
22
	 * @param Index $index
23
	 *
24
	 * @throws \InvalidArgumentException if incorrect object type is added
25
	 */
26 2
	public function add($index) {
27 2
		if (!is_object($index) || get_class($index) !== 'Zortje\MySQLKeeper\Database\Table\Index') {
28 1
			$argumentType = is_object($index) ? get_class($index) : gettype($index);
29
30 1
			throw new \InvalidArgumentException(sprintf('Collection may only contain "%s" objects, "%s" is not allowed', 'Zortje\MySQLKeeper\Database\Table\Index', $argumentType));
31
		}
32
33 1
		$this->collection[] = $index;
34 1
	}
35
36
	/**
37
	 * Get index collection with non primary key indices
38
	 *
39
	 * @return IndexCollection
40
	 */
41 1
	public function isNotPrimaryKey() {
42 1
		$indices = new IndexCollection();
43
44 1
		foreach ($this->collection as $index) {
45 1
			if ($index->isPrimaryKey() === false) {
46 1
				$indices->add($index);
47 1
			}
48 1
		}
49
50 1
		return $indices;
51
	}
52
}
53