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 — v6-livewire-widget ( bb8951...53543e )
by Pedro
14:58
created

AddMenuContent::__construct()   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 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Backpack\CRUD\app\Console\Commands;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Support\Facades\Storage;
7
8
class AddMenuContent extends Command
9
{
10
    use \Backpack\CRUD\app\Console\Commands\Traits\PrettyCommandOutput;
0 ignored issues
show
introduced by
The trait Backpack\CRUD\app\Consol...its\PrettyCommandOutput requires some properties which are not provided by Backpack\CRUD\app\Console\Commands\AddMenuContent: $progressBar, $statusColor, $status
Loading history...
11
12
    /**
13
     * The name and signature of the console command.
14
     *
15
     * @var string
16
     */
17
    protected $signature = 'backpack:add-menu-content
18
                                {code : HTML/PHP code that shows menu items. Use either single quotes or double quotes. Never both. }';
19
20
    /**
21
     * The console command description.
22
     *
23
     * @var string
24
     */
25
    protected $description = 'Add HTML/PHP code to the Backpack menu_items file';
26
27
    /**
28
     * Create a new command instance.
29
     *
30
     * @return void
31
     */
32
    public function __construct()
33
    {
34
        parent::__construct();
35
    }
36
37
    /**
38
     * Execute the console command.
39
     *
40
     * @return mixed
41
     */
42
    public function handle()
43
    {
44
        $path = 'resources/views/vendor/backpack/ui/inc/menu_items.blade.php';
45
        $disk_name = config('backpack.base.root_disk_name');
46
        $disk = Storage::disk($disk_name);
47
        $code = $this->argument('code');
48
49
        $this->progressBlock("Adding menu entry to <fg=blue>$path</>");
50
51
        // Validate file exists
52
        if (! $disk->exists($path)) {
53
            $this->errorProgressBlock();
54
            $this->note('The menu_items file does not exist. Make sure Backpack is properly installed.', 'red');
55
56
            return;
57
        }
58
59
        $contents = $disk->get($path);
60
        $file_lines = file($disk->path($path), FILE_IGNORE_NEW_LINES);
61
62
        // Validate the entry already exists
63
        if ($this->getLastLineNumberThatContains($code, $file_lines)) {
0 ignored issues
show
Bug introduced by
It seems like $code can also be of type array; however, parameter $needle of Backpack\CRUD\app\Consol...ineNumberThatContains() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

63
        if ($this->getLastLineNumberThatContains(/** @scrutinizer ignore-type */ $code, $file_lines)) {
Loading history...
64
            $this->closeProgressBlock('Already existed', 'yellow');
65
66
            return;
67
        }
68
69
        if (! $disk->put($path, $contents.PHP_EOL.$code)) {
70
            $this->errorProgressBlock();
71
            $this->note('Could not write to menu_items file.', 'red');
72
73
            return;
74
        }
75
76
        $this->closeProgressBlock();
77
    }
78
79
    /**
80
     * Parse the given file stream and return the line number where a string is found.
81
     *
82
     * @param  string  $needle  The string that's being searched for.
83
     * @param  array  $haystack  The file where the search is being performed.
84
     * @return bool|int The last line number where the string was found. Or false.
85
     */
86
    private function getLastLineNumberThatContains($needle, $haystack)
87
    {
88
        $matchingLines = array_filter($haystack, function ($k) use ($needle) {
89
            return strpos($k, $needle) !== false;
90
        });
91
92
        if ($matchingLines) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $matchingLines of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
93
            return array_key_last($matchingLines);
0 ignored issues
show
Bug Best Practice introduced by
The expression return array_key_last($matchingLines) also could return the type string which is incompatible with the documented return type boolean|integer.
Loading history...
94
        }
95
96
        return false;
97
    }
98
}
99