Issues (30)

src/ServiceProviders/ServiceProviderAwareTrait.php (5 issues)

1
<?php
2
3
namespace Nip\Container\ServiceProviders;
4
5
use Nip\Config\Config;
6
use Nip\Container\Container;
7
8
/**
9
 * Class ServiceProviderAwareTrait
10
 * @package Nip\Container\ServiceProviders
11
 */
12
trait ServiceProviderAwareTrait
13
{
14
    /**
15
     * All of the registered service providers.
16
     *
17
     * @var ProviderRepository
18
     */
19
    protected $providerRepository = null;
20
21
    public function registerConfiguredProviders()
22
    {
23
        $providers = $this->getConfiguredProviders();
24
        foreach ($providers as $provider) {
25
            $this->getProviderRepository()->add($provider);
26
        }
27
28
        return $this->getProviderRepository()->register();
0 ignored issues
show
Are you sure the usage of $this->getProviderRepository()->register() targeting Nip\Container\ServicePro...rRepository::register() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
29
    }
30
31
    /**
32
     * @return ProviderRepository
33
     */
34
    public function getProviderRepository()
35
    {
36
        if ($this->providerRepository === null) {
37
            $this->providerRepository = new ProviderRepository();
38
            $this->providerRepository->setContainer($this->getContainer());
0 ignored issues
show
It seems like getContainer() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

38
            $this->providerRepository->setContainer($this->/** @scrutinizer ignore-call */ getContainer());
Loading history...
39
        }
40
41
        return $this->providerRepository;
42
    }
43
44
    /**
45
     * @return array
46
     */
47 2
    public function getConfiguredProviders()
48
    {
49 2
        return $this->getConfigProvidersValue($this->getGenericProviders());
50
    }
51
52
    /**
53
     * @param null $default
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $default is correct as it would always require null to be passed?
Loading history...
54
     * @return array|null
55
     * @throws \Exception
56
     */
57 2
    protected function getConfigProvidersValue($default = null)
58
    {
59 2
        if (function_exists('config') === false) {
60
            return $default;
61
        }
62 2
        if (function_exists('app') === false && !(Container::getInstance() instanceof Container)) {
0 ignored issues
show
Nip\Container\Container::getInstance() is always a sub-type of Nip\Container\Container.
Loading history...
63
            return $default;
64
        }
65 2
        $container = function_exists('app') ? app() : Container::getInstance();
66 2
        if ($container->has('config') == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
67 1
            return $default;
68
        }
69 1
        $value = config('app.providers');
70 1
        if ($value instanceof Config) {
71 1
            $value = $value->toArray();
72
        }
73 1
        if (empty($value)) {
74
            return $default;
75
        }
76 1
        return $value;
77
    }
78
79
    /**
80
     * @return array
81
     */
82
    public function getGenericProviders()
83
    {
84
        return [];
85
    }
86
87
    public function bootProviders()
88
    {
89
        $this->getProviderRepository()->boot();
90
    }
91
}
92