Completed
Push — master ( 0e6e64...9192b1 )
by Viacheslav
12s
created

PatternPropertySetter   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 28
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0
wmc 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 26 1
1
<?php
2
3
namespace Swaggest\PhpCodeBuilder\Property;
4
5
6
use Swaggest\CodeBuilder\PlaceholderString;
7
use Swaggest\JsonSchema\Exception\StringException;
8
use Swaggest\JsonSchema\Helper;
9
use Swaggest\JsonSchema\InvalidValue;
10
use Swaggest\PhpCodeBuilder\PhpAnyType;
11
use Swaggest\PhpCodeBuilder\PhpClass;
12
use Swaggest\PhpCodeBuilder\PhpCode;
13
use Swaggest\PhpCodeBuilder\PhpConstant;
14
use Swaggest\PhpCodeBuilder\PhpFlags;
15
use Swaggest\PhpCodeBuilder\PhpFunction;
16
use Swaggest\PhpCodeBuilder\PhpNamedVar;
17
use Swaggest\PhpCodeBuilder\PhpStdType;
18
use Swaggest\PhpCodeBuilder\Types\TypeOf;
19
20
class PatternPropertySetter extends PhpFunction
21
{
22 4
    public function __construct(PhpConstant $patternConst, PhpAnyType $type)
23
    {
24 4
        $name = PhpCode::makePhpName($patternConst->getValue(), false);
25 4
        parent::__construct('set' . $name . 'Value', PhpFlags::VIS_PUBLIC);
26
27 4
        $this->addArgument(new PhpNamedVar('name', PhpStdType::string()));
28 4
        $this->addArgument(new PhpNamedVar('value', $type));
29
30 4
        $this->skipCodeCoverage = true;
31 4
        $this->addThrows(PhpClass::byFQN(InvalidValue::class));
32
33 4
        $this->setResult(PhpStdType::tSelf());
34 4
        $this->setBody(
35 4
            new PlaceholderString(<<<PHP
36 4
if (preg_match(:helper::toPregPattern(self::{$patternConst->getName()}), \$name)) {
37
    throw new StringException('Pattern mismatch', :stringException::PATTERN_MISMATCH);
38
}
39 4
\$this->addPatternPropertyName(self::{$patternConst->getName()}, \$name);
40
\$this->{\$name} = \$value;
41
return \$this;
42
43
PHP
44
                ,
45
                array(
46 4
                    ':stringException' => new TypeOf(PhpClass::byFQN(StringException::class)),
47 4
                    ':helper' => new TypeOf(PhpClass::byFQN(Helper::class)),
48
                ))
49
        );
50
    }
51
}