Completed
Push — develop ( 10d533...fd1d54 )
by Paul
02:00
created

AssertAnnotation   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 42
rs 10
c 0
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A compile() 0 15 4
A getExpected() 0 3 1
A getType() 0 3 1
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 AssertAnnotation.
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 AssertAnnotation extends AbstractAnnotation
20
{
21
    /**
22
     * @var string|null $expected The expected value, null if none.
23
     */
24
    private $expected;
25
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function getType(): int
30
    {
31
        return AnnotationInterface::TYPE_ASSERT;
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('"assertion" annotation content is invalid (invalid JSON content)');
45
            }
46
            if (! is_string($decoded)) {
47
                throw new AnnotationParseException(
48
                    '"assertion" annotation content is invalid (expected value must be a string)'
49
                );
50
            }
51
            $this->expected = $decoded;
52
        }
53
    }
54
55
    /**
56
     * @return string The expected value, null if none.
57
     */
58
    public function getExpected(): ?string
59
    {
60
        return $this->expected;
61
    }
62
}
63