Passed
Push — develop ( a13f6a...04479e )
by Paul
02:02
created

SetAnnotation::compile()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 15
nc 4
nop 0
dl 0
loc 22
rs 8.9197
c 0
b 0
f 0
1
<?php
2
3
namespace PhpUnitGen\Annotation;
4
5
use PhpUnitGen\Annotation\AnnotationInterface\AnnotationInterface;
6
use PhpUnitGen\Exception\AnnotationParseException;
7
use PhpUnitGen\Exception\JsonException;
8
use PhpUnitGen\Util\Json;
9
10
/**
11
 * Class SetAnnotation.
12
 *
13
 * @author     Paul Thébaud <[email protected]>.
14
 * @copyright  2017-2018 Paul Thébaud <[email protected]>.
15
 * @license    https://opensource.org/licenses/MIT The MIT license.
16
 * @link       https://github.com/paul-thebaud/phpunit-generator
17
 * @since      Class available since Release 2.0.0.
18
 */
19
class SetAnnotation extends AbstractAnnotation
20
{
21
    /**
22
     * @var string $property The name of the property to get.
23
     */
24
    private $property;
25
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function getType(): int
30
    {
31
        return AnnotationInterface::TYPE_SET;
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function compile(): void
38
    {
39
        if (strlen($this->getStringContent()) > 0) {
40
            // Decode JSON content
41
            try {
42
                $decoded = Json::decode($this->getStringContent());
43
            } catch (JsonException $exception) {
44
                throw new AnnotationParseException('"setter" annotation content is invalid (invalid JSON content)');
45
            }
46
            if (! is_string($decoded)) {
47
                throw new AnnotationParseException(
48
                    '"setter" annotation content is invalid (property name must be a string)'
49
                );
50
            }
51
            $this->property = $decoded;
52
        } else {
53
            $this->property = preg_replace(
54
                '/^set/',
55
                '',
56
                $this->getParentNode()->/** @scrutinizer ignore-call */ getName()
57
            );
58
            $this->property = lcfirst($this->property);
59
        }
60
    }
61
62
    /**
63
     * @return string The name of the property to get.
64
     */
65
    public function getProperty(): string
66
    {
67
        return $this->property;
68
    }
69
}
70