Passed
Pull Request — main (#92)
by Andrey
15:13
created

Instance::callOf()   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 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
c 1
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
    public function of($haystack, $needles): bool
21
    {
22
        if (! $this->exists($haystack)) {
23
            return false;
24 90
        }
25
26 90
        $reflection = $this->resolve($haystack);
27 74
        $classname  = $this->classname($haystack);
28
29
        foreach (ArrHelper::wrap($needles) as $needle) {
30 38
            if (! $this->exists($needle)) {
31 38
                continue;
32
            }
33 38
34 38
            if (
35 4
                $haystack instanceof $needle ||
36
                $classname === $this->classname($needle) ||
37
                $reflection->isSubclassOf($needle) ||
38
                ($reflection->isInterface() && $reflection->implementsInterface($needle))
39 38
            ) {
40 20
                return true;
41 20
            }
42 38
        }
43
44 26
        return false;
45
    }
46
47
    /**
48 20
     * Extract the trailing name component from a file path.
49
     *
50
     * @param  object|string  $class
51
     *
52
     * @return string|null
53
     */
54
    public function basename($class): ?string
55
    {
56
        $class = $this->classname($class);
57
58 2
        return basename(str_replace('\\', '/', $class)) ?: null;
59
    }
60 2
61
    /**
62 2
     * Gets the class name of the object.
63
     *
64
     * @param  object|string|null  $class
65
     *
66
     * @return string|null
67
     */
68
    public function classname($class = null): ?string
69
    {
70
        if (IsHelper::object($class)) {
71
            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 44
        }
73
74 44
        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 42
    }
76
77
    /**
78 26
     * Checks if the object exists.
79
     *
80
     * @param  object|string  $haystack
81
     *
82
     * @return bool
83
     */
84
    public function exists($haystack): bool
85
    {
86
        if (IsHelper::object($haystack)) {
87
            return true;
88 92
        }
89
90 92
        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 40
    }
92
93
    /**
94 92
     * Creates a ReflectionClass object.
95
     *
96
     * @param  object|ReflectionClass|string  $class
97
     *
98
     * @return \ReflectionClass
99
     */
100
    protected function resolve($class): ReflectionClass
101
    {
102
        return ReflectionHelper::resolve($class);
103
    }
104
}
105