Completed
Push — master ( c61969...df1bf5 )
by Marcus
02:35
created

StringVal   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 0
dl 0
loc 24
c 0
b 0
f 0
ccs 5
cts 5
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 10 2
1
<?php
2
3
namespace Mbright\Validation\Rule\Validate;
4
5
class StringVal
6
{
7
    /**
8
     * Validates that the value can be represented as a string.
9
     *
10
     * Essentially, this means any scalar value is valid (no arrays, objects, resources, etc). If the $subject is an
11
     * object and has a __tostring() method, this will validate successfully.
12
     *
13
     * @param object $subject The subject to be filtered.
14
     * @param string $field The subject field name.
15
     *
16
     * @return bool True if valid, false if not.
17
     */
18 24
    public function __invoke($subject, string $field): bool
19
    {
20 24
        $value = $subject->$field;
21
22 24
        if (is_object($value)) {
23 3
            return method_exists($value, '__toString');
24
        }
25
26 21
        return is_scalar($value);
27
    }
28
}
29