TypeTrait   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 16
lcom 0
cbo 0
dl 0
loc 56
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C checkType() 0 46 16
1
<?php
2
/**
3
 * This file is part of the ramsey/collection library
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @copyright Copyright (c) Ben Ramsey <[email protected]>
9
 * @license http://opensource.org/licenses/MIT MIT
10
 * @link https://benramsey.com/projects/ramsey-collection/ Documentation
11
 * @link https://packagist.org/packages/ramsey/collection Packagist
12
 * @link https://github.com/ramsey/collection GitHub
13
 */
14
15
namespace Ramsey\Collection\Tool;
16
17
/**
18
 * Provides functionality to check values for specific types
19
 */
20
trait TypeTrait
21
{
22
    /**
23
     * Returns `true` if value is of the specified type
24
     *
25
     * @param string $type
26
     * @param mixed $value
27
     * @return bool
28
     */
29
    protected function checkType($type, $value)
30
    {
31
        switch ($type) {
32
            case 'array':
33
                return is_array($value);
34
35
            case 'bool':
36
            case 'boolean':
37
                return is_bool($value);
38
39
            case 'callable':
40
                return is_callable($value);
41
42
            case 'float':
43
            case 'double':
44
                return is_float($value);
45
46
            case 'int':
47
            case 'integer':
48
                return is_int($value);
49
50
            case 'null':
51
                return is_null($value);
52
53
            case 'numeric':
54
                return is_numeric($value);
55
56
            case 'object':
57
                return is_object($value);
58
59
            case 'resource':
60
                return is_resource($value);
61
62
            case 'scalar':
63
                return is_scalar($value);
64
65
            case 'string':
66
                return is_string($value);
67
68
            case 'mixed':
69
                return true;
70
71
            default:
72
                return ($value instanceof $type);
73
        }
74
    }
75
}
76