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.
Completed
Pull Request — master (#24)
by
unknown
06:39
created

Exporter::include()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 2
nc 1
nop 1
1
<?php
2
3
namespace Spatie\Export;
4
5
use Spatie\Crawler\Crawler;
6
use RecursiveIteratorIterator;
7
use RecursiveDirectoryIterator;
8
use Spatie\Crawler\CrawlInternalUrls;
9
use Spatie\Export\Concerns\Messenger;
10
use Illuminate\Contracts\Filesystem\Filesystem;
11
12
class Exporter
13
{
14
    use Messenger;
15
16
    /** @var \Illuminate\Contracts\Filesystem\Filesystem */
17
    protected $filesystem;
18
19
    /** @var string[] */
20
    protected $entries;
21
22
    /** @var string[] */
23
    protected $include;
24
25
    /** @var string[] */
26
    protected $exclude;
27
28
    /** @var \Spatie\Crawler\Crawler */
29
    protected $crawler;
30
31
    public function __construct(Filesystem $filesystem)
32
    {
33
        $this->filesystem = $filesystem;
34
        $this->crawler = (new Crawler(new InternalClient()));
35
    }
36
37
    /**
38
     * @return Filesystem
39
     */
40
    public function getFilesystem(): Filesystem
41
    {
42
        return $this->filesystem;
43
    }
44
45
    public function entries(array $entries): Exporter
46
    {
47
        $this->entries = array_map(function (string $entry) {
48
            return "http://localhost{$entry}";
49
        }, $entries);
50
51
        return $this;
52
    }
53
54
    public function include(array $include): Exporter
55
    {
56
        $this->include = array_map(function ($include) {
57
            return is_array($include)
58
                ? $include
59
                : ['source' => $include, 'target' => $include];
60
        }, $include);
61
62
        return $this;
63
    }
64
65
    public function exclude(array $exclude): Exporter
66
    {
67
        $this->exclude = $exclude;
68
69
        return $this;
70
    }
71
72
    public function export(): void
73
    {
74
        $this->exportEntries();
75
76
        $this->exportIncludedFiles();
77
    }
78
79
    protected function exportEntries(): void
80
    {
81
        foreach ($this->entries as $entry) {
82
            $this->message("[{$entry}]");
83
84
            $crawlObserver = new ExportCrawlObserver($this->filesystem, $entry);
85
            $crawlObserver->onMessage($this->onMessage);
0 ignored issues
show
Bug introduced by
It seems like $this->onMessage can also be of type null; however, Spatie\Export\Concerns\Messenger::onMessage() does only seem to accept callable, 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...
86
87
            $this->crawler
88
                ->setCrawlObserver($crawlObserver)
89
                ->setCrawlProfile(new CrawlInternalUrls($entry))
90
                ->startCrawling($entry);
91
        }
92
    }
93
94
    protected function exportIncludedFiles(): void
95
    {
96
        foreach ($this->include as ['source' => $source, 'target' => $target]) {
97
            $this->message("[{$source}]");
0 ignored issues
show
Bug introduced by
The variable $source does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
98
99
            if (is_file($source)) {
100
                $this->exportIncludedFile($source, $target);
0 ignored issues
show
Bug introduced by
The variable $target does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
101
            } else {
102
                $this->exportIncludedDirectory($source, $target);
103
            }
104
        }
105
    }
106
107
    protected function exportIncludedFile(string $source, string $target): void
108
    {
109
        if ($this->excludes($source)) {
110
            return;
111
        }
112
113
        $target = '/'.ltrim($target, '/');
114
115
        $this->message($target);
116
117
        $this->filesystem->put($target, file_get_contents($source));
118
    }
119
120
    protected function exportIncludedDirectory(string $source, string $target): void
121
    {
122
        $iterator = new RecursiveIteratorIterator(
123
            new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS),
124
            RecursiveIteratorIterator::SELF_FIRST
125
        );
126
127
        foreach ($iterator as $item) {
128
            if ($item->isDir()) {
129
                continue;
130
            }
131
132
            $this->exportIncludedFile($item->getPathname(), $target.'/'.$iterator->getSubPathName());
133
        }
134
    }
135
136
    protected function excludes(string $source): bool
137
    {
138
        foreach ($this->exclude as $pattern) {
139
            if (preg_match($pattern, $source)) {
140
                return true;
141
            }
142
        }
143
144
        return false;
145
    }
146
}
147