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

UploadersRepository::getRepeatableUploadersFor()   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 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Backpack\CRUD\app\Library\Uploaders\Support;
4
5
use Backpack\CRUD\app\Library\Uploaders\Support\Interfaces\UploaderInterface;
6
7
final class UploadersRepository
8
{
9
    /**
10
     * The array of uploaders classes for field types.
11
     *
12
     * @var array
13
     */
14
    private array $uploaderClasses;
15
16
    /**
17
     * Uploaders registered in a repeatable group.
18
     *
19
     * @var array
20
     */
21
    private array $repeatableUploaders = [];
22
23
    /**
24
     * Uploaders that have already been handled (events registered) for each field/column instance.
25
     * 
26
     * @var array
27
     */
28
    private array $handledUploaders = [];
29
30
    public function __construct()
31
    {
32
        $this->uploaderClasses = config('backpack.crud.uploaders');
33
    }
34
35
    /**
36
     * Mark the given uploader as handled.
37
     *
38
     * @param  string  $objectName  - the field/column name
39
     * @return void
40
     */
41
    public function markAsHandled(string $objectName)
42
    {
43
        if (! in_array($objectName, $this->handledUploaders)) {
44
            $this->handledUploaders[] = $objectName;
45
        }
46
    }
47
48
    /**
49
     * Check if the given uploader for field/column have been handled.
50
     *
51
     * @param  string  $objectName
52
     * @return bool
53
     */
54
    public function isUploadHandled(string $objectName)
55
    {
56
        return in_array($objectName, $this->handledUploaders);
57
    }
58
59
    /**
60
     * Check if there are uploads for the give object(field/column) type.
61
     *
62
     * @param  string  $objectType
63
     * @param  string  $group
64
     * @return bool
65
     */
66
    public function hasUploadFor(string $objectType, string $group)
67
    {
68
        return array_key_exists($objectType, $this->uploaderClasses[$group]);
69
    }
70
71
    /**
72
     * Return the uploader for the given object type.
73
     *
74
     * @param  string  $objectType
75
     * @param  string  $group
76
     * @return void
77
     */
78
    public function getUploadFor(string $objectType, string $group)
79
    {
80
        return $this->uploaderClasses[$group][$objectType];
81
    }
82
83
    /**
84
     * Register new uploaders or override existing ones.
85
     *
86
     * @param  array  $uploaders
87
     * @param  string  $group
88
     * @return void
89
     */
90
    public function addUploaderClasses(array $uploaders, string $group)
91
    {
92
        $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

92
        $this->uploaderClasses[$group] = array_merge(/** @scrutinizer ignore-type */ $this->getGroupUploadersClasses($group), $uploaders);
Loading history...
93
    }
94
95
    /**
96
     * Return the uploaders classes for the given group.
97
     *
98
     * @param  string  $group
99
     * @return void
100
     */
101
    private function getGroupUploadersClasses(string $group)
102
    {
103
        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...
104
    }
105
106
    /**
107
     * Register the specified uploader for the given upload name.
108
     *
109
     * @param  string  $uploadName
110
     * @param  UploaderInterface  $uploader
111
     * @return void
112
     */
113
    public function registerRepeatableUploader(string $uploadName, UploaderInterface $uploader)
114
    {
115
        if (! array_key_exists($uploadName, $this->repeatableUploaders) || ! in_array($uploader, $this->repeatableUploaders[$uploadName])) {
116
            $this->repeatableUploaders[$uploadName][] = $uploader;
117
        }
118
    }
119
120
    /**
121
     * Check if there are uploaders registered for the given upload name.
122
     *
123
     * @param  string  $uploadName
124
     * @return bool
125
     */
126
    public function hasRepeatableUploadersFor(string $uploadName)
127
    {
128
        return array_key_exists($uploadName, $this->repeatableUploaders);
129
    }
130
131
    /**
132
     * Get the repeatable uploaders for the given upload name.
133
     *
134
     * @param  string  $uploadName
135
     * @return array
136
     */
137
    public function getRepeatableUploadersFor(string $uploadName)
138
    {
139
        return $this->repeatableUploaders[$uploadName] ?? [];
140
    }
141
142
    /**
143
     * Check if the specified upload is registered for the given repeatable uploads.
144
     *
145
     * @param  string  $uploadName
146
     * @param  UploaderInterface  $upload
147
     * @return bool
148
     */
149
    public function isUploadRegistered(string $uploadName, UploaderInterface $upload)
150
    {
151
        return $this->hasRepeatableUploadersFor($uploadName) && in_array($upload->getName(), $this->getRegisteredUploadNames($uploadName));
152
    }
153
154
    /**
155
     * Return the registered uploaders names for the given repeatable upload name.
156
     *
157
     * @param  string  $uploadName
158
     * @return array
159
     */
160
    public function getRegisteredUploadNames(string $uploadName)
161
    {
162
        return array_map(function ($uploader) {
163
            return $uploader->getName();
164
        }, $this->getRepeatableUploadersFor($uploadName));
165
    }
166
}
167