GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — master ( 3480d9...5b2345 )
by Freek
01:45
created

DirectoryParser::onStartParsing()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\Outline;
4
5
use Closure;
6
use Symfony\Component\Finder\Finder;
7
8
class DirectoryParser implements Parser
9
{
10
    /** @var string */
11
    protected $path;
12
13
    /** @var array */
14
    protected $extensions = ['php'];
15
16
    /** @var Closure */
17
    protected $initListener;
18
19
    /** @var Closure */
20
    protected $progressListener;
21
22
    public function __construct(string $path)
23
    {
24
        $this->path = $path;
25
    }
26
27
    public function setExtensions(array $extensions): self
28
    {
29
        $this->extensions = $extensions;
30
31
        return $this;
32
    }
33
34
    public function setExtensionsFromString(string $extensionsString): self
35
    {
36
        $extensions = explode(',', $extensionsString);
37
38
        return $this->setExtensions(
39
            array_map(function (string $extension) {
40
                return str_replace(['*', '.'], '', $extension);
41
            }, $extensions)
42
        );
43
    }
44
45
    public function onStartParsing(Closure $initListener): self
46
    {
47
        $this->initListener = $initListener;
48
49
        return $this;
50
    }
51
52
    public function onFileParsed(Closure $progressListener): self
53
    {
54
        $this->progressListener = $progressListener;
55
56
        return $this;
57
    }
58
59
    public function getParsed(): array
60
    {
61
        $parsed = [];
62
63
        $files = Finder::create()->files()->in($this->path)->name($this->getExtensionsSearchQuery());
64
65
        if ($this->initListener) {
66
            call_user_func_array($this->initListener, [$files->count()]);
67
        }
68
69
        /** @var \Symfony\Component\Finder\SplFileInfo $file */
70
        foreach ($files as $file) {
71
            $fileParser = new FileParser($file->getRealPath());
72
73
            $parsed[] = $fileParser->getParsed();
74
75
            if (!$this->progressListener) {
76
                continue;
77
            }
78
79
            call_user_func($this->progressListener);
80
        }
81
82
        return $this->flatten($parsed);
83
    }
84
85
    protected function flatten(array $pages): array
86
    {
87
        $flattened = [];
88
89
        foreach ($pages as $page) {
90
            foreach ($page as $lineIndex => $line) {
91
                if (!$line) {
92
                    $flattened[$lineIndex] = $flattened[$lineIndex] ?? null;
93
94
                    continue;
95
                }
96
97
                foreach ($line as $cursorIndex => $characterValue) {
98
                    $flattened[$lineIndex][$cursorIndex] = ($flattened[$lineIndex][$cursorIndex] ?? 0) + $characterValue;
99
                }
100
            }
101
        }
102
103
        return $flattened;
104
    }
105
106
    protected function getExtensionsSearchQuery(): string
107
    {
108
        $query = implode('|', array_map(function ($extension) {
109
            return "\.{$extension}\$";
110
        }, $this->extensions));
111
112
        return "/{$query}/";
113
    }
114
}
115