Completed
Pull Request — master (#1350)
by
unknown
02:50
created

Grid::setLabel()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 3
nop 2
dl 0
loc 11
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace Encore\Admin;
4
5
use Closure;
6
use Encore\Admin\Exception\Handler;
7
use Encore\Admin\Grid\Column;
8
use Encore\Admin\Grid\Displayers\Actions;
9
use Encore\Admin\Grid\Displayers\RowSelector;
10
use Encore\Admin\Grid\Exporter;
11
use Encore\Admin\Grid\Filter;
12
use Encore\Admin\Grid\Model;
13
use Encore\Admin\Grid\Row;
14
use Encore\Admin\Grid\Tools;
15
use Illuminate\Database\Eloquent\Model as Eloquent;
16
use Illuminate\Database\Eloquent\Relations\BelongsTo;
17
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
18
use Illuminate\Database\Eloquent\Relations\HasMany;
19
use Illuminate\Database\Eloquent\Relations\HasOne;
20
use Illuminate\Database\Eloquent\Relations\MorphToMany;
21
use Illuminate\Database\Eloquent\Relations\Relation;
22
use Illuminate\Support\Collection;
23
use Illuminate\Support\Facades\Input;
24
use Illuminate\Support\Facades\Lang;
25
use Illuminate\Support\Facades\Schema;
26
use Jenssegers\Mongodb\Eloquent\Model as MongodbModel;
27
28
class Grid
29
{
30
    /**
31
     * The grid data model instance.
32
     *
33
     * @var \Encore\Admin\Grid\Model
34
     */
35
    protected $model;
36
37
    /**
38
     * Collection of all grid columns.
39
     *
40
     * @var \Illuminate\Support\Collection
41
     */
42
    protected $columns;
43
44
    /**
45
     * Collection of table columns.
46
     *
47
     * @var \Illuminate\Support\Collection
48
     */
49
    protected $dbColumns;
50
51
    /**
52
     * Collection of all data rows.
53
     *
54
     * @var \Illuminate\Support\Collection
55
     */
56
    protected $rows;
57
58
    /**
59
     * Rows callable fucntion.
60
     *
61
     * @var \Closure
62
     */
63
    protected $rowsCallback;
64
65
    /**
66
     * All column names of the grid.
67
     *
68
     * @var array
69
     */
70
    public $columnNames = [];
71
72
    /**
73
     * Grid builder.
74
     *
75
     * @var \Closure
76
     */
77
    protected $builder;
78
79
    /**
80
     * Mark if the grid is builded.
81
     *
82
     * @var bool
83
     */
84
    protected $builded = false;
85
86
    /**
87
     * All variables in grid view.
88
     *
89
     * @var array
90
     */
91
    protected $variables = [];
92
93
    /**
94
     * The grid Filter.
95
     *
96
     * @var \Encore\Admin\Grid\Filter
97
     */
98
    protected $filter;
99
100
    /**
101
     * Resource path of the grid.
102
     *
103
     * @var
104
     */
105
    protected $resourcePath;
106
107
    /**
108
     * Default primary key name.
109
     *
110
     * @var string
111
     */
112
    protected $keyName = 'id';
113
114
    /**
115
     * Export driver.
116
     *
117
     * @var string
118
     */
119
    protected $exporter;
120
121
    /**
122
     * View for grid to render.
123
     *
124
     * @var string
125
     */
126
    protected $view = 'admin::grid.table';
127
128
    /**
129
     * Per-page options.
130
     *
131
     * @var array
132
     */
133
    public $perPages = [10, 20, 30, 50, 100];
134
135
    /**
136
     * Default items count per-page.
137
     *
138
     * @var int
139
     */
140
    public $perPage = 20;
141
142
    /**
143
     * Header tools.
144
     *
145
     * @var Tools
146
     */
147
    public $tools;
148
149
    /**
150
     * Callback for grid actions.
151
     *
152
     * @var Closure
153
     */
154
    protected $actionsCallback;
155
156
    /**
157
     * Options for grid.
158
     *
159
     * @var array
160
     */
161
    protected $options = [
162
        'usePagination'     => true,
163
        'useFilter'         => true,
164
        'useExporter'       => true,
165
        'useActions'        => true,
166
        'useRowSelector'    => true,
167
        'allowCreate'       => true,
168
    ];
169
170
    /**
171
     * @var Tools\Footer
172
     */
173
    protected $footer;
174
175
    /**
176
     * Create a new grid instance.
177
     *
178
     * @param Eloquent $model
179
     * @param Closure  $builder
180
     */
181
    public function __construct(Eloquent $model, Closure $builder)
182
    {
183
        $this->keyName = $model->getKeyName();
184
        $this->model = new Model($model);
185
        $this->columns = new Collection();
186
        $this->rows = new Collection();
187
        $this->builder = $builder;
188
189
        $this->setupTools();
190
        $this->setupFilter();
191
        $this->setupExporter();
192
    }
193
194
    /**
195
     * Setup grid tools.
196
     */
197
    public function setupTools()
198
    {
199
        $this->tools = new Tools($this);
200
    }
201
202
    /**
203
     * Setup grid filter.
204
     *
205
     * @return void
206
     */
207
    protected function setupFilter()
208
    {
209
        $this->filter = new Filter($this->model());
210
    }
211
212
    /**
213
     * Setup grid exporter.
214
     *
215
     * @return void
216
     */
217
    protected function setupExporter()
218
    {
219
        if ($scope = Input::get(Exporter::$queryName)) {
220
            $this->model()->usePaginate(false);
221
222
            call_user_func($this->builder, $this);
223
224
            (new Exporter($this))->resolve($this->exporter)->withScope($scope)->export();
225
        }
226
    }
227
228
    /**
229
     * Get or set option for grid.
230
     *
231
     * @param string $key
232
     * @param mixed  $value
233
     *
234
     * @return $this|mixed
235
     */
236
    public function option($key, $value = null)
237
    {
238
        if (is_null($value)) {
239
            return $this->options[$key];
240
        }
241
242
        $this->options[$key] = $value;
243
244
        return $this;
245
    }
246
247
    /**
248
     * Get primary key name of model.
249
     *
250
     * @return string
251
     */
252
    public function getKeyName()
253
    {
254
        return $this->keyName ?: 'id';
255
    }
256
257
    /**
258
     * Add column to Grid.
259
     *
260
     * @param string $name
261
     * @param string $label
262
     *
263
     * @return Column
264
     */
265
    public function column($name, $label = '')
266
    {
267
        $relationName = $relationColumn = '';
268
269
        if (strpos($name, '.') !== false) {
270
            list($relationName, $relationColumn) = explode('.', $name);
271
272
            $relation = $this->model()->eloquent()->$relationName();
273
274
            $label = $this->setLabel($label,$relationColumn);
275
//            $label = empty($label) ? ucfirst($relationColumn) : $label;
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
276
            $name = snake_case($relationName).'.'.$relationColumn;
277
        }
278
279
        $column = $this->addColumn($name, $label);
280
281
        if (isset($relation) && $relation instanceof Relation) {
282
            $this->model()->with($relationName);
0 ignored issues
show
Documentation Bug introduced by
The method with does not exist on object<Encore\Admin\Grid\Model>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
283
            $column->setRelation($relationName, $relationColumn);
284
        }
285
286
        return $column;
287
    }
288
289
    public function setLabel($label , $relationColumn)
290
    {
291
        $trans_key = 'validation.attributes.' . strtolower($relationColumn);
292
293
         if (empty($label) && Lang::has($trans_key)) {
294
            $label = Lang::get($trans_key);
295
        }else if (empty($label)) {
296
             $label = ucfirst($relationColumn);
297
         }
298
        return $label;
299
    }
300
301
    /**
302
     * Batch add column to grid.
303
     *
304
     * @example
305
     * 1.$grid->columns(['name' => 'Name', 'email' => 'Email' ...]);
306
     * 2.$grid->columns('name', 'email' ...)
307
     *
308
     * @param array $columns
309
     *
310
     * @return Collection|null
311
     */
312
    public function columns($columns = [])
313
    {
314
        if (func_num_args() == 0) {
315
            return $this->columns;
316
        }
317
318
        if (func_num_args() == 1 && is_array($columns)) {
319
            foreach ($columns as $column => $label) {
320
                $this->column($column, $label);
321
            }
322
323
            return;
324
        }
325
326
        foreach (func_get_args() as $column) {
327
            $this->column($column);
328
        }
329
    }
330
331
    /**
332
     * Add column to grid.
333
     *
334
     * @param string $column
335
     * @param string $label
336
     *
337
     * @return Column
338
     */
339
    protected function addColumn($column = '', $label = '')
340
    {
341
        $column = new Column($column, $label);
342
        $column->setGrid($this);
343
344
        return $this->columns[] = $column;
345
    }
346
347
    /**
348
     * Get Grid model.
349
     *
350
     * @return Model
351
     */
352
    public function model()
353
    {
354
        return $this->model;
355
    }
356
357
    /**
358
     * Paginate the grid.
359
     *
360
     * @param int $perPage
361
     *
362
     * @return void
363
     */
364
    public function paginate($perPage = 20)
365
    {
366
        $this->perPage = $perPage;
367
368
        $this->model()->paginate($perPage);
0 ignored issues
show
Bug introduced by
The method paginate() does not exist on Encore\Admin\Grid\Model. Did you maybe mean usePaginate()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
369
    }
370
371
    /**
372
     * Get the grid paginator.
373
     *
374
     * @return mixed
375
     */
376
    public function paginator()
377
    {
378
        return new Tools\Paginator($this);
379
    }
380
381
    /**
382
     * Disable grid pagination.
383
     *
384
     * @return $this
385
     */
386
    public function disablePagination()
387
    {
388
        $this->model->usePaginate(false);
389
390
        $this->option('usePagination', false);
391
392
        return $this;
393
    }
394
395
    /**
396
     * If this grid use pagination.
397
     *
398
     * @return bool
399
     */
400
    public function usePagination()
401
    {
402
        return $this->option('usePagination');
403
    }
404
405
    /**
406
     * Set per-page options.
407
     *
408
     * @param array $perPages
409
     */
410
    public function perPages(array $perPages)
411
    {
412
        $this->perPages = $perPages;
413
    }
414
415
    /**
416
     * Disable all actions.
417
     *
418
     * @return $this
419
     */
420
    public function disableActions()
421
    {
422
        return $this->option('useActions', false);
423
    }
424
425
    /**
426
     * Set grid action callback.
427
     *
428
     * @param Closure $callback
429
     *
430
     * @return $this
431
     */
432
    public function actions(Closure $callback)
433
    {
434
        $this->actionsCallback = $callback;
435
436
        return $this;
437
    }
438
439
    /**
440
     * Add `actions` column for grid.
441
     *
442
     * @return void
443
     */
444
    protected function appendActionsColumn()
445
    {
446
        if (!$this->option('useActions')) {
447
            return;
448
        }
449
450
        $grid = $this;
451
        $callback = $this->actionsCallback;
452
        $column = $this->addColumn('__actions__', trans('admin.action'));
453
454
        $column->display(function ($value) use ($grid, $column, $callback) {
455
            $actions = new Actions($value, $grid, $column, $this);
0 ignored issues
show
Documentation introduced by
$this is of type this<Encore\Admin\Grid>, but the function expects a object<stdClass>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
456
457
            return $actions->display($callback);
458
        });
459
    }
460
461
    /**
462
     * Disable row selector.
463
     *
464
     * @return Grid|mixed
465
     */
466
    public function disableRowSelector()
467
    {
468
        $this->tools(function ($tools) {
469
            /* @var Grid\Tools $tools */
470
            $tools->disableBatchActions();
471
        });
472
473
        return $this->option('useRowSelector', false);
474
    }
475
476
    /**
477
     * Prepend checkbox column for grid.
478
     *
479
     * @return void
480
     */
481
    protected function prependRowSelectorColumn()
482
    {
483
        if (!$this->option('useRowSelector')) {
484
            return;
485
        }
486
487
        $grid = $this;
488
489
        $column = new Column(Column::SELECT_COLUMN_NAME, ' ');
490
        $column->setGrid($this);
491
492
        $column->display(function ($value) use ($grid, $column) {
493
            $actions = new RowSelector($value, $grid, $column, $this);
0 ignored issues
show
Documentation introduced by
$this is of type this<Encore\Admin\Grid>, but the function expects a object<stdClass>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
494
495
            return $actions->display();
496
        });
497
498
        $this->columns->prepend($column);
499
    }
500
501
    /**
502
     * Build the grid.
503
     *
504
     * @return void
505
     */
506
    public function build()
507
    {
508
        if ($this->builded) {
509
            return;
510
        }
511
512
        $data = $this->processFilter();
513
514
        $this->prependRowSelectorColumn();
515
        $this->appendActionsColumn();
516
517
        Column::setOriginalGridData($data);
518
519
        $this->columns->map(function (Column $column) use (&$data) {
520
            $data = $column->fill($data);
521
522
            $this->columnNames[] = $column->getName();
523
        });
524
525
        $this->buildRows($data);
526
527
        $this->builded = true;
528
    }
529
530
    /**
531
     * Disable grid filter.
532
     *
533
     * @return $this
534
     */
535
    public function disableFilter()
536
    {
537
        $this->option('useFilter', false);
538
539
        return $this;
540
    }
541
542
    /**
543
     * Get filter of Grid.
544
     *
545
     * @return Filter
546
     */
547
    public function getFilter()
548
    {
549
        return $this->filter;
550
    }
551
552
    /**
553
     * Process the grid filter.
554
     *
555
     * @return array
556
     */
557
    public function processFilter()
558
    {
559
        call_user_func($this->builder, $this);
560
561
        return $this->filter->execute();
562
    }
563
564
    /**
565
     * Set the grid filter.
566
     *
567
     * @param Closure $callback
568
     */
569
    public function filter(Closure $callback)
570
    {
571
        call_user_func($callback, $this->filter);
572
    }
573
574
    /**
575
     * Render the grid filter.
576
     *
577
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|string
578
     */
579
    public function renderFilter()
580
    {
581
        if (!$this->option('useFilter')) {
582
            return '';
583
        }
584
585
        return $this->filter->render();
586
    }
587
588
    /**
589
     * Build the grid rows.
590
     *
591
     * @param array $data
592
     *
593
     * @return void
594
     */
595
    protected function buildRows(array $data)
596
    {
597
        $this->rows = collect($data)->map(function ($model, $number) {
598
            return new Row($number, $model);
599
        });
600
601
        if ($this->rowsCallback) {
602
            $this->rows->map($this->rowsCallback);
603
        }
604
    }
605
606
    /**
607
     * Set grid row callback function.
608
     *
609
     * @param Closure $callable
610
     *
611
     * @return Collection|null
612
     */
613
    public function rows(Closure $callable = null)
614
    {
615
        if (is_null($callable)) {
616
            return $this->rows;
617
        }
618
619
        $this->rowsCallback = $callable;
620
    }
621
622
    /**
623
     * Setup grid tools.
624
     *
625
     * @param Closure $callback
626
     *
627
     * @return void
628
     */
629
    public function tools(Closure $callback)
630
    {
631
        call_user_func($callback, $this->tools);
632
    }
633
634
    /**
635
     * Render custom tools.
636
     *
637
     * @return string
638
     */
639
    public function renderHeaderTools()
640
    {
641
        return $this->tools->render();
642
    }
643
644
    /**
645
     * Set exporter driver for Grid to export.
646
     *
647
     * @param $exporter
648
     *
649
     * @return $this
650
     */
651
    public function exporter($exporter)
652
    {
653
        $this->exporter = $exporter;
654
655
        return $this;
656
    }
657
658
    /**
659
     * Get the export url.
660
     *
661
     * @param int  $scope
662
     * @param null $args
663
     *
664
     * @return string
665
     */
666
    public function exportUrl($scope = 1, $args = null)
667
    {
668
        $input = array_merge(Input::all(), Exporter::formatExportQuery($scope, $args));
669
670
        return $this->resource().'?'.http_build_query($input);
671
    }
672
673
    /**
674
     * If grid allows export.s.
675
     *
676
     * @return bool
677
     */
678
    public function allowExport()
679
    {
680
        return $this->option('useExporter');
681
    }
682
683
    /**
684
     * Disable export.
685
     *
686
     * @return $this
687
     */
688
    public function disableExport()
689
    {
690
        return $this->option('useExporter', false);
691
    }
692
693
    /**
694
     * Render export button.
695
     *
696
     * @return Tools\ExportButton
697
     */
698
    public function renderExportButton()
699
    {
700
        return new Tools\ExportButton($this);
701
    }
702
703
    /**
704
     * Disable creation.
705
     *
706
     * @return $this
707
     */
708
    public function disableCreation()
709
    {
710
        return $this->option('allowCreate', false);
711
    }
712
713
    /**
714
     * If allow creation.
715
     *
716
     * @return bool
717
     */
718
    public function allowCreation()
719
    {
720
        return $this->option('allowCreate');
721
    }
722
723
    /**
724
     * Render create button for grid.
725
     *
726
     * @return Tools\CreateButton
727
     */
728
    public function renderCreateButton()
729
    {
730
        return new Tools\CreateButton($this);
731
    }
732
733
    /**
734
     * Set grid footer.
735
     *
736
     * @param Closure|null $closure
737
     *
738
     * @return $this|Tools\Footer
739
     */
740
    public function footer(Closure $closure = null)
741
    {
742
        if (!$closure) {
743
            return $this->footer;
744
        }
745
746
        $this->footer = $closure;
0 ignored issues
show
Documentation Bug introduced by
It seems like $closure of type object<Closure> is incompatible with the declared type object<Encore\Admin\Grid\Tools\Footer> of property $footer.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
747
748
        return $this;
749
    }
750
751
    /**
752
     * Render grid footer.
753
     *
754
     * @return Tools\Footer|string
755
     */
756
    public function renderFooter()
757
    {
758
        if (!$this->footer) {
759
            return '';
760
        }
761
762
        return new Tools\Footer($this);
763
    }
764
765
    /**
766
     * Get current resource uri.
767
     *
768
     * @param string $path
769
     *
770
     * @return string
771
     */
772
    public function resource($path = null)
773
    {
774
        if (!empty($path)) {
775
            $this->resourcePath = $path;
776
777
            return $this;
778
        }
779
780
        if (!empty($this->resourcePath)) {
781
            return $this->resourcePath;
782
        }
783
784
        return app('request')->getPathInfo();
785
    }
786
787
    /**
788
     * Get the table columns for grid.
789
     *
790
     * @return void
791
     */
792
    protected function setDbColumns()
793
    {
794
        $connection = $this->model()->eloquent()->getConnectionName();
795
796
        $this->dbColumns = collect(Schema::connection($connection)->getColumnListing($this->model()->getTable()));
797
    }
798
799
    /**
800
     * Handle table column for grid.
801
     *
802
     * @param string $method
803
     * @param string $label
804
     *
805
     * @return bool|Column
806
     */
807
    protected function handleTableColumn($method, $label)
808
    {
809
        if (empty($this->dbColumns)) {
810
            $this->setDbColumns();
811
        }
812
813
        if ($this->dbColumns->has($method)) {
814
            return $this->addColumn($method, $label);
815
        }
816
817
        return false;
818
    }
819
820
    /**
821
     * Handle get mutator column for grid.
822
     *
823
     * @param string $method
824
     * @param string $label
825
     *
826
     * @return bool|Column
827
     */
828
    protected function handleGetMutatorColumn($method, $label)
829
    {
830
        if ($this->model()->eloquent()->hasGetMutator($method)) {
831
            return $this->addColumn($method, $label);
832
        }
833
834
        return false;
835
    }
836
837
    /**
838
     * Handle relation column for grid.
839
     *
840
     * @param string $method
841
     * @param string $label
842
     *
843
     * @return bool|Column
844
     */
845
    protected function handleRelationColumn($method, $label)
846
    {
847
        $model = $this->model()->eloquent();
848
849
        if (!method_exists($model, $method)) {
850
            return false;
851
        }
852
853
        if (!($relation = $model->$method()) instanceof Relation) {
854
            return false;
855
        }
856
857
        if ($relation instanceof HasOne || $relation instanceof BelongsTo) {
858
            $this->model()->with($method);
0 ignored issues
show
Documentation Bug introduced by
The method with does not exist on object<Encore\Admin\Grid\Model>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
859
860
            return $this->addColumn($method, $label)->setRelation(snake_case($method));
861
        }
862
863
        if ($relation instanceof HasMany || $relation instanceof BelongsToMany || $relation instanceof MorphToMany) {
864
            $this->model()->with($method);
0 ignored issues
show
Documentation Bug introduced by
The method with does not exist on object<Encore\Admin\Grid\Model>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
865
866
            return $this->addColumn(snake_case($method), $label);
867
        }
868
869
        return false;
870
    }
871
872
    /**
873
     * Dynamically add columns to the grid view.
874
     *
875
     * @param $method
876
     * @param $arguments
877
     *
878
     * @return Column
879
     */
880
    public function __call($method, $arguments)
881
    {
882
//        $label = isset($arguments[0]) ? $arguments[0] : ucfirst($method);
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
883
        $label = isset($arguments[0]) ? $arguments[0] : null;
884
        $label = $this->setLabel($label,$method);
885
886
        if ($this->model()->eloquent() instanceof MongodbModel) {
0 ignored issues
show
Bug introduced by
The class Jenssegers\Mongodb\Eloquent\Model does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
887
            return $this->addColumn($method, $label);
888
        }
889
890
        if ($column = $this->handleGetMutatorColumn($method, $label)) {
891
            return $column;
892
        }
893
894
        if ($column = $this->handleRelationColumn($method, $label)) {
895
            return $column;
896
        }
897
898
        if ($column = $this->handleTableColumn($method, $label)) {
899
            return $column;
900
        }
901
902
        return $this->addColumn($method, $label);
903
    }
904
905
    /**
906
     * Register column displayers.
907
     *
908
     * @return void.
0 ignored issues
show
Documentation introduced by
The doc-type void. could not be parsed: Unknown type name "void." at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
909
     */
910
    public static function registerColumnDisplayer()
911
    {
912
        $map = [
913
            'editable'      => \Encore\Admin\Grid\Displayers\Editable::class,
914
            'switch'        => \Encore\Admin\Grid\Displayers\SwitchDisplay::class,
915
            'switchGroup'   => \Encore\Admin\Grid\Displayers\SwitchGroup::class,
916
            'select'        => \Encore\Admin\Grid\Displayers\Select::class,
917
            'image'         => \Encore\Admin\Grid\Displayers\Image::class,
918
            'label'         => \Encore\Admin\Grid\Displayers\Label::class,
919
            'button'        => \Encore\Admin\Grid\Displayers\Button::class,
920
            'link'          => \Encore\Admin\Grid\Displayers\Link::class,
921
            'badge'         => \Encore\Admin\Grid\Displayers\Badge::class,
922
            'progressBar'   => \Encore\Admin\Grid\Displayers\ProgressBar::class,
923
            'radio'         => \Encore\Admin\Grid\Displayers\Radio::class,
924
            'checkbox'      => \Encore\Admin\Grid\Displayers\Checkbox::class,
925
            'orderable'     => \Encore\Admin\Grid\Displayers\Orderable::class,
926
        ];
927
928
        foreach ($map as $abstract => $class) {
929
            Column::extend($abstract, $class);
930
        }
931
    }
932
933
    /**
934
     * Add variables to grid view.
935
     *
936
     * @param array $variables
937
     *
938
     * @return $this
939
     */
940
    public function with($variables = [])
941
    {
942
        $this->variables = $variables;
943
944
        return $this;
945
    }
946
947
    /**
948
     * Get all variables will used in grid view.
949
     *
950
     * @return array
951
     */
952
    protected function variables()
953
    {
954
        $this->variables['grid'] = $this;
955
956
        return $this->variables;
957
    }
958
959
    /**
960
     * Set a view to render.
961
     *
962
     * @param string $view
963
     * @param array  $variables
964
     */
965
    public function setView($view, $variables = [])
966
    {
967
        if (!empty($variables)) {
968
            $this->with($variables);
969
        }
970
971
        $this->view = $view;
972
    }
973
974
    /**
975
     * Get the string contents of the grid view.
976
     *
977
     * @return string
978
     */
979
    public function render()
980
    {
981
        try {
982
            $this->build();
983
        } catch (\Exception $e) {
984
            return Handler::renderException($e);
985
        }
986
987
        return view($this->view, $this->variables())->render();
0 ignored issues
show
Bug introduced by
The method render does only exist in Illuminate\View\View, but not in Illuminate\Contracts\View\Factory.

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...
988
    }
989
990
    /**
991
     * Get the string contents of the grid view.
992
     *
993
     * @return string
994
     */
995
    public function __toString()
996
    {
997
        return $this->render();
998
    }
999
}
1000