• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

codeigniter4 / CodeIgniter4 / 12673986434

08 Jan 2025 03:42PM UTC coverage: 84.455% (+0.001%) from 84.454%
12673986434

Pull #9385

github

web-flow
Merge 06e47f0ee into e475fd8fa
Pull Request #9385: refactor: Fix phpstan expr.resultUnused

20699 of 24509 relevant lines covered (84.45%)

190.57 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

89.02
/system/Validation/StrictRules/Rules.php
1
<?php
2

3
declare(strict_types=1);
4

5
/**
6
 * This file is part of CodeIgniter 4 framework.
7
 *
8
 * (c) CodeIgniter Foundation <admin@codeigniter.com>
9
 *
10
 * For the full copyright and license information, please view
11
 * the LICENSE file that was distributed with this source code.
12
 */
13

14
namespace CodeIgniter\Validation\StrictRules;
15

16
use CodeIgniter\Helpers\Array\ArrayHelper;
17
use CodeIgniter\Validation\Rules as NonStrictRules;
18

19
/**
20
 * Validation Rules.
21
 *
22
 * @see \CodeIgniter\Validation\StrictRules\RulesTest
23
 */
24
class Rules
25
{
26
    private readonly NonStrictRules $nonStrictRules;
27

28
    public function __construct()
29
    {
30
        $this->nonStrictRules = new NonStrictRules();
963✔
31
    }
32

33
    /**
34
     * The value does not match another field in $data.
35
     *
36
     * @param array|bool|float|int|object|string|null $str
37
     * @param array                                   $data Other field/value pairs
38
     */
39
    public function differs(
40
        $str,
41
        string $otherField,
42
        array $data,
43
        ?string $error = null,
44
        ?string $field = null
45
    ): bool {
46
        if (str_contains($otherField, '.')) {
13✔
47
            return $str !== dot_array_search($otherField, $data);
2✔
48
        }
49

50
        if (! array_key_exists($otherField, $data)) {
11✔
51
            return false;
2✔
52
        }
53

54
        if (str_contains($field, '.')) {
9✔
55
            if (! ArrayHelper::dotKeyExists($field, $data)) {
2✔
56
                return false;
×
57
            }
58
        } elseif (! array_key_exists($field, $data)) {
7✔
59
            return false;
1✔
60
        }
61

62
        return $str !== ($data[$otherField] ?? null);
8✔
63
    }
64

65
    /**
66
     * Equals the static value provided.
67
     *
68
     * @param array|bool|float|int|object|string|null $str
69
     */
70
    public function equals($str, string $val): bool
71
    {
72
        return $this->nonStrictRules->equals($str, $val);
5✔
73
    }
74

75
    /**
76
     * Returns true if $str is $val characters long.
77
     * $val = "5" (one) | "5,8,12" (multiple values)
78
     *
79
     * @param array|bool|float|int|object|string|null $str
80
     */
81
    public function exact_length($str, string $val): bool
82
    {
83
        if (is_int($str) || is_float($str)) {
9✔
84
            $str = (string) $str;
3✔
85
        }
86

87
        if (! is_string($str)) {
9✔
88
            return false;
2✔
89
        }
90

91
        return $this->nonStrictRules->exact_length($str, $val);
8✔
92
    }
93

94
    /**
95
     * Greater than
96
     *
97
     * @param array|bool|float|int|object|string|null $str expects int|string
98
     */
99
    public function greater_than($str, string $min): bool
100
    {
101
        if (is_int($str) || is_float($str)) {
17✔
102
            $str = (string) $str;
7✔
103
        }
104

105
        if (! is_string($str)) {
17✔
106
            return false;
2✔
107
        }
108

109
        return $this->nonStrictRules->greater_than($str, $min);
15✔
110
    }
111

112
    /**
113
     * Equal to or Greater than
114
     *
115
     * @param array|bool|float|int|object|string|null $str expects int|string
116
     */
117
    public function greater_than_equal_to($str, string $min): bool
118
    {
119
        if (is_int($str) || is_float($str)) {
17✔
120
            $str = (string) $str;
6✔
121
        }
122

123
        if (! is_string($str)) {
17✔
124
            return false;
3✔
125
        }
126

127
        return $this->nonStrictRules->greater_than_equal_to($str, $min);
14✔
128
    }
129

130
    /**
131
     * Checks the database to see if the given value exist.
132
     * Can ignore records by field/value to filter (currently
133
     * accept only one filter).
134
     *
135
     * Example:
136
     *    is_not_unique[dbGroup.table.field,where_field,where_value]
137
     *    is_not_unique[table.field,where_field,where_value]
138
     *    is_not_unique[menu.id,active,1]
139
     *
140
     * @param array|bool|float|int|object|string|null $str
141
     */
142
    public function is_not_unique($str, string $field, array $data): bool
143
    {
144
        if (is_object($str) || is_array($str)) {
7✔
145
            return false;
×
146
        }
147

148
        return $this->nonStrictRules->is_not_unique($str, $field, $data);
7✔
149
    }
150

151
    /**
152
     * Value should be within an array of values
153
     *
154
     * @param array|bool|float|int|object|string|null $value
155
     */
156
    public function in_list($value, string $list): bool
157
    {
158
        if (is_int($value) || is_float($value)) {
29✔
159
            $value = (string) $value;
9✔
160
        }
161

162
        if (! is_string($value)) {
29✔
163
            return false;
1✔
164
        }
165

166
        return $this->nonStrictRules->in_list($value, $list);
28✔
167
    }
168

169
    /**
170
     * Checks the database to see if the given value is unique. Can
171
     * ignore a single record by field/value to make it useful during
172
     * record updates.
173
     *
174
     * Example:
175
     *    is_unique[dbGroup.table.field,ignore_field,ignore_value]
176
     *    is_unique[table.field,ignore_field,ignore_value]
177
     *    is_unique[users.email,id,5]
178
     *
179
     * @param array|bool|float|int|object|string|null $str
180
     */
181
    public function is_unique($str, string $field, array $data): bool
182
    {
183
        if (is_object($str) || is_array($str)) {
10✔
184
            return false;
×
185
        }
186

187
        return $this->nonStrictRules->is_unique($str, $field, $data);
10✔
188
    }
189

190
    /**
191
     * Less than
192
     *
193
     * @param array|bool|float|int|object|string|null $str expects int|string
194
     */
195
    public function less_than($str, string $max): bool
196
    {
197
        if (is_int($str) || is_float($str)) {
19✔
198
            $str = (string) $str;
8✔
199
        }
200

201
        if (! is_string($str)) {
19✔
202
            return false;
2✔
203
        }
204

205
        return $this->nonStrictRules->less_than($str, $max);
17✔
206
    }
207

208
    /**
209
     * Equal to or Less than
210
     *
211
     * @param array|bool|float|int|object|string|null $str expects int|string
212
     */
213
    public function less_than_equal_to($str, string $max): bool
214
    {
215
        if (is_int($str) || is_float($str)) {
17✔
216
            $str = (string) $str;
6✔
217
        }
218

219
        if (! is_string($str)) {
17✔
220
            return false;
3✔
221
        }
222

223
        return $this->nonStrictRules->less_than_equal_to($str, $max);
14✔
224
    }
225

226
    /**
227
     * Matches the value of another field in $data.
228
     *
229
     * @param array|bool|float|int|object|string|null $str
230
     * @param array                                   $data Other field/value pairs
231
     */
232
    public function matches(
233
        $str,
234
        string $otherField,
235
        array $data,
236
        ?string $error = null,
237
        ?string $field = null
238
    ): bool {
239
        if (str_contains($otherField, '.')) {
13✔
240
            return $str === dot_array_search($otherField, $data);
2✔
241
        }
242

243
        if (! array_key_exists($otherField, $data)) {
11✔
244
            return false;
2✔
245
        }
246

247
        if (str_contains($field, '.')) {
9✔
248
            if (! ArrayHelper::dotKeyExists($field, $data)) {
2✔
249
                return false;
×
250
            }
251
        } elseif (! array_key_exists($field, $data)) {
7✔
252
            return false;
1✔
253
        }
254

255
        return $str === ($data[$otherField] ?? null);
8✔
256
    }
257

258
    /**
259
     * Returns true if $str is $val or fewer characters in length.
260
     *
261
     * @param array|bool|float|int|object|string|null $str
262
     */
263
    public function max_length($str, string $val): bool
264
    {
265
        if (is_int($str) || is_float($str) || null === $str) {
10✔
266
            $str = (string) $str;
1✔
267
        }
268

269
        if (! is_string($str)) {
10✔
270
            return false;
×
271
        }
272

273
        return $this->nonStrictRules->max_length($str, $val);
10✔
274
    }
275

276
    /**
277
     * Returns true if $str is at least $val length.
278
     *
279
     * @param array|bool|float|int|object|string|null $str
280
     */
281
    public function min_length($str, string $val): bool
282
    {
283
        if (is_int($str) || is_float($str)) {
56✔
284
            $str = (string) $str;
×
285
        }
286

287
        if (! is_string($str)) {
56✔
288
            return false;
1✔
289
        }
290

291
        return $this->nonStrictRules->min_length($str, $val);
55✔
292
    }
293

294
    /**
295
     * Does not equal the static value provided.
296
     *
297
     * @param array|bool|float|int|object|string|null $str
298
     */
299
    public function not_equals($str, string $val): bool
300
    {
301
        return $this->nonStrictRules->not_equals($str, $val);
×
302
    }
303

304
    /**
305
     * Value should not be within an array of values.
306
     *
307
     * @param array|bool|float|int|object|string|null $value
308
     */
309
    public function not_in_list($value, string $list): bool
310
    {
311
        if (null === $value) {
9✔
312
            return true;
1✔
313
        }
314

315
        if (is_int($value) || is_float($value)) {
8✔
316
            $value = (string) $value;
×
317
        }
318

319
        if (! is_string($value)) {
8✔
320
            return false;
×
321
        }
322

323
        return $this->nonStrictRules->not_in_list($value, $list);
8✔
324
    }
325

326
    /**
327
     * @param array|bool|float|int|object|string|null $str
328
     */
329
    public function required($str = null): bool
330
    {
331
        return $this->nonStrictRules->required($str);
113✔
332
    }
333

334
    /**
335
     * The field is required when any of the other required fields are present
336
     * in the data.
337
     *
338
     * Example (field is required when the password field is present):
339
     *
340
     *     required_with[password]
341
     *
342
     * @param array|bool|float|int|object|string|null $str
343
     * @param string|null                             $fields List of fields that we should check if present
344
     * @param array                                   $data   Complete list of fields from the form
345
     */
346
    public function required_with($str = null, ?string $fields = null, array $data = []): bool
347
    {
348
        return $this->nonStrictRules->required_with($str, $fields, $data);
33✔
349
    }
350

351
    /**
352
     * The field is required when all the other fields are present
353
     * in the data but not required.
354
     *
355
     * Example (field is required when the id or email field is missing):
356
     *
357
     *     required_without[id,email]
358
     *
359
     * @param array|bool|float|int|object|string|null $str
360
     * @param string|null                             $otherFields The param fields of required_without[].
361
     * @param string|null                             $field       This rule param fields aren't present, this field is required.
362
     */
363
    public function required_without(
364
        $str = null,
365
        ?string $otherFields = null,
366
        array $data = [],
367
        ?string $error = null,
368
        ?string $field = null
369
    ): bool {
370
        return $this->nonStrictRules->required_without($str, $otherFields, $data, $error, $field);
24✔
371
    }
372

373
    /**
374
     * The field exists in $data.
375
     *
376
     * @param array|bool|float|int|object|string|null $value The field value.
377
     * @param string|null                             $param The rule's parameter.
378
     * @param array                                   $data  The data to be validated.
379
     * @param string|null                             $field The field name.
380
     */
381
    public function field_exists(
382
        $value = null,
383
        ?string $param = null,
384
        array $data = [],
385
        ?string $error = null,
386
        ?string $field = null
387
    ): bool {
388
        if (str_contains($field, '.')) {
10✔
389
            return ArrayHelper::dotKeyExists($field, $data);
5✔
390
        }
391

392
        return array_key_exists($field, $data);
5✔
393
    }
394
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc