Test Failed
Push — master ( b7ab7b...94ee03 )
by Kirill
02:59
created

helpers.php ➔ object_to_string()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 1
dl 0
loc 14
ccs 6
cts 7
cp 0.8571
crap 3.0261
rs 9.7998
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of Railt package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace Railt\SDL;
11
12
use Railt\Io\Exception\NotReadableException;
13
use Railt\Io\File;
14
use Railt\Reflection\Contracts\Document;
15
use Railt\SDL\Exception\CompilerException;
16
17
18
if (! \function_exists('sdl')) {
19
    /**
20
     * @param string $fileOrSources
21
     * @param bool $filename
22
     * @return Document
23
     * @throws CompilerException
24
     * @throws NotReadableException
25
     */
26
    function sdl(string $fileOrSources, bool $filename = false): Document
27
    {
28
        $file = $filename ? File::fromPathname($fileOrSources) : File::fromSources($fileOrSources);
29
30
        return (new Compiler())->compile($file);
0 ignored issues
show
Bug introduced by
It seems like $file defined by $filename ? \Railt\Io\Fi...Sources($fileOrSources) on line 28 can also be of type object<Railt\Io\File>; however, Railt\SDL\Compiler::compile() does only seem to accept object<Railt\Io\Readable>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
31
    }
32
}
33
34
if (! \function_exists('object_to_string')) {
35
    /**
36
     * @param object $object
37
     * @return string
38
     */
39
    function object_to_string($object): string
40
    {
41 1
        \assert(\is_object($object));
42
43 1
        $hash = \function_exists('\\spl_object_id')
44 1
            ? \spl_object_id($object)
45 1
            : \spl_object_hash($object);
46
47 1
        if (is_renderable($object)) {
48
            return \sprintf('%s(%s)#%s', \get_class($object), (string)$object, $hash);
49
        }
50
51 1
        return \sprintf('%s#%s', \get_class($object), $hash);
52
    }
53
}
54
55
56
if (! \function_exists('is_renderable')) {
57
    /**
58
     * @param mixed $value
59
     * @return bool
60
     */
61
    function is_renderable($value): bool
62
    {
63 1
        return \is_scalar($value) || (\is_object($value) && \method_exists($value, '__toString'));
64
    }
65
}
66