TypeSafetyTrait::ensure()   C
last analyzed

Complexity

Conditions 15
Paths 15

Size

Total Lines 58
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 37
CRAP Score 15

Importance

Changes 5
Bugs 1 Features 0
Metric Value
c 5
b 1
f 0
dl 0
loc 58
ccs 37
cts 37
cp 1
rs 6.4661
cc 15
eloc 34
nc 15
nop 2
crap 15

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * This file is part of the kaloa/util package.
5
 *
6
 * For full copyright and license information, please view the LICENSE file
7
 * that was distributed with this source code.
8
 */
9
10
namespace Kaloa\Util\TypeSafety;
11
12
use InvalidArgumentException;
13
14
trait TypeSafetyTrait
15
{
16
    /**
17
     * @param string $types
18
     * @param array $args
19
     */
20 12
    private function ensure($types, array $args)
21
    {
22 12
        if (!is_string($types)) {
23 1
            throw new InvalidArgumentException('Type list must be of type string');
24
        }
25
26 11
        $cnt = count($args);
27
28 11
        if (strlen($types) !== $cnt) {
29 1
            throw new InvalidArgumentException('Type list length does not match argument count');
30
        }
31
32 10
        $i = 0;
33
34 10
        foreach ($args as $arg) {
35 10
            switch ($types[$i]) {
36 10
                case 'b':
37 4
                    if (!is_bool($arg)) {
38 1
                        throw new InvalidArgumentException('bool expected');
39
                    }
40 3
                    break;
41
42 9
                case 'f':
43 4
                    if (!is_float($arg)) {
44 1
                        throw new InvalidArgumentException('float expected');
45
                    }
46 3
                    break;
47
48 8
                case 'i':
49 4
                    if (!is_int($arg)) {
50 1
                        throw new InvalidArgumentException('int expected');
51
                    }
52 3
                    break;
53
54 7
                case 'r':
55 2
                    if (!is_resource($arg)) {
56 1
                        throw new InvalidArgumentException('resource expected');
57
                    }
58 1
                    break;
59
60 5
                case 's':
61 4
                    if (!is_string($arg)) {
62 1
                        throw new InvalidArgumentException('string expected');
63
                    }
64 3
                    break;
65
66 4
                case '-':
67
                    /* skip */
68 3
                    break;
69
70 1
                default:
71 1
                    throw new InvalidArgumentException('Unknown type at offset ' . $i . '. One of [bfirs-] expected');
72
                    // no break
73 5
            }
74
75 4
            $i++;
76 4
        }
77 4
    }
78
}
79