GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 869845...9ac565 )
by James
08:24 queued 03:31
created

SearchController::search()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 27
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 14
nc 4
nop 2
1
<?php
2
/**
3
 * SearchController.php
4
 * Copyright (c) 2017 [email protected]
5
 *
6
 * This file is part of Firefly III.
7
 *
8
 * Firefly III is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * Firefly III is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with Firefly III.  If not, see <http://www.gnu.org/licenses/>.
20
 */
21
22
declare(strict_types=1);
23
24
namespace FireflyIII\Http\Controllers;
25
26
use FireflyIII\Support\CacheProperties;
27
use FireflyIII\Support\Search\SearchInterface;
28
use Illuminate\Http\Request;
29
use Response;
30
use View;
31
32
/**
33
 * Class SearchController
34
 *
35
 * @package FireflyIII\Http\Controllers
36
 */
37
class SearchController extends Controller
38
{
39
    /**
40
     * SearchController constructor.
41
     */
42
    public function __construct()
43
    {
44
        parent::__construct();
45
46
        $this->middleware(
47
            function ($request, $next) {
48
                View::share('mainTitleIcon', 'fa-search');
49
                View::share('title', trans('firefly.search'));
50
51
                return $next($request);
52
            }
53
        );
54
55
    }
56
57
    /**
58
     * @param Request         $request
59
     * @param SearchInterface $searcher
60
     *
61
     * @return View
62
     */
63
    public function index(Request $request, SearchInterface $searcher)
64
    {
65
        $fullQuery = strval($request->get('q'));
66
67
        // parse search terms:
68
        $searcher->parseQuery($fullQuery);
69
        $query    = $searcher->getWordsAsString();
70
        $subTitle = trans('breadcrumbs.search_result', ['query' => $query]);
71
72
        return view('search.index', compact('query', 'fullQuery', 'subTitle'));
73
    }
74
75
    public function search(Request $request, SearchInterface $searcher)
76
    {
77
        $fullQuery = strval($request->get('query'));
78
79
        // cache
80
        $cache = new CacheProperties;
81
        $cache->addProperty('search');
82
        $cache->addProperty($fullQuery);
83
84
        if ($cache->has()) {
85
            $transactions = $cache->get();
86
        }
87
88
        if (!$cache->has()) {
89
            // parse search terms:
90
            $searcher->parseQuery($fullQuery);
91
            $searcher->setLimit(intval(env('SEARCH_RESULT_LIMIT', 50)));
92
            $transactions = $searcher->searchTransactions();
93
            $cache->store($transactions);
94
        }
95
96
        $html = view('search.search', compact('transactions'))->render();
97
98
        return Response::json(['count' => $transactions->count(), 'html' => $html]);
0 ignored issues
show
Bug introduced by
The variable $transactions does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
99
100
101
    }
102
103
}
104