TCheckKey::checkKey()   B
last analyzed

Complexity

Conditions 9
Paths 9

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 9

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 17
c 1
b 0
f 0
nc 9
nop 1
dl 0
loc 29
ccs 18
cts 18
cp 1
crap 9
rs 8.0555
1
<?php
2
3
namespace kalanis\kw_cache_psr\Traits;
4
5
6
use kalanis\kw_cache_psr\InvalidArgumentException;
7
8
9
/**
10
 * Trait TCheckKey
11
 * @package kalanis\kw_cache_psr\Traits
12
 * Check key for problematic characters
13
 */
14
trait TCheckKey
15
{
16
    /**
17
     * @param string $key
18
     * @return string
19
     * @throws InvalidArgumentException
20
     */
21 48
    protected function checkKey(string $key): string
22
    {
23
        // problematic chars
24
        // {}()/\@:
25 48
        if (false !== strpos($key, '{')) {
26 7
            throw new InvalidArgumentException(sprintf('The key *%s* contains "{"', $key));
27
        }
28 43
        if (false !== strpos($key, '}')) {
29 1
            throw new InvalidArgumentException(sprintf('The key *%s* contains "}"', $key));
30
        }
31 42
        if (false !== strpos($key, '(')) {
32 1
            throw new InvalidArgumentException(sprintf('The key *%s* contains "("', $key));
33
        }
34 41
        if (false !== strpos($key, ')')) {
35 1
            throw new InvalidArgumentException(sprintf('The key *%s* contains ")"', $key));
36
        }
37 40
        if (false !== strpos($key, '/')) {
38 1
            throw new InvalidArgumentException(sprintf('The key *%s* contains "/"', $key));
39
        }
40 39
        if (false !== strpos($key, '\\')) {
41 1
            throw new InvalidArgumentException(sprintf('The key *%s* contains "\\"', $key));
42
        }
43 38
        if (false !== strpos($key, '@')) {
44 1
            throw new InvalidArgumentException(sprintf('The key *%s* contains "@"', $key));
45
        }
46 37
        if (false !== strpos($key, ':')) {
47 1
            throw new InvalidArgumentException(sprintf('The key *%s* contains ":"', $key));
48
        }
49 36
        return $key;
50
    }
51
}
52