Completed
Push — master ( 09a151...fdd724 )
by Zoltán
02:38
created

StrictlyTypedCollectionTrait::doTypeCheck()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
rs 9.2
cc 4
eloc 7
nc 4
nop 1
1
<?php namespace BuildR\Collection\Collection;
2
3
/**
4
 * Trait for collections that can be strictly typed
5
 *
6
 * BuildR PHP Framework
7
 *
8
 * @author Zoltán Borsos <[email protected]>
9
 * @package Collection
10
 * @subpackage Collection
11
 *
12
 * @copyright    Copyright 2015, Zoltán Borsos.
13
 * @license      https://github.com/Zolli/BuildR/blob/master/LICENSE.md
14
 * @link         https://github.com/Zolli/BuildR
15
 *
16
 * @codeCoverageIgnore
17
 */
18
trait StrictlyTypedCollectionTrait {
19
20
    /**
21
     * @type NULL|callable
22
     */
23
    protected $typeChecker;
24
25
    /**
26
     * @type NULL|string
27
     */
28
    protected $typeCheckFailMessage;
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function setStrictType(callable $typeCheck, $message = NULL) {
34
        $this->typeChecker = $typeCheck;
35
        $this->typeCheckFailMessage = $message;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function isStrict() {
42
        return is_callable($this->typeChecker);
43
    }
44
45
    /**
46
     * Executes the type check if the collection is strict. Always
47
     * returns true, when the collection is not strictly typed
48
     *
49
     * @param mixed $value
50
     *
51
     * @return bool
52
     *
53
     * @throws \BuildR\Collection\Exception\CollectionException
54
     */
55
    protected function doTypeCheck($value) {
56
        if($this->isStrict()) {
57
            $result = (bool) $this->typeChecker($value);
0 ignored issues
show
Bug introduced by
It seems like typeChecker() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
58
59
            if($result === FALSE) {
60
                $message = ($this->typeCheckFailMessage === NULL) ? gettype($value) : $this->typeCheckFailMessage;
61
62
                throw CollectionException::typeException($message);
63
            }
64
65
            return TRUE;
66
        }
67
    }
68
69
}
70