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 — v4dot1 ( bece96 )
by Cristian
10:39 queued 08:03
created

FetchOperation::fetch()   B

Complexity

Conditions 9
Paths 49

Size

Total Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
nc 49
nop 1
dl 0
loc 43
rs 7.6764
c 0
b 0
f 0
1
<?php
2
3
namespace Backpack\CRUD\app\Http\Controllers\Operations;
4
5
use Illuminate\Support\Facades\Route;
6
use Illuminate\Support\Str;
7
8
trait FetchOperation
9
{
10
    /**
11
     * Define which routes are needed for this operation.
12
     *
13
     * @param string $segment    Name of the current entity (singular). Used as first URL segment.
14
     * @param string $routeName  Prefix of the route name.
15
     * @param string $controller Name of the current CrudController.
16
     */
17
    protected function setupFetchOperationRoutes($segment, $routeName, $controller)
18
    {
19
        // get all method names on the current model that start with "fetch" (ex: fetchCategory)
20
        // if a method that looks like that is present, it means we need to add the routes that fetch that entity
21
        preg_match_all('/(?<=^|;)fetch([^;]+?)(;|$)/', implode(';', get_class_methods($this)), $matches);
22
23
        if (count($matches[1])) {
24
            foreach ($matches[1] as $methodName) {
25
                Route::post($segment.'/fetch/'.Str::kebab($methodName), [
26
                    'uses'      => $controller.'@fetch'.$methodName,
27
                    'operation' => 'FetchOperation',
28
                ]);
29
            }
30
        }
31
    }
32
33
    /**
34
     * Gets items from database and returns to selects.
35
     *
36
     * @param string|array $arg
37
     * @return void
38
     */
39
    private function fetch($arg)
40
    {
41
        // get the actual words that were used to search for an item (the search term / search string)
42
        $searchString = request()->input('q') ?? false;
43
44
        // if the Class was passed as the sole argument, use that as the configured Model
45
        // otherwise assume the arguments are actually the configuration array
46
        $config = [];
47
48
        if (! is_array($arg)) {
49
            if (! class_exists($arg)) {
50
                return response()->json(['error' => 'Class: '.$arg.' does not exists'], 500);
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
51
            }
52
            $config['model'] = $arg;
53
        } else {
54
            $config = $arg;
55
        }
56
57
        // set configuration defaults
58
        $config['itemsPerPage'] = $config['itemsPerPage'] ?? 10;
59
        $config['searchableAttributes'] = $config['searchableAttributes'] ?? [$config['model']::getIdentifiableName()];
60
        $config['query'] = isset($config['query']) && is_callable($config['query']) ? $config['query']($config['model']) : new $config['model']; // if a closure that has been passed as "query", use the closure - otherwise use the model
61
62
        // FetchOperation sends an empty query to retrieve the default entry for select when field is not nullable.
63
        if ($searchString === false) {
64
            return $config['query']->first();
65
        }
66
67
        // for each searchable attribute, add a WHERE clause
68
        foreach ($config['searchableAttributes'] as $k => $searchColumn) {
69
            $operation = ($k == 0) ? 'where' : 'orWhere';
70
            $columnType = $config['query']->getColumnType($searchColumn);
71
72
            if ($columnType == 'string') {
73
                $config['query'] = $config['query']->{$operation}($searchColumn, 'LIKE', '%'.$searchString.'%');
74
            } else {
75
                $config['query'] = $config['query']->{$operation}($searchColumn, $searchString);
76
            }
77
        }
78
79
        // return the paginated results
80
        return $config['query']->paginate($config['itemsPerPage']);
81
    }
82
}
83