Passed
Push — master ( 9d312b...da6698 )
by Vincent
04:40
created

PhoneChildBuilder::saveAsString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 2
c 1
b 0
f 1
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Bdf\Form\Phone;
4
5
use Bdf\Form\Child\ChildBuilder;
6
use Bdf\Form\ElementBuilderInterface;
7
use Bdf\Form\Phone\Transformer\PhoneNumberToStringTransformer;
8
use Bdf\Form\Registry\RegistryInterface;
9
use libphonenumber\PhoneNumberFormat;
10
11
/**
12
 * @extends ChildBuilder<PhoneElementBuilder>
13
 */
14
class PhoneChildBuilder extends ChildBuilder
15
{
16
    /**
17
     * @var PhoneNumberFormat::*|null
18
     */
19
    private $saveFormat;
20
21
    /**
22
     * {@inheritdoc}
23
     *
24
     * @param PhoneElementBuilder $elementBuilder
25
     */
26 9
    public function __construct(string $name, ElementBuilderInterface $elementBuilder, RegistryInterface $registry = null)
27
    {
28 9
        parent::__construct($name, $elementBuilder, $registry);
29
30 9
        $this->addTransformerProvider([$this, 'provideModelTransformer']);
31 9
    }
32
33
    /**
34
     * The model value of the input will be transformer to a formatted string
35
     *
36
     * <code>
37
     * // The entity : the phone is a simple string
38
     * class MyEntity {
39
     *     public string $phone;
40
     * }
41
     *
42
     * // Build the element
43
     * $builder->phone('phone')->saveAsString()->getter()->setter();
44
     *
45
     * $form->import(MyEntity::get($id));
46
     * $form['phone']->element()->value(); // Value is an instance of PhoneNumber
47
     *
48
     * $entity = $form->value();
49
     * $entity->phone; // phone is a string
50
     * </code>
51
     *
52
     * @param PhoneNumberFormat::*|null $format The phone number format. Must be one of the constant of PhoneNumberFormat. Set null to disable
0 ignored issues
show
Documentation Bug introduced by
The doc comment $format at position 0 could not be parsed: Unknown type name '$format' at position 0 in $format.
Loading history...
53
     *
54
     * @return $this
55
     *
56
     * @see PhoneNumberToStringTransformer
57
     */
58 5
    public function saveAsString(?int $format = PhoneNumberFormat::E164): self
59
    {
60 5
        $this->saveFormat = $format;
0 ignored issues
show
Documentation Bug introduced by
It seems like $format can also be of type integer. However, the property $saveFormat is declared as type libphonenumber\PhoneNumberFormat. 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...
61
62 5
        return $this;
63
    }
64
65
    /**
66
     * @return PhoneNumberToStringTransformer[]
67
     */
68 8
    protected function provideModelTransformer(): array
69
    {
70 8
        if ($this->saveFormat === null) {
71 4
            return [];
72
        }
73
74 4
        return [new PhoneNumberToStringTransformer($this->saveFormat)];
0 ignored issues
show
Bug introduced by
$this->saveFormat of type libphonenumber\PhoneNumberFormat is incompatible with the type integer expected by parameter $format of Bdf\Form\Phone\Transform...nsformer::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

74
        return [new PhoneNumberToStringTransformer(/** @scrutinizer ignore-type */ $this->saveFormat)];
Loading history...
75
    }
76
}
77