StringExpectations::match()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Dgame\Expectation;
4
5
/**
6
 * Class StringExpectations
7
 * @package Dgame\Expectation
8
 */
9
final class StringExpectations extends ScalarExpectations
10
{
11
    /**
12
     * StringExpectations constructor.
13
     *
14
     * @param mixed $value
15
     */
16 3
    public function __construct($value)
17
    {
18 3
        if ($this->isString($value)) {
19 3
            parent::__construct($value);
20
        }
21 3
    }
22
23
    /**
24
     * @param mixed $value
25
     *
26
     * @return bool
27
     */
28 3
    private function isString($value): bool
29
    {
30 3
        return $this->approveIf(is_string($value))->isApproved();
31
    }
32
33
    /**
34
     * @param int $length
35
     *
36
     * @return StringExpectations
37
     */
38 1
    public function hasLength(int $length): self
39
    {
40 1
        return $this->approveIf(strlen($this->value) === $length);
41
    }
42
43
    /**
44
     * @param string $pattern
45
     *
46
     * @return StringExpectations
47
     */
48 1
    public function match(string $pattern): self
49
    {
50 1
        return $this->approveIf(preg_match($pattern, $this->value) === 1);
51
    }
52
}
53