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 ( c657db...bf972d )
by Pedro
13:12
created

UploadersRepository::addUploaderClasses()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Backpack\CRUD\app\Library\CrudPanel\Uploads;
4
use Backpack\CRUD\app\Library\CrudPanel\Uploads\Interfaces\UploaderInterface;
5
6
final class UploadersRepository
7
{
8
    /**
9
     * The array of uploaders classes for field types.
10
     *
11
     * @var array
12
     */
13
    private array $uploaderClasses;
14
15
    /**
16
     * The array of uploaders that have been handled, aka events registered.
17
     *
18
     * @var array
19
     */
20
    private array $handledUploaders = [];
21
22
    /**
23
     * The array of uploaders that have been registered, aka would be handled if needed by the event register.
24
     *
25
     * @var array
26
     */
27
    private array $registeredUploaders = [];
28
29
    public function __construct()
30
    {
31
        $this->uploaderClasses = config('backpack.crud.uploaders');
32
    }
33
34
    /**
35
     * Mark the given uploader as handled.
36
     *
37
     * @param string $objectName  - the field/column name 
38
     * @return void
39
     */
40
    public function markAsHandled(string $objectName)
41
    {
42
        if (! in_array($objectName, $this->handledUploaders)) {
43
            $this->handledUploaders[] = $objectName;
44
        }
45
    }
46
47
    /**
48
     * Check if the given uploader for field/column have been handled.
49
     *
50
     * @param string $objectName
51
     * @return boolean
52
     */
53
    public function isUploadHandled(string $objectName)
54
    {
55
        return in_array($objectName, $this->handledUploaders);
56
    }
57
58
    /**
59
     * Check if there are uploads for the give object(field/column) type.
60
     *
61
     * @param  string  $objectType
62
     * @param  string  $group
63
     * @return boolean
64
     */
65
    public function hasUploadFor(string $objectType, string $group)
66
    {
67
        return array_key_exists($objectType, $this->uploaderClasses[$group]);
68
    }
69
70
    /**
71
     * Return the uploader for the given object type.
72
     *
73
     * @param string $objectType
74
     * @param string $group
75
     * @return void
76
     */
77
    public function getUploadFor(string $objectType, string $group)
78
    {
79
        return $this->uploaderClasses[$group][$objectType];
80
    }
81
82
    /**
83
     * Register new uploaders or override existing ones.
84
     *
85
     * @param array $uploaders
86
     * @param string $group
87
     * @return void
88
     */
89
    public function addUploaderClasses(array $uploaders, string $group)
90
    {
91
        $this->uploaderClasses[$group] = array_merge($this->getGroupUploadersClasses($group), $uploaders);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->getGroupUploadersClasses($group) targeting Backpack\CRUD\app\Librar...GroupUploadersClasses() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Bug introduced by
$this->getGroupUploadersClasses($group) of type void is incompatible with the type array expected by parameter $arrays of array_merge(). ( Ignorable by Annotation )

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

91
        $this->uploaderClasses[$group] = array_merge(/** @scrutinizer ignore-type */ $this->getGroupUploadersClasses($group), $uploaders);
Loading history...
92
    }
93
94
    /**
95
     * Return the uploaders classes for the given group.
96
     *
97
     * @param string $group
98
     * @return void
99
     */
100
    private function getGroupUploadersClasses(string $group)
101
    {
102
        return $this->uploaderClasses[$group] ?? [];
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->uploaderClasses[$group] ?? array() also could return the type array which is incompatible with the documented return type void.
Loading history...
103
    }
104
105
    /**
106
     * Register the specified uploader for the given upload name.
107
     *
108
     * @param string $uploadName
109
     * @param UploaderInterface $uploader
110
     * @return void
111
     */
112
    public function registerUploader(string $uploadName, UploaderInterface $uploader)
113
    {
114
        if (! array_key_exists($uploadName, $this->registeredUploaders) || ! in_array($uploader, $this->registeredUploaders[$uploadName])) {
115
            $this->registeredUploaders[$uploadName][] = $uploader;
116
        }
117
    }
118
119
    /**
120
     * Check if there are uploaders registered for the given upload name.
121
     *
122
     * @param string $uploadName
123
     * @return boolean
124
     */
125
    public function hasUploadersRegisteredFor(string $uploadName)
126
    {
127
        return array_key_exists($uploadName, $this->registeredUploaders);
128
    }
129
130
    /**
131
     * Get the registered uploaders for the given upload name.
132
     *
133
     * @param string $uploadName
134
     * @return array
135
     */
136
    public function getRegisteredUploadersFor(string $uploadName)
137
    {
138
        return $this->registeredUploaders[$uploadName] ?? [];
139
    }
140
141
    /**
142
     * Check if the specified upload is registered for the given upload name.
143
     *
144
     * @param string $uploadName
145
     * @param UploaderInterface $upload
146
     * @return boolean
147
     */
148
    public function isUploadRegistered(string $uploadName, UploaderInterface $upload)
149
    {
150
        return $this->hasUploadersRegisteredFor($uploadName) && in_array($upload->getName(), $this->getRegisteredUploadNames($uploadName));
151
    }
152
153
    /**
154
     * Return the registered uploaders names for the given upload name.
155
     *
156
     * @param string $uploadName
157
     * @return array
158
     */
159
    public function getRegisteredUploadNames(string $uploadName)
160
    {
161
        return array_map(function ($uploader) {
162
            return $uploader->getName();
163
        }, $this->getRegisteredUploadersFor($uploadName));
164
    }
165
}
166