Completed
Push — master ( 6bbd38...55e0d1 )
by Erin
10s
created

src/Core/Definitions.php (25 issues)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * Main definition for Assertion. Define core methods and properties here.
4
 */
5
use Peridot\Leo\Assertion;
6
use Peridot\Leo\Matcher\EmptyMatcher;
7
use Peridot\Leo\Matcher\EqualMatcher;
8
use Peridot\Leo\Matcher\ExceptionMatcher;
9
use Peridot\Leo\Matcher\InclusionMatcher;
10
use Peridot\Leo\Matcher\InstanceofMatcher;
11
use Peridot\Leo\Matcher\KeysMatcher;
12
use Peridot\Leo\Matcher\LengthMatcher;
13
use Peridot\Leo\Matcher\NullMatcher;
14
use Peridot\Leo\Matcher\PatternMatcher;
15
use Peridot\Leo\Matcher\PredicateMatcher;
16
use Peridot\Leo\Matcher\PropertyMatcher;
17
use Peridot\Leo\Matcher\RangeMatcher;
18
use Peridot\Leo\Matcher\SameMatcher;
19
use Peridot\Leo\Matcher\SubStringMatcher;
20
use Peridot\Leo\Matcher\TrueMatcher;
21
use Peridot\Leo\Matcher\TruthyMatcher;
22
use Peridot\Leo\Matcher\TypeMatcher;
23
24
return function (Assertion $assertion) {
25
    /**
26
     * Default language chains.
27
     */
28
    $chains = [
29
        'to', 'be', 'been',
30
        'is', 'and', 'has',
31
        'have', 'with', 'that',
32
        'at', 'of', 'same',
33
        'an', 'a'
34
    ];
35
36
    foreach ($chains as $chain) {
37
        $assertion->addProperty($chain, function () {
38
            return $this;
0 ignored issues
show
The variable $this does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
39
        }, true);
40
    }
41
42
    $assertion->addProperty('not', function () {
43
        return $this->flag('not', true);
0 ignored issues
show
The variable $this does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
44
    });
45
46
    $assertion->addMethod('with', function () {
47
        return $this->flag('arguments', func_get_args());
0 ignored issues
show
The variable $this does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
48
    });
49
50
    $assertion->addProperty('loosely', function () {
51
        return $this->flag('loosely', true);
0 ignored issues
show
The variable $this does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
52
    });
53
54
    $assertion->addMethod('equal', function ($expected, $message = "") {
55
        $this->flag('message', $message);
0 ignored issues
show
The variable $this does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
56
        if ($this->flag('loosely')) {
57
            return new EqualMatcher($expected);
58
        }
59
        return new SameMatcher($expected);
60
    });
61
62
    $assertion->addMethod('throw', function ($exceptionType, $exceptionMessage = '', $message = "") {
63
        $this->flag('message', $message);
0 ignored issues
show
The variable $this does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
64
        $matcher = new ExceptionMatcher($exceptionType);
65
        $matcher->setExpectedMessage($exceptionMessage);
66
        $matcher->setArguments($this->flag('arguments') ?: []);
67
        return $matcher;
68
    });
69
70
    $type = function ($expected, $message = "") {
71
        $this->flag('message', $message);
0 ignored issues
show
The variable $this does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
72
        return new TypeMatcher($expected);
73
    };
74
75
    $assertion
76
        ->addMethod('a', $type)
77
        ->addMethod('an', $type);
78
79
    $include = function ($expected, $message = "") {
80
        $this->flag('message', $message);
0 ignored issues
show
The variable $this does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
81
        return new InclusionMatcher($expected);
82
    };
83
84
    $assertion
85
        ->addMethod('include', $include)
86
        ->addMethod('contain', $include);
87
88
    $contain = function () {
89
        return $this->flag('contain', true);
0 ignored issues
show
The variable $this does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
90
    };
91
92
    $assertion
93
        ->addProperty('contain', $contain)
94
        ->addProperty('include', $contain);
95
96
    $truthy = function ($message = "") {
97
        $this->flag('message', $message);
0 ignored issues
show
The variable $this does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
98
        return new TruthyMatcher();
99
    };
100
101
    $assertion
102
        ->addMethod('ok', $truthy)
103
        ->addProperty('ok', $truthy);
104
105
    $true = function ($message = "") {
106
        $this->flag('message', $message);
0 ignored issues
show
The variable $this does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
107
        return new TrueMatcher();
108
    };
109
110
    $assertion
111
        ->addMethod('true', $true)
112
        ->addProperty('true', $true);
113
114
    $false = function ($message = "") {
115
        $this->flag('message', $message);
0 ignored issues
show
The variable $this does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
116
        $matcher = new TrueMatcher();
117
        return $matcher->invert();
118
    };
119
120
    $assertion
121
        ->addMethod('false', $false)
122
        ->addProperty('false', $false);
123
124
    $null = function ($message = "") {
125
        $this->flag('message', $message);
0 ignored issues
show
The variable $this does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
126
        return new NullMatcher();
127
    };
128
129
    $assertion
130
        ->addMethod('null', $null)
131
        ->addProperty('null', $null);
132
133
    $empty = function ($message = "") {
134
        $this->flag('message', $message);
0 ignored issues
show
The variable $this does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
135
        return new EmptyMatcher();
136
    };
137
138
    $assertion
139
        ->addMethod('empty', $empty)
140
        ->addProperty('empty', $empty);
141
142
    $assertion->addProperty('length', function () {
143
        return $this->flag('length', $this->getActual());
0 ignored issues
show
The variable $this does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
144
    });
145
146
    /**
147
     * Define a helper for creating countable matchers. A countable
148
     * matcher is a matcher that matches against a single numeric
149
     * value, or a value that can be reduced to a single numeric value
150
     * via the count() function.
151
     *
152
     * @param $className
153
     * @return callable
154
     */
155
    $countable = function ($className) {
156
        return function ($expected, $message = "") use ($className) {
157
            $this->flag('message', $message);
0 ignored issues
show
The variable $this does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
158
            $class = "Peridot\\Leo\\Matcher\\$className";
159
            $matcher = new $class($expected);
160
            if ($countable = $this->flag('length')) {
161
                $matcher->setCountable($countable);
162
            }
163
            return $matcher;
164
        };
165
    };
166
167
    $assertion->addMethod('above', $countable('GreaterThanMatcher'));
168
    $assertion->addMethod('least', $countable('GreaterThanOrEqualMatcher'));
169
    $assertion->addMethod('below', $countable('LessThanMatcher'));
170
    $assertion->addMethod('most', $countable('LessThanOrEqualMatcher'));
171
172
    $assertion->addMethod('within', function ($lower, $upper, $message = "") {
173
        $this->flag('message', $message);
0 ignored issues
show
The variable $this does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
174
        $matcher = new RangeMatcher($lower, $upper);
175
        if ($countable = $this->flag('length')) {
176
            $matcher->setCountable($countable);
177
        }
178
        return $matcher;
179
    });
180
181
    $assertion->addMethod('instanceof', function ($expected, $message = "") {
182
        $this->flag('message', $message);
0 ignored issues
show
The variable $this does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
183
        return new InstanceofMatcher($expected);
184
    });
185
186
    $assertion->addProperty('deep', function () {
187
        return $this->flag('deep', true);
0 ignored issues
show
The variable $this does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
188
    });
189
190
    $assertion->addMethod('property', function ($name, $value = "", $message = "") {
191
        $this->flag('message', $message);
0 ignored issues
show
The variable $this does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
192
        $matcher = new PropertyMatcher($name, $value);
193
        $matcher->setAssertion($this);
194
        return $matcher->setIsDeep($this->flag('deep'));
195
    });
196
197
    $assertion->addMethod('length', function ($expected, $message = "") {
198
        $this->flag('message', $message);
0 ignored issues
show
The variable $this does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
199
        return new LengthMatcher($expected);
200
    });
201
202
    $assertion->addMethod('match', function ($pattern, $message = "") {
203
        $this->flag('message', $message);
0 ignored issues
show
The variable $this does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
204
        return new PatternMatcher($pattern);
205
    });
206
207
    $assertion->addMethod('string', function ($expected, $message = "") {
208
        $this->flag('message', $message);
0 ignored issues
show
The variable $this does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
209
        return new SubStringMatcher($expected);
210
    });
211
212
    $assertion->addMethod('keys', function (array $keys, $message = "") {
213
        $this->flag('message', $message);
0 ignored issues
show
The variable $this does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
214
        return new KeysMatcher($keys);
215
    });
216
217
    $assertion->addMethod('satisfy', function (callable $predicate, $message = "") {
218
        $this->flag('message', $message);
0 ignored issues
show
The variable $this does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
219
        return new PredicateMatcher($predicate);
220
    });
221
};
222