PWA   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 43
ccs 0
cts 22
cp 0
rs 10
c 0
b 0
f 0
wmc 5
lcom 0
cbo 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A routes() 0 4 1
A assets() 0 28 4
1
<?php
2
3
namespace CodexShaper\PWA;
4
5
use Illuminate\Support\Facades\File;
6
use Illuminate\Support\Facades\Response;
7
8
class PWA
9
{
10
    public function routes()
11
    {
12
        require __DIR__.'/../routes/pwa.php';
13
    }
14
15
    /**
16
     * Load assests.
17
     *
18
     * @param string $path
19
     *
20
     * @return \Illuminate\Http\Response
21
     */
22
    public function assets($path)
23
    {
24
        $file = base_path(trim(config('pwa.resources_path'), '/').'/'.urldecode($path));
25
26
        if (File::exists($file)) {
27
            switch ($extension = pathinfo($file, PATHINFO_EXTENSION)) {
0 ignored issues
show
Unused Code introduced by
$extension is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
28
                case 'js':
29
                    $mimeType = 'text/javascript';
30
                    break;
31
                case 'css':
32
                    $mimeType = 'text/css';
33
                    break;
34
                default:
35
                    $mimeType = File::mimeType($file);
36
                    break;
37
            }
38
39
            $response = Response::make(File::get($file), 200);
40
            $response->header('Content-Type', $mimeType);
0 ignored issues
show
Security Bug introduced by
It seems like $mimeType defined by \Illuminate\Support\Facades\File::mimeType($file) on line 35 can also be of type false; however, Illuminate\Http\ResponseTrait::header() does only seem to accept array|string, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
41
            $response->setSharedMaxAge(31536000);
42
            $response->setMaxAge(31536000);
43
            $response->setExpires(new \DateTime('+1 year'));
44
45
            return $response;
46
        }
47
48
        return response('', 404);
0 ignored issues
show
Bug Compatibility introduced by
The expression response('', 404); of type Illuminate\Http\Response...Routing\ResponseFactory adds the type Illuminate\Contracts\Routing\ResponseFactory to the return on line 48 which is incompatible with the return type documented by CodexShaper\PWA\PWA::assets of type Illuminate\Http\Response.
Loading history...
49
    }
50
}
51