Passed
Push — main ( 1a8771...d7bb24 )
by Andrey
11:52 queued 10:31
created

Instance::callWhen()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
c 0
b 0
f 0
nc 1
nop 3
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 2
rs 10
1
<?php
2
3
namespace Helldar\Support\Helpers;
4
5
use Helldar\Support\Facades\Helpers\Arr as ArrHelper;
6
use Helldar\Support\Facades\Helpers\Is as IsHelper;
7
use Helldar\Support\Facades\Helpers\Reflection as ReflectionHelper;
8
use ReflectionClass;
9
10
final class Instance
11
{
12
    /**
13
     * Checks if the item being checked inherits from other objects and interfaces.
14
     *
15
     * @param  object|string  $haystack
16
     * @param  string|string[]  $needles
17
     *
18
     * @return bool
19
     */
20 88
    public function of($haystack, $needles): bool
21
    {
22 88
        if (! $this->exists($haystack)) {
23 76
            return false;
24
        }
25
26 36
        $reflection = $this->resolve($haystack);
27 36
        $classname  = $this->classname($haystack);
28
29 36
        foreach (ArrHelper::wrap($needles) as $needle) {
30 36
            if (! $this->exists($needle)) {
31 2
                continue;
32
            }
33
34
            if (
35 36
                $haystack instanceof $needle ||
36 14
                $classname === $this->classname($needle) ||
37 14
                $reflection->isSubclassOf($needle) ||
38 36
                ($reflection->isInterface() && $reflection->implementsInterface($needle))
39
            ) {
40 28
                return true;
41
            }
42
        }
43
44 14
        return false;
45
    }
46
47
    /**
48
     * Extract the trailing name component from a file path.
49
     *
50
     * @param  object|string  $class
51
     *
52
     * @return string|null
53
     */
54 2
    public function basename($class): ?string
55
    {
56 2
        $class = $this->classname($class);
57
58 2
        return basename(str_replace('\\', '/', $class)) ?: null;
59
    }
60
61
    /**
62
     * Gets the class name of the object.
63
     *
64
     * @param  object|string|null  $class
65
     *
66
     * @return string|null
67
     */
68 42
    public function classname($class = null): ?string
69
    {
70 42
        if (IsHelper::object($class)) {
71 40
            return get_class($class);
0 ignored issues
show
Bug introduced by
It seems like $class can also be of type null and string; however, parameter $object of get_class() does only seem to accept object, 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

71
            return get_class(/** @scrutinizer ignore-type */ $class);
Loading history...
72
        }
73
74 20
        return class_exists($class) || interface_exists($class) ? $class : null;
0 ignored issues
show
Bug Best Practice introduced by
The expression return class_exists($cla...$class) ? $class : null could return the type object which is incompatible with the type-hinted return null|string. Consider adding an additional type-check to rule them out.
Loading history...
Bug introduced by
It seems like $class can also be of type null and object; however, parameter $interface of interface_exists() 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

74
        return class_exists($class) || interface_exists(/** @scrutinizer ignore-type */ $class) ? $class : null;
Loading history...
Bug introduced by
It seems like $class can also be of type null and object; however, parameter $class of class_exists() 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

74
        return class_exists(/** @scrutinizer ignore-type */ $class) || interface_exists($class) ? $class : null;
Loading history...
75
    }
76
77
    /**
78
     * Checks if the object exists.
79
     *
80
     * @param  object|string  $haystack
81
     *
82
     * @return bool
83
     */
84 90
    public function exists($haystack): bool
85
    {
86 90
        if (IsHelper::object($haystack)) {
87 38
            return true;
88
        }
89
90 90
        return IsHelper::string($haystack) ? class_exists($haystack) || interface_exists($haystack) : false;
0 ignored issues
show
Bug introduced by
It seems like $haystack can also be of type object; however, parameter $class of class_exists() 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

90
        return IsHelper::string($haystack) ? class_exists(/** @scrutinizer ignore-type */ $haystack) || interface_exists($haystack) : false;
Loading history...
Bug introduced by
It seems like $haystack can also be of type object; however, parameter $interface of interface_exists() 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

90
        return IsHelper::string($haystack) ? class_exists($haystack) || interface_exists(/** @scrutinizer ignore-type */ $haystack) : false;
Loading history...
91
    }
92
93
    /**
94
     * Creates a ReflectionClass object.
95
     *
96
     * @param  object|ReflectionClass|string  $class
97
     *
98
     * @return \ReflectionClass
99
     */
100 36
    protected function resolve($class): ReflectionClass
101
    {
102 36
        return ReflectionHelper::resolve($class);
103
    }
104
}
105