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 (#1)
by Cees-Jan
02:43
created

WebrootPreloadMiddleware   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Test Coverage

Coverage 97.56%

Importance

Changes 0
Metric Value
dl 0
loc 71
ccs 40
cts 41
cp 0.9756
rs 10
c 0
b 0
f 0
wmc 9

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 13 3
B __construct() 0 48 6
1
<?php declare(strict_types=1);
2
3
namespace WyriHaximus\React\Http\Middleware;
4
5
use Narrowspark\Mimetypes\MimeTypeByExtensionGuesser;
6
use Psr\Http\Message\ServerRequestInterface;
7
use Psr\Log\LoggerInterface;
8
use RingCentral\Psr7\Response;
9
use ScriptFUSION\Byte\ByteFormatter;
10
use function RingCentral\Psr7\stream_for;
0 ignored issues
show
introduced by
The function RingCentral\Psr7\stream_for was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
11
12
final class WebrootPreloadMiddleware
13
{
14
    /**
15
     * @var array
16
     */
17
    private $files = [];
18
19 10
    public function __construct(string $webroot, LoggerInterface $logger = null)
20
    {
21 10
        $totalSize = 0;
22 10
        $byteFormatter = (new ByteFormatter())->setPrecision(2)->setFormat('%v%u');
23 10
        $directory = new \RecursiveDirectoryIterator($webroot);
0 ignored issues
show
Bug introduced by
The call to RecursiveDirectoryIterator::__construct() has too few arguments starting with flags. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

23
        $directory = /** @scrutinizer ignore-call */ new \RecursiveDirectoryIterator($webroot);

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
24 10
        $directory = new \RecursiveIteratorIterator($directory);
25 10
        $directory = iterator_to_array($directory);
26 10
        usort($directory, function ($a, $b) {
27 10
            return $a->getPathname() <=> $b->getPathname();
28 10
        });
29 10
        foreach ($directory as $fileinfo) {
30 10
            if (!$fileinfo->isFile()) {
31 10
                continue;
32
            }
33
34 10
            $filePath = str_replace(
35
                [
36 10
                    $webroot,
37 10
                    DIRECTORY_SEPARATOR,
38 10
                    '//',
39
                ],
40
                [
41 10
                    DIRECTORY_SEPARATOR,
42 10
                    '/',
43 10
                    '/',
44
                ],
45 10
                $fileinfo->getPathname()
46
            );
47
48 10
            $this->files[$filePath] = [
49 10
                'contents' => file_get_contents($fileinfo->getPathname()),
50
            ];
51
52 10
            $mime = MimeTypeByExtensionGuesser::guess($fileinfo->getExtension());
53 10
            list($mime) = explode(';', $mime);
54 10
            if (strpos($mime, '/') !== false) {
55 10
                $this->files[$filePath]['mime'] = $mime;
56
            }
57
58 10
            if ($logger instanceof LoggerInterface) {
59 1
                $fileSize = strlen($this->files[$filePath]['contents']);
60 1
                $totalSize += $fileSize;
61 10
                $logger->debug($filePath . ': ' . $byteFormatter->format($fileSize));
62
            }
63
        }
64
65 10
        if ($logger instanceof LoggerInterface) {
66 1
            $logger->info('Preloaded ' . count($this->files) . ' file(s) with a combined size of ' . $byteFormatter->format($totalSize) . ' from "' . $webroot . '" into memory');
67
        }
68 10
    }
69
70 9
    public function __invoke(ServerRequestInterface $request, callable $next)
71
    {
72 9
        $path = $request->getUri()->getPath();
73 9
        if (!isset($this->files[$path])) {
74 1
            return $next($request);
75
        }
76
77 8
        $response = (new Response(200))->withBody(stream_for($this->files[$path]['contents']));
78 8
        if (!isset($this->files[$path]['mime'])) {
79
            return $response;
80
        }
81
82 8
        return $response->withHeader('Content-Type', $this->files[$path]['mime']);
83
    }
84
}
85