Test Setup Failed
Push — master ( b8c4ab...931d35 )
by Matthew
13:28 queued 09:02
created

IncludeCollector::runAndCollect()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
namespace Psalm\Internal;
4
5
use function array_diff;
6
use function array_merge;
7
use function array_unique;
8
use function array_values;
9
use function get_included_files;
10
use function preg_grep;
11
12
use const PREG_GREP_INVERT;
13
14
/**
15
 * Include collector
16
 *
17
 * Used to execute code that may cause file inclusions, and report what files have been included
18
 * NOTE: dependencies of this class should be kept at minimum, as it's used before autoloader is
19
 * registered.
20
 */
21
final class IncludeCollector
22
{
23
    /** @var list<string> */
24
    private $included_files = [];
25
26
    /**
27
     * @template T
28
     * @param callable():T $f
0 ignored issues
show
Documentation introduced by
The doc-type callable():T could not be parsed: Expected "|" or "end of type", but got "(" at position 8. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
29
     * @return T
30
     */
31
    public function runAndCollect(callable $f)
32
    {
33
        $before = get_included_files();
34
        $ret = $f();
35
        $after = get_included_files();
36
37
        $included = array_diff($after, $before);
38
39
        $this->included_files = array_values(array_unique(array_merge($this->included_files, $included)));
40
41
        return $ret;
42
    }
43
44
    /** @return list<string> */
0 ignored issues
show
Documentation introduced by
The doc-type list<string> could not be parsed: Expected "|" or "end of type", but got "<" at position 4. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
45
    public function getIncludedFiles(): array
46
    {
47
        return $this->included_files;
48
    }
49
50
    /** @return list<string> */
0 ignored issues
show
Documentation introduced by
The doc-type list<string> could not be parsed: Expected "|" or "end of type", but got "<" at position 4. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
51
    public function getFilteredIncludedFiles(): array
52
    {
53
        return array_values(preg_grep('@^phar://@', $this->getIncludedFiles(), PREG_GREP_INVERT));
54
    }
55
}
56