StringConverter::convertToString()   A
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 13.776

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 4
cts 10
cp 0.4
rs 9.1111
c 0
b 0
f 0
cc 6
nc 5
nop 1
crap 13.776
1
<?php
2
3
namespace Metabor;
4
5
use MetaborStd\NamedInterface;
6
use MetaborStd\StringConverterInterface;
7
8
/**
9
 * @author Oliver Tischlinger
10
 */
11
class StringConverter implements StringConverterInterface
12
{
13
    /**
14
     * @param mixed $source
15
     *
16
     * @return string
17
     */
18 1
    public function convertToString($source)
19
    {
20 1
        if (is_object($source)) {
21 1
            if ($source instanceof NamedInterface) {
22 1
                return $source->getName();
23
            } elseif (method_exists($source, '__toString')) {
24
                return (string) $source;
25
            } else {
26
                return get_class($source);
27
            }
28
        } elseif (is_scalar($source) || is_null($source)) {
29
            return (string) $source;
30
        } else {
31
            return gettype($source);
32
        }
33
    }
34
}
35