Issues (41)

src/Traits/CanBootTraitsTrait.php (4 issues)

1
<?php
2
3
namespace Nip\Utility\Traits;
4
5
use Nip\Utility\Oop;
6
7
/**
8
 * Trait CanBootTraitsTrait
9
 * @package Nip\Utility\Traits
10
 */
11
trait CanBootTraitsTrait
12
{
13
    protected $bootTraits = null;
14
    protected $bootedTraits = [];
15
16
    /**
17
     * Initialize any initializable traits on the model.
18
     *
19
     * @return void
20
     */
21
    public function bootTraits()
22
    {
23
        $needBooting = array_diff($this->getBootTraits(), $this->bootedTraits);
0 ignored issues
show
$this->getBootTraits() of type null is incompatible with the type array expected by parameter $array of array_diff(). ( Ignorable by Annotation )

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

23
        $needBooting = array_diff(/** @scrutinizer ignore-type */ $this->getBootTraits(), $this->bootedTraits);
Loading history...
Are you sure the usage of $this->getBootTraits() targeting Nip\Utility\Traits\CanBo...sTrait::getBootTraits() 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...
24
        foreach ($needBooting as $method) {
25
            $this->{$method}();
26
            $this->bootedTraits[] = $method;
27
        }
28
    }
29
30
    /**
31
     * @return null
32
     */
33
    public function getBootTraits()
34
    {
35
        if ($this->bootTraits === null) {
36
            $this->initBootTraits();
37
        }
38
        return $this->bootTraits;
39
    }
40
41
    /**
42
     * @param null $bootTraits
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $bootTraits is correct as it would always require null to be passed?
Loading history...
43
     */
44
    public function setBootTraits($bootTraits): void
45
    {
46
        $this->bootTraits = $bootTraits;
47
    }
48
49
    /**
50
     * @return bool
51
     */
52
    public function hasBootTraits()
53
    {
54
        return is_array($this->getBootTraits());
0 ignored issues
show
Are you sure the usage of $this->getBootTraits() targeting Nip\Utility\Traits\CanBo...sTrait::getBootTraits() 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...
55
    }
56
57
    protected function initBootTraits()
58
    {
59
        $this->setBootTraits($this->generateBootTraits());
60
    }
61
62
    /**
63
     * @return array
64
     */
65
    protected function generateBootTraits()
66
    {
67
        $traitBoots = [];
68
        $class = static::class;
69
70
        foreach (Oop::uses($class) as $trait) {
71
            $method = 'boot' . Oop::basename($trait);
72
73
            if (method_exists($class, $method)) {
74
                $traitBoots[] = $method;
75
            }
76
        }
77
        return $traitBoots;
78
    }
79
}
80