Completed
Push — master ( 332a4c...198c54 )
by Kirill
02:13
created

helpers.php ➔ trait_uses_recursive()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 10
ccs 0
cts 0
cp 0
crap 6
rs 9.9332
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
if (! \function_exists('\\sdl')) {
11
    /**
12
     * @param string $fileOrSources
13
     * @param bool $filename
14
     * @return \Railt\Reflection\Contracts\Document
15
     * @throws \Railt\Io\Exception\ExternalFileException
16
     * @throws \Railt\Io\Exception\NotReadableException
17
     * @throws \Railt\Reflection\Exception\TypeConflictException
18
     */
19
    function sdl(string $fileOrSources, bool $filename = false): \Railt\Reflection\Contracts\Document
20
    {
21
        $file = $filename ? \Railt\Io\File::fromPathname($fileOrSources) : \Railt\Io\File::fromSources($fileOrSources);
22
23
        return (new \Railt\SDL\Compiler())->compile($file);
0 ignored issues
show
Bug introduced by
It seems like $file defined by $filename ? \Railt\Io\Fi...Sources($fileOrSources) on line 21 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...
24
    }
25
}
26
27
28
if (! function_exists('trait_uses_recursive')) {
29
    /**
30
     * Returns all traits used by a trait and its traits.
31
     *
32
     * @param string $trait
33
     * @return array
34
     */
35
    function trait_uses_recursive($trait): array
36
    {
37
        $traits = \class_uses($trait);
38
39
        foreach ($traits as $trait) {
40
            $traits += \trait_uses_recursive($trait);
41
        }
42
43
        return $traits;
44
    }
45
}
46
47
48
if (! function_exists('class_uses_recursive')) {
49
    /**
50
     * Returns all traits used by a class, its parent classes and trait of their traits.
51
     *
52
     * @param object|string $class
53
     * @return array
54
     */
55
    function class_uses_recursive($class): array
56
    {
57
        if (\is_object($class)) {
58
            $class = \get_class($class);
59
        }
60
61
        $results = [];
62
63
        foreach (\array_reverse(\class_parents($class)) + [$class => $class] as $class) {
64
            $results += \trait_uses_recursive($class);
65
        }
66
67
        return \array_unique($results);
68
    }
69
}
70
71
72
if (! function_exists('class_basename')) {
73
    /**
74
     * Get the class "basename" of the given object / class.
75
     *
76
     * @param string|object $class
77
     * @return string
78
     */
79
    function class_basename($class): string
80
    {
81
        $class = is_object($class) ? get_class($class) : $class;
82
83
        return basename(\str_replace('\\', '/', $class));
84
    }
85
}
86