Test Failed
Push — master ( e081e8...a6938b )
by Kirill
02:31
created

helpers.php ➔ iterator_reverse_map()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 0
loc 8
ccs 0
cts 3
cp 0
crap 6
rs 10
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('\\iterator_to_generator')) {
29
    /**
30
     * @param iterable $iterator
31
     * @return Generator
32
     */
33
    function iterator_to_generator(iterable $iterator): \Generator
34
    {
35
        yield from $iterator;
36
    }
37
}
38
39
40
if (! \function_exists('\\iterator_map')) {
41
    /**
42
     * @param iterable $iterator
43
     * @param Closure $map
44
     * @return Generator
45
     */
46
    function iterator_map(iterable $iterator, \Closure $map): \Generator
47
    {
48
        $generator = \iterator_to_generator($iterator);
49
50
        while ($generator->valid()) {
51
            $generator->send(yield $generator->key() => $map($generator->current()));
52
        }
53
    }
54
}
55
56
57
if (! \function_exists('\\iterator_reverse_map')) {
58
    /**
59
     * @param iterable $iterator
60
     * @param Closure $map
61
     * @return Generator
62
     */
63
    function iterator_reverse_map(iterable $iterator, \Closure $map): \Generator
64
    {
65
        $generator = \iterator_to_generator($iterator);
66
67
        while ($generator->valid()) {
68
            $generator->send($map(yield $generator->key() => $generator->current()));
69
        }
70
    }
71
}
72
73
74
if (! \function_exists('\\iterator_each')) {
75
    /**
76
     * @param iterable $iterator
77
     * @param Closure $each
78
     * @return Generator
79
     */
80
    function iterator_each(iterable $iterator, \Closure $each): \Generator
81
    {
82
        $generator = \iterator_to_generator($iterator);
83
84
        while ($generator->valid()) {
85
            $each($value = $generator->current());
86
87
            $generator->send(yield $generator->key() => $value);
88
        }
89
    }
90
}
91
92
93
if (! \function_exists('\\iterator_reverse_each')) {
94
    /**
95
     * @param iterable $iterator
96
     * @param Closure $each
97
     * @return Generator
98
     */
99
    function iterator_reverse_each(iterable $iterator, \Closure $each): \Generator
100
    {
101
        $generator = \iterator_to_generator($iterator);
102
103
        while ($generator->valid()) {
104
            $each($result = yield $generator->key() => $generator->current());
105
106
            $generator->send($result);
107
        }
108
    }
109
}
110
111
112 View Code Duplication
if (! \function_exists('\\iterator_filter')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
113
    /**
114
     * @param iterable $iterator
115
     * @param Closure $filter
116
     * @return Generator
117
     */
118
    function iterator_reverse_filter(iterable $iterator, \Closure $filter): \Generator
119
    {
120
        $generator = \iterator_to_generator($iterator);
121
122
        while ($generator->valid()) {
123
            $next = $filter($result = $generator->current());
124
125
            $generator->send($next ? (yield $generator->key() => $result) : null);
126
        }
127
    }
128
}
129
130
131 View Code Duplication
if (! \function_exists('\\iterator_reverse_filter')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
132
    /**
133
     * @param iterable $iterator
134
     * @param Closure $filter
135
     * @return Generator
136
     */
137
    function iterator_reverse_filter(iterable $iterator, \Closure $filter): \Generator
0 ignored issues
show
Best Practice introduced by
The function iterator_reverse_filter() has been defined more than once; this definition is ignored, only the first definition in this file (L118-127) is considered.

This check looks for functions that have already been defined in the same file.

Some Codebases, like WordPress, make a practice of defining functions multiple times. This may lead to problems with the detection of function parameters and types. If you really need to do this, you can mark the duplicate definition with the @ignore annotation.

/**
 * @ignore
 */
function getUser() {

}

function getUser($id, $realm) {

}

See also the PhpDoc documentation for @ignore.

Loading history...
138
    {
139
        $generator = \iterator_to_generator($iterator);
140
141
        while ($generator->valid()) {
142
            $next = $filter($result = yield $generator->key() => $generator->current());
143
144
            $generator->send($next ? $result : null);
145
        }
146
    }
147
}
148