Completed
Push — master ( b6d955...929034 )
by Marcus
02:03
created

StringVal::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Mbright\Validation\Rule\Sanitize;
4
5
class StringVal implements SanitizeRuleInterface
6
{
7
    /** @var null */
8
    protected $find;
9
10
    /** @var null */
11
    protected $replace;
12
13
    /**
14
     * @param string|array $find Find this/these in the value.
15
     * @param string|array $replace Replace with this/these in the value.
16
     *
17
     */
18 9
    public function __construct($find = null, $replace = null)
19
    {
20 9
        $this->find = $find;
0 ignored issues
show
Documentation Bug introduced by
It seems like $find can also be of type string or array. However, the property $find is declared as type null. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
21 9
        $this->replace = $replace;
0 ignored issues
show
Documentation Bug introduced by
It seems like $replace can also be of type string or array. However, the property $replace is declared as type null. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
22 9
    }
23
24
    /**
25
     * Forces the value to a string, optionally applying `str_replace()`.
26
     *
27
     * @param object $subject The subject to be filtered.
28
     * @param string $field The subject field name.
29
     *
30
     * @return bool True if the value was sanitized, false if not.
31
     */
32 9
    public function __invoke($subject, string $field): bool
33
    {
34 9
        $value = (string) $subject->$field;
35 9
        if ($this->find || $this->replace) {
36 9
            $value = str_replace($this->find, $this->replace, $value);
37
        }
38 9
        $subject->$field = $value;
39
40 9
        return true;
41
    }
42
}
43