Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Passed
Push — crud-uploads ( f5e93a...469ece )
by Pedro
11:36
created

SingleBase64Image::uploadRepeatableFile()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 13
nc 8
nop 2
dl 0
loc 24
rs 9.5222
c 0
b 0
f 0
1
<?php
2
3
namespace Backpack\CRUD\app\Library\Uploaders;
4
5
use Backpack\CRUD\app\Library\CrudPanel\CrudPanelFacade as CRUD;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Support\Facades\Storage;
8
use Illuminate\Support\Str;
9
10
class SingleBase64Image extends Uploader
11
{
12
    public function uploadFile(Model $entry, $value = null)
13
    {
14
        $value = $value ?? CRUD::getRequest()->get($this->getName());
0 ignored issues
show
Bug introduced by
The method getRequest() does not exist on Backpack\CRUD\app\Librar...udPanel\CrudPanelFacade. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

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

14
        $value = $value ?? CRUD::/** @scrutinizer ignore-call */ getRequest()->get($this->getName());
Loading history...
15
        $previousImage = $entry->getOriginal($this->getName());
16
17
        if (! $value && $previousImage) {
18
            Storage::disk($this->getDisk())->delete($previousImage);
19
20
            return null;
21
        }
22
23
        if (Str::startsWith($value, 'data:image')) {
24
            if ($previousImage) {
25
                Storage::disk($this->getDisk())->delete($previousImage);
26
            }
27
28
            $base64Image = Str::after($value, ';base64,');
29
            $finalPath = $this->getPath().$this->getFileName($value);
30
31
            Storage::disk($this->getDisk())->put($finalPath, base64_decode($base64Image));
32
33
            return $finalPath;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $finalPath returns the type string which is incompatible with the return type mandated by Backpack\CRUD\app\Librar...Interface::uploadFile() of void.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
34
        }
35
36
        return $previousImage;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $previousImage also could return the type array which is incompatible with the return type mandated by Backpack\CRUD\app\Librar...Interface::uploadFile() of void.
Loading history...
37
    }
38
39
    public function uploadRepeatableFile(Model $entry, $value = null)
40
    {
41
        $previousImages = $this->getPreviousRepeatableValues($entry);
42
43
        foreach ($value as $row => $rowValue) {
44
            if ($rowValue) {
45
                if (Str::startsWith($rowValue, 'data:image')) {
46
                    $base64Image = Str::after($rowValue, ';base64,');
47
                    $finalPath = $this->getPath().$this->getFileName($rowValue);
48
                    Storage::disk($this->getDisk())->put($finalPath, base64_decode($base64Image));
49
                    $value[$row] = $previousImages[] = $finalPath;
50
51
                    continue;
52
                }
53
            }
54
        }
55
56
        $imagesToDelete = array_diff($previousImages, $value);
57
58
        foreach ($imagesToDelete as $image) {
59
            Storage::disk($this->getDisk())->delete($image);
60
        }
61
62
        return $value;
63
    }
64
}
65