Completed
Branch develop-3.0 (4fe777)
by Mohamed
11:06
created

CollectionMacroServiceProvider   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 15
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 1
lcom 0
cbo 2
dl 0
loc 15
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A register() 0 12 1
1
<?php
2
3
/*
4
 * This file is part of the Tinyissue package.
5
 *
6
 * (c) Mohamed Alsharaf <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Tinyissue\Providers;
13
14
use Illuminate\Support\Collection;
15
use Illuminate\Support\ServiceProvider;
16
17
/**
18
 * @author Mohamed Alsharaf <[email protected]>
19
 */
20
class CollectionMacroServiceProvider extends ServiceProvider
21
{
22
    public function register()
23
    {
24
        // Fetch an element from collection by a field 'name'
25
        Collection::macro('getByName', function ($type) {
26
            return $this->where('name', $type)->first();
0 ignored issues
show
Documentation Bug introduced by
The method where does not exist on object<Tinyissue\Provide...onMacroServiceProvider>? 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...
27
        });
28
29
        // Convert collections to pluck array for html select usage
30
        Collection::macro('dropdown', function ($text = 'name', $value = 'id') {
31
            return $this->pluck($text, $value)->all();
0 ignored issues
show
Documentation Bug introduced by
The method pluck does not exist on object<Tinyissue\Provide...onMacroServiceProvider>? 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...
32
        });
33
    }
34
}
35