Conditions | 7 |
Paths | 4 |
Total Lines | 28 |
Code Lines | 17 |
Lines | 0 |
Ratio | 0 % |
Tests | 14 |
CRAP Score | 7 |
Changes | 0 |
1 | <?php |
||
17 | 42 | public function __invoke($subject, string $field): bool |
|
18 | { |
||
19 | 42 | $value = $subject->$field; |
|
20 | 42 | if (!is_scalar($value)) { |
|
21 | 3 | return false; |
|
22 | } |
||
23 | |||
24 | // first, make sure there are no invalid chars, list from ext/filter |
||
25 | $other = "$-_.+" // safe |
||
26 | . "!*'()," // extra |
||
27 | . "{}|\\^~[]`" // national |
||
28 | . "<>#%\"" // punctuation |
||
29 | 39 | . ";/?:@&="; // reserved |
|
30 | |||
31 | 39 | $valid = 'a-zA-Z0-9' . preg_quote($other, '/'); |
|
32 | 39 | $clean = preg_replace("/[^$valid]/", '', $value); |
|
33 | 39 | if ($value != $clean) { |
|
34 | 12 | return false; |
|
35 | } |
||
36 | |||
37 | // now make sure it parses as a URL with scheme and host |
||
38 | 27 | $result = @parse_url($value); |
|
39 | 27 | if (empty($result['scheme']) || trim($result['scheme']) == '' || |
|
40 | 27 | empty($result['host']) || trim($result['host']) == '') { |
|
41 | 9 | return false; |
|
42 | } |
||
43 | |||
44 | 18 | return true; |
|
45 | } |
||
47 |