Completed
Push — master ( 528f7d...f4b721 )
by Kacper
04:19
created

Filter.php ➔ predicate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 1
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * XMPP Library
4
 *
5
 * Copyright (C) 2016, Some right reserved.
6
 *
7
 * @author Kacper "Kadet" Donat <[email protected]>
8
 *
9
 * Contact with author:
10
 * Xmpp: [email protected]
11
 * E-mail: [email protected]
12
 *
13
 * From Kadet with love.
14
 */
15
16
namespace Kadet\Xmpp\Utils\filter;
17
18
use Kadet\Xmpp\Exception\InvalidArgumentException;
19
use Kadet\Xmpp\Xml\XmlElement;
20
use Kadet\Xmpp\Utils\helper;
21
22
function xmlns($uri)
23
{
24
    return function (XmlElement $element) use ($uri) {
25
        return $element->namespace === $uri;
26
    };
27
}
28
29
function tag($name)
30
{
31
    return function (XmlElement $element) use ($name) {
32
        return $element->localName === $name;
33
    };
34
}
35
36
function ofType($class)
37
{
38
    return function ($object) use ($class) {
39
        return $object instanceof $class;
40
    };
41
}
42
43 View Code Duplication
function all(callable ...$functions)
1 ignored issue
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
44
{
45
    return function (...$args) use ($functions) {
46
        foreach ($functions as $function) {
47
            if (!$function(...$args)) {
48
                return false;
49
            }
50
        }
51
52
        return true;
53
    };
54
}
55
56 View Code Duplication
function any(callable ...$functions)
1 ignored issue
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
57
{
58
    return function (...$args) use ($functions) {
59
        foreach ($functions as $function) {
60
            if ($function(...$args)) {
61
                return true;
62
            }
63
        }
64
65
        return false;
66
    };
67
}
68
69
function predicate($predicate) : callable
70
{
71
    if (is_callable($predicate)) {
72
        return $predicate;
73
    } elseif (class_exists($predicate)) {
74
        return ofType($predicate);
75
    } else {
76
        throw new InvalidArgumentException('$condition must be either class-name or callable, ' . helper\typeof($predicate) . ' given.');
77
    }
78
}
79