ValidNameChecker   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 0
cbo 1
dl 0
loc 35
ccs 13
cts 13
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A ensureValidName() 0 14 4
A asString() 0 6 3
1
<?php
2
3
4
namespace Beanie\Tube;
5
6
7
use Beanie\Exception\InvalidNameException;
8
9
class ValidNameChecker
10
{
11
    const VALID_NAME_REGEX = '/^[A-Za-z0-9+\/;.$_()][A-Za-z0-9+\/;.$_()\-]*$/';
12
13
    /**
14
     * @param string $name
15
     * @return bool
16
     * @throws InvalidNameException
17
     */
18 29
    public function ensureValidName($name)
19
    {
20
        if (!(
21 29
            is_string($name) &&
22 29
            strlen($name) <= 200 &&
23 25
            preg_match(self::VALID_NAME_REGEX, $name)
24 29
        )) {
25 8
            throw new InvalidNameException(sprintf(
26 8
                'Invalid name: \'%s\'', $this->asString($name)
27 8
            ));
28
        }
29
30 21
        return true;
31
    }
32
33
    /**
34
     * @param mixed $value
35
     * @return string
36
     */
37 8
    private function asString($value)
38
    {
39 8
        return (is_object($value) && !method_exists($value, '__toString'))
40 8
            ? sprintf('{object of type %s', get_class($value))
41 8
            : $value;
42
    }
43
}
44