Passed
Pull Request — develop (#27)
by Glynn
03:53 queued 27s
created

implementsInterface()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 1
nop 1
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Object functions.
5
 *
6
 * This file is part of PinkCrab Function Constructors.
7
 *
8
 * PinkCrab Function Constructors is free software: you can redistribute it and/or modify it under the terms of the
9
 * GNU General Public License as published by the Free Software Foundation, either version 2
10
 * of the License, or (at your option) any later version.
11
 *
12
 * PinkCrab Function Constructors is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
13
 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
 * See the GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License along with PinkCrab Function Constructors.
17
 * If not, see <https://www.gnu.org/licenses/>.
18
 *
19
 * @author Glynn Quelch <[email protected]>
20
 * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
21
 * @package PinkCrab\FunctionConstructors
22
 * @since 0.0.1
23
 *
24
 * @template Number of int|float
25
 * @phpstan-template Number of int|float
26
 * @psalm-template Number of int|float
27
 * @template Class of object|class-string
28
 * @phpstan-template Class of object|class-string
29
 * @psalm-template Class of object|class-string
30
 */
31
32
declare(strict_types=1);
33
34
namespace PinkCrab\FunctionConstructors\Objects;
35
36
use Closure;
37
use InvalidArgumentException;
38
use PinkCrab\FunctionConstructors\Comparisons as C;
0 ignored issues
show
Bug introduced by
The type PinkCrab\FunctionConstructors\Comparisons was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
39
40
/**
41
 * Returns a function for checking if the passed class or string-class-name matches
42
 * that of the predefined class or string-class-name.
43
 *
44
 * @template Class of object|class-string
45
 * @param Class $class
0 ignored issues
show
Bug introduced by
The type PinkCrab\FunctionConstructors\Objects\Class was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
46
 * @return Closure(Class): bool
47
 */
48
function isInstanceOf($class): Closure
49
{
50
51
    // If we have an object, get full name.
52
    if (is_object($class)) {
53
        $class = get_class($class);
54
    }
55
    if (! class_exists($class)) {
56
        throw new InvalidArgumentException(__FUNCTION__ . 'only accepts a Class or Class Name');
57
    }
58
59
    /**
60
     * @param Class $value
61
     * @return bool
62
     */
63
    return function ($value) use ($class): bool {
64
        if (is_object($value)) {
65
            $value = get_class($value);
66
        }
67
        if (! class_exists($value)) {
68
            throw new InvalidArgumentException(__FUNCTION__ . 'only accepts a Class or Class Name');
69
        }
70
        return is_a($value, $class, true);
71
    };
72
}
73
74
/**
75
 * Returns a function to check if a passed class or string-class-name implements a predefined interface.
76
 *
77
 * @template Class of object|class-string
78
 * @param Class $interface The interface to check against.
79
 * @return Closure(Class): bool
80
 */
81
function implementsInterface($interface): Closure
82
{
83
    /**
84
     * @param Class $class
85
     * @return bool
86
     */
87
    return function ($class) use ($interface): bool {
88
        return in_array(
89
            $class,
90
            class_implements($interface, false) ?: array(),
91
            true
92
        );
93
    };
94
}
95