Completed
Push — master ( 8a9ad2...75b1ab )
by Richard
02:40
created

LazyAssertion   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
c 2
b 0
f 0
cbo 1
dl 0
loc 40
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A that() 0 7 1
A __call() 0 15 3
A verifyNow() 0 6 2
1
<?php
2
/**
3
 * Assert
4
 *
5
 * LICENSE
6
 *
7
 * This source file is subject to the MIT license that is bundled
8
 * with this package in the file LICENSE.txt.
9
 * If you did not receive a copy of the license and are unable to
10
 * obtain it through the world-wide-web, please send an email
11
 * to [email protected] so I can send you a copy immediately.
12
 */
13
14
namespace Assert;
15
16
/**
17
 * Chaining builder for lazy assertions
18
 *
19
 * @author Benjamin Eberlei <[email protected]>
20
 *
21
 * METHODSTART
22
 * @method \Assert\LazyAssertion eq($value2, $message = null, $propertyPath = null)
23
 * @method \Assert\LazyAssertion same($value2, $message = null, $propertyPath = null)
24
 * @method \Assert\LazyAssertion notEq($value2, $message = null, $propertyPath = null)
25
 * @method \Assert\LazyAssertion notSame($value2, $message = null, $propertyPath = null)
26
 * @method \Assert\LazyAssertion integer($message = null, $propertyPath = null)
27
 * @method \Assert\LazyAssertion float($message = null, $propertyPath = null)
28
 * @method \Assert\LazyAssertion digit($message = null, $propertyPath = null)
29
 * @method \Assert\LazyAssertion integerish($message = null, $propertyPath = null)
30
 * @method \Assert\LazyAssertion boolean($message = null, $propertyPath = null)
31
 * @method \Assert\LazyAssertion scalar($message = null, $propertyPath = null)
32
 * @method \Assert\LazyAssertion notEmpty($message = null, $propertyPath = null)
33
 * @method \Assert\LazyAssertion noContent($message = null, $propertyPath = null)
34
 * @method \Assert\LazyAssertion notNull($message = null, $propertyPath = null)
35
 * @method \Assert\LazyAssertion string($message = null, $propertyPath = null)
36
 * @method \Assert\LazyAssertion regex($pattern, $message = null, $propertyPath = null)
37
 * @method \Assert\LazyAssertion length($length, $message = null, $propertyPath = null, $encoding = "utf8")
38
 * @method \Assert\LazyAssertion minLength($minLength, $message = null, $propertyPath = null, $encoding = "utf8")
39
 * @method \Assert\LazyAssertion maxLength($maxLength, $message = null, $propertyPath = null, $encoding = "utf8")
40
 * @method \Assert\LazyAssertion betweenLength($minLength, $maxLength, $message = null, $propertyPath = null, $encoding = "utf8")
41
 * @method \Assert\LazyAssertion startsWith($needle, $message = null, $propertyPath = null, $encoding = "utf8")
42
 * @method \Assert\LazyAssertion endsWith($needle, $message = null, $propertyPath = null, $encoding = "utf8")
43
 * @method \Assert\LazyAssertion contains($needle, $message = null, $propertyPath = null, $encoding = "utf8")
44
 * @method \Assert\LazyAssertion choice($choices, $message = null, $propertyPath = null)
45
 * @method \Assert\LazyAssertion inArray($choices, $message = null, $propertyPath = null)
46
 * @method \Assert\LazyAssertion numeric($message = null, $propertyPath = null)
47
 * @method \Assert\LazyAssertion isArray($message = null, $propertyPath = null)
48
 * @method \Assert\LazyAssertion isTraversable($message = null, $propertyPath = null)
49
 * @method \Assert\LazyAssertion isArrayAccessible($message = null, $propertyPath = null)
50
 * @method \Assert\LazyAssertion keyExists($key, $message = null, $propertyPath = null)
51
 * @method \Assert\LazyAssertion keyIsset($key, $message = null, $propertyPath = null)
52
 * @method \Assert\LazyAssertion notEmptyKey($key, $message = null, $propertyPath = null)
53
 * @method \Assert\LazyAssertion notBlank($message = null, $propertyPath = null)
54
 * @method \Assert\LazyAssertion isInstanceOf($className, $message = null, $propertyPath = null)
55
 * @method \Assert\LazyAssertion notIsInstanceOf($className, $message = null, $propertyPath = null)
56
 * @method \Assert\LazyAssertion subclassOf($className, $message = null, $propertyPath = null)
57
 * @method \Assert\LazyAssertion range($minValue, $maxValue, $message = null, $propertyPath = null)
58
 * @method \Assert\LazyAssertion min($minValue, $message = null, $propertyPath = null)
59
 * @method \Assert\LazyAssertion max($maxValue, $message = null, $propertyPath = null)
60
 * @method \Assert\LazyAssertion file($message = null, $propertyPath = null)
61
 * @method \Assert\LazyAssertion directory($message = null, $propertyPath = null)
62
 * @method \Assert\LazyAssertion readable($message = null, $propertyPath = null)
63
 * @method \Assert\LazyAssertion writeable($message = null, $propertyPath = null)
64
 * @method \Assert\LazyAssertion email($message = null, $propertyPath = null)
65
 * @method \Assert\LazyAssertion url($message = null, $propertyPath = null)
66
 * @method \Assert\LazyAssertion alnum($message = null, $propertyPath = null)
67
 * @method \Assert\LazyAssertion true($message = null, $propertyPath = null)
68
 * @method \Assert\LazyAssertion false($message = null, $propertyPath = null)
69
 * @method \Assert\LazyAssertion classExists($message = null, $propertyPath = null)
70
 * @method \Assert\LazyAssertion implementsInterface($interfaceName, $message = null, $propertyPath = null)
71
 * @method \Assert\LazyAssertion isJsonString($message = null, $propertyPath = null)
72
 * @method \Assert\LazyAssertion uuid($message = null, $propertyPath = null)
73
 * @method \Assert\LazyAssertion count($count, $message = null, $propertyPath = null)
74
 * @method \Assert\LazyAssertion choicesNotEmpty($choices, $message = null, $propertyPath = null)
75
 * @method \Assert\LazyAssertion methodExists($object, $message = null, $propertyPath = null)
76
 * @method \Assert\LazyAssertion isObject($message = null, $propertyPath = null)
77
 * @method \Assert\LazyAssertion lessThan($limit, $message = null, $propertyPath = null)
78
 * @method \Assert\LazyAssertion lessOrEqualThan($limit, $message = null, $propertyPath = null)
79
 * @method \Assert\LazyAssertion greaterThan($limit, $message = null, $propertyPath = null)
80
 * @method \Assert\LazyAssertion greaterOrEqualThan($limit, $message = null, $propertyPath = null)
81
 * @method \Assert\LazyAssertion date($format, $message = null, $propertyPath = null)
82
 * @method \Assert\LazyAssertion all()
83
 * @method \Assert\LazyAssertion nullOr()
84
 * METHODEND
85
 */
86
class LazyAssertion
87
{
88
    private $currentChainFailed = false;
89
    private $currentChain;
90
    private $errors = array();
91
92
    public function that($value, $propertyPath, $defaultMessage = null)
93
    {
94
        $this->currentChainFailed = false;
95
        $this->currentChain = \Assert\that($value, $defaultMessage, $propertyPath);
96
97
        return $this;
98
    }
99
100
    public function __call($method, $args)
101
    {
102
        if ($this->currentChainFailed === true) {
103
            return $this;
104
        }
105
106
        try {
107
            call_user_func_array(array($this->currentChain, $method), $args);
108
        } catch (AssertionFailedException $e) {
109
            $this->errors[] = $e;
110
            $this->currentChainFailed = true;
111
        }
112
113
        return $this;
114
    }
115
116
    /**
117
     * @throws \Assert\LazyAssertionException
118
     */
119
    public function verifyNow()
120
    {
121
        if ($this->errors) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->errors of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
122
            throw LazyAssertionException::fromErrors($this->errors);
123
        }
124
    }
125
}
126