ObjectCollection::validateType()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 3
eloc 5
nc 3
nop 1
1
<?php
2
3
  namespace Fiv\Collection;
4
5
  abstract class ObjectCollection extends TypedCollection {
6
7
    /**
8
     * Used for validation
9
     * Return class name
10
     *
11
     * @codeCoverageIgnore
12
     * @return string
13
     */
14
    public abstract function objectsClassName();
15
16
    /**
17
     * Clone each item
18
     */
19
    public function __clone() {
20
      $items = array();
21
      foreach ($this->items as $item) {
22
        $items[] = clone $item;
23
      }
24
      $this->setItems($items);
25
    }
26
27
28
    public function validateType($item) {
29
      $itemClass = $this->objectsClassName();
30
      if (($item instanceof $itemClass) == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
31
        $className = ($item === null) ? null : get_class($item);
32
        throw new \Exception('Invalid object type. Expect ' . $this->objectsClassName() . ' given ' . $className);
33
      }
34
    }
35
36
  }
37